-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.js
More file actions
67 lines (59 loc) · 1.69 KB
/
Copy pathconfig.js
File metadata and controls
67 lines (59 loc) · 1.69 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
import os from 'os';
import fs from 'fs';
const configLocation = os.homedir() + '/.dwc';
let localConfig;
export function configCommand() {
return {
command: 'config',
describe: 'Edit the configs located in ~/.dwc',
handler: (argv) => handleConfig(argv),
builder: {
prop: {
type: 'string',
describe: 'Path to your config property, i.e --env.dev.host=newHost:1000'
}
}
}
}
export function setupConfig() {
try {
localConfig = JSON.parse(fs.readFileSync(configLocation));
} catch (e) {
localConfig = {}
}
}
export function getConfig() {
return localConfig || {};
}
/**
* Overrides the in-memory config for testing.
* @param {Object} config - Must be a plain, non-null object; handleConfig writes keys directly onto it.
*/
export function setConfigForTests(config) {
if (config === null || typeof config !== 'object') {
throw new Error('setConfigForTests: config must be a plain object');
}
localConfig = config;
}
export function handleConfig(argv) {
localConfig = localConfig || {};
Object.keys(argv).forEach(a => {
if (a != '_' && a != '$0') {
localConfig[a] = resolveConfig(a, argv[a], localConfig[a] || {});
updateConfig();
}
})
}
export function updateConfig() {
fs.writeFileSync(configLocation, JSON.stringify(localConfig));
}
function resolveConfig(key, obj, conf) {
if (typeof obj !== 'object' || !(obj instanceof Object)) {
return obj;
}
Object.keys(obj).forEach(a => {
conf[a] = conf[a] || {};
conf[a] = resolveConfig(key, obj[a], conf[a]);
})
return conf;
}