-
Notifications
You must be signed in to change notification settings - Fork 19
/
command-parser.js
47 lines (43 loc) · 1.11 KB
/
command-parser.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
module.exports = args => {
args = args.map(a => a.trim()).filter(Boolean);
const env = {};
const cmd = [];
let envDone;
let lastEnv;
for (const arg of args) {
if (envDone) {
cmd.push(arg);
} else if (lastEnv) {
let value = arg;
if (value.endsWith(`'`)) {
value = value.substr(0, value.length - 1);
env[lastEnv] += ' ' + value
lastEnv = undefined;
} else {
env[lastEnv] += ' ' + value
}
} else if (!arg.match(/=/)) {
envDone = true;
cmd.push(arg);
} else {
const firstSplit = arg.indexOf('=');
const envVar = arg.substr(0, firstSplit);
let value = arg.substr(firstSplit + 1);
if (!envVar || !value || firstSplit === -1) {
exit(`Invalid env var:`, arg);
return;
} else {
if (value.startsWith(`'`)) {
if (value.endsWith(`'`)) {
value = value.substring(1, value.length - 1);
} else {
value = value.substr(1);
lastEnv = envVar;
}
}
env[envVar] = value;
}
}
}
return { env, cmd };
}