-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessFlags.js
48 lines (42 loc) · 1.2 KB
/
processFlags.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
module.exports = function processFlags(flags) {
let { d, define, e, env, ...other } = flags;
if (Object.keys(other).length > 0) {
throw new Error('unknown ts-build option ' + Object.keys(other).join(', '));
}
if (!Array.isArray(d)) {
d = typeof d !== 'undefined' ? [d] : [];
}
if (!Array.isArray(define)) {
define = typeof define !== 'undefined' ? [define] : [];
}
if (!Array.isArray(e)) {
e = typeof e !== 'undefined' ? [e] : [];
}
if (!Array.isArray(env)) {
env = typeof env !== 'undefined' ? [env] : [];
}
const allEnvs = [...e, ...env]
.map(x => x.split(','))
.reduce((a, x) => [...a, ...x], [])
.reduce((a, x) => ({ ...a, [x]: process.env[x] }), {});
const allDefines = [...d, ...define];
const defineVars = allDefines
.map(x => {
const i = x.indexOf('=');
if (i === 0) {
throw new Error('expected define flag to look like key=value');
}
if (i < 0) {
// auto boolean
return { k: x, v: true };
}
return {
k: x.substring(0, i),
v: x.substring(i + 1),
};
})
.reduce((a, { k, v }) => ({ ...a, [k]: v }), {});
return {
define: { ...defineVars, ...allEnvs },
};
};