-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (115 loc) · 3.39 KB
/
index.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const argv = require('yargs').argv;
const fs = require('fs');
const path = require('path');
const Case = require('case');
function isObject(v) {
return typeof v === 'object' && v !== null;
}
function maybeNumber(v, b) {
if (!b) {
return v;
}
try {
const n = Number(v);
return Number.isNaN(n) ? v : n;
}
catch (err) {
return v;
}
}
function computeArgsConf(args) {
if (args._) {
for (const singleArg of args._) {
if (!args.hasOwnProperty(singleArg)) {
args[singleArg] = undefined;
}
}
delete args._;
}
return args;
}
function computeFileConf(config) {
let fileConf = {};
if (typeof config.filename === 'string') {
const filepath = path.join(process.cwd(), config.filename);
try {
if (fs.existsSync(filepath)) {
fileConf = require(filepath);
}
} catch (err) {
// fails silently
}
}
return fileConf;
}
function computeEnvConf(defaultConf, fileConf, env) {
let envConf = {};
for (const [k, v] of Object.entries(env)) {
if (isObject(defaultConf)) {
const defaultConfKey = Object.keys(defaultConf).find(ck => Case.camel(ck) === Case.camel(k))
if (defaultConfKey) {
envConf[defaultConfKey] = maybeNumber(v, typeof defaultConf[defaultConfKey] === 'number');
}
}
if (isObject(fileConf)) {
const fileConfKey = Object.keys(fileConf).find(fk => Case.camel(fk) === Case.camel(k))
if (fileConfKey) {
envConf[fileConfKey] = maybeNumber(v, typeof fileConf[fileConfKey] === 'number');
}
}
}
return envConf;
}
function computeShortcuts(config, userShortcuts, conf) {
let ret = {};
let shortcuts = {};
if (isObject(config.shortcuts)) {
shortcuts = config.shortcuts;
}
shortcuts = {...shortcuts, ...userShortcuts};
for (const [key, value] of Object.entries(conf)) {
if (Object.keys(shortcuts).includes(value)) {
ret[key] = shortcuts[value];
} else {
ret[key] = value;
}
}
return ret;
}
function fconf(config, testHooks = {}) {
if (!config) {return;}
// default configuration
let defaultConf = testHooks.defaults || config.defaults;
// command line arguments
// flatten argv, only key,value like command line arguments (non positional) are supported
let args = argv;
if (testHooks.argv) {
args = testHooks.argv;
}
const argsConf = computeArgsConf(args);
// configuration file
let fileConf = {};
if (testHooks.fileConf) {
fileConf = testHooks.fileConf;
}
else {
fileConf = computeFileConf(config);
}
let userShortcuts = {};
if (isObject(fileConf.shortcuts)) {
userShortcuts = fileConf.shortcuts;
delete fileConf.shortcuts;
}
// environment variables
// environment variables gets into final return value only if it overrides a similar key
let env = process.env;
if (testHooks.env) {
env = testHooks.env;
}
let envConf = computeEnvConf(defaultConf, fileConf, env);
let conf = {...defaultConf, ...fileConf, ...envConf, ...argsConf};
// compute shortcuts
let ret = computeShortcuts(config, userShortcuts, conf);
return ret;
}
module.exports = fconf;