This repository was archived by the owner on Mar 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcredentials.js
More file actions
76 lines (70 loc) · 2.22 KB
/
credentials.js
File metadata and controls
76 lines (70 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
const mapValues = require('lodash/mapValues');
const pick = require('lodash/pick');
const fs = require('fs');
const yaml = require('js-yaml');
const args = require('minimist')(process.argv.slice(2), {
string: ['env']
});
const log = console.log.bind(console, '>| ');
const load = dir => {
const fileNames = fs
.readdirSync(dir, {encoding: 'UTF-8'})
.filter(fileName => /^\.account\.(json|ya?ml)$/.test(fileName));
return fileNames.length > 0 ? `${dir}/${fileNames[0]}` : null;
};
/**
* Load JSON or YAML configuration file
* @returns {object}
*/
const loadConf = () => {
const fileName = load(process.env.HOME) || load(__dirname);
if (fileName) {
log(`Reading credentials from: ${fileName.replace(process.env.HOME, '$HOME')}`);
const fileContent = fs.readFileSync(fileName, 'utf8');
return !/\.json$/.test(fileName) ? yaml.safeLoad(fileContent) : JSON.parse(fileContent);
}
log('No credentials file found');
return null;
};
/**
* List of configurations variables
* @type {string[]}
*/
const confValues = [
'WEBCOM_EMAIL',
'WEBCOM_PASSWORD',
'WEBCOM_TOKEN',
'WEBCOM_DOMAIN',
'WEBCOM_PROTOCOL',
'WEBCOM_NAMESPACE',
'NODE_ENV'
];
// Account configuration from file
let fromFile = loadConf();
if (fromFile) {
// Select ENV
if (args.env && fromFile[args.env]) {
fromFile = fromFile[args.env];
}
fromFile = pick(fromFile, confValues);
}
// Account configuration from ENV variables
const fromEnv = pick(process.env, confValues);
// Mixes ENV variables & .account.json content for tests configuration
const credentials = Object.assign(fromEnv, fromFile);
// Default values
if (!credentials.WEBCOM_DOMAIN || !/^[a-z:0-9\.]+$/.test(credentials.WEBCOM_DOMAIN)) {
credentials.WEBCOM_DOMAIN = 'io.datasync.orange.com';
}
if (!credentials.WEBCOM_PROTOCOL || !/^https?$/.test(credentials.WEBCOM_PROTOCOL)) {
credentials.WEBCOM_PROTOCOL = 'https';
}
confValues.forEach(confValue => {
credentials[confValue] = credentials[confValue] || null;
const logValue = !~['WEBCOM_PASSWORD', 'WEBCOM_TOKEN'].indexOf(confValue) || !credentials[confValue] ?
credentials[confValue] : '**********';
log(`${confValue} = ${logValue}`);
});
// Encloses strings for webpack globals
module.exports = mapValues(credentials, v => v ? `'${v}'` : null);