-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli-parse.js
216 lines (170 loc) Β· 5.09 KB
/
cli-parse.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict';
const os = require('os');
const yargsParser = require('yargs-parser');
const log = require('debug')('redrun:cli-parse');
const pkg = require('../package');
const helpjson = require('../help');
const regexp = require('./regexp');
const forEachKey = require('for-each-key');
module.exports = (argv, scripts, fn) => {
log(`argv: ${argv}`);
check(argv, scripts, fn);
const getArguments = (index, argv) => {
return !~index ? '' : ' ' + argv.slice(index + 1).join(' ');
};
const cutArguments = (index, argv) => {
return !~index ? argv : argv.slice(0, index);
};
const indexOfParams = argv.indexOf('--');
const params = getArguments(indexOfParams, argv);
argv = cutArguments(indexOfParams, argv);
const args = parseArgs(argv);
let result;
const unknownName = findUnknown(args);
if (unknownName) {
result = build('unknown', unknown, unknownName);
} else if (args.version) {
result = build('version', version);
} else if (args.help || !args._.concat(args.parallel, args.series, args.parallelCalm, args.seriesCalm).length) {
result = build('help', help);
} else {
const cmd = parse(args, params, scripts);
if (!cmd)
result = build('script-not-found', scriptNotFound, args);
else
result = {
name: 'run',
quiet: args.quiet,
calm: args.calm,
cmd,
};
}
log(`result: ${result.cmd || result.output}`);
return result;
};
function parseArgs(args) {
return yargsParser(args, {
array: [
'series',
'parallel',
'series-calm',
'parallel-calm'
],
boolean: [
'version',
'help',
'quiet',
'calm'
],
alias: {
's': 'series',
'p': 'parallel',
'S': 'series-calm',
'P': 'parallel-calm',
'v': 'version',
'h': 'help',
'q': 'quiet',
'c': 'calm'
},
default: {
'series': [],
'parallel': [],
'parallel-calm': [],
'series-calm': [],
'version': false,
'help': false,
'quiet': false,
'calm': false
}
});
}
function build(name, fn, arg) {
return {
name,
output: fn(arg),
};
}
module.exports.help = help;
module.exports.version = version;
module.exports.unknown = unknown;
module.exports.scriptNotFound = scriptNotFound;
function check(argv, scripts) {
if (!argv)
throw Error('argv should be an array!');
if (!scripts)
throw Error('scripts should be object!');
}
function findUnknown(args) {
const argsRegExp = regexp.cli;
const keys = Object.keys(args);
const test = (a) => !argsRegExp.test(a);
return keys
.filter(test)
.pop();
}
function version() {
return 'v' + pkg.version;
}
function help() {
let result = 'Usage: ' + pkg.name + ' [...tasks] [options] [-- ...args]\n';
result += 'Options:\n';
const addStr = (key, value) =>{
result +=` ${key} ${value}\n`;
};
forEachKey(addStr, helpjson);
return result;
}
function unknown(cmd) {
if (cmd.length === 1)
cmd = '-' + cmd;
else
cmd = '--' + cmd;
const name = pkg.name;
const result =
`${cmd} is not a ${name} option. ` +
`See '${name} --help'`;
return result;
}
function scriptNotFound(argv) {
const argStr = argv._.concat(argv.s, argv.p).join(' ');
const result = `One of scripts not found: ${argStr}`;
return result;
}
function parse(args, params, scripts) {
let cmd = '';
const and = (cmd) => cmd && ` && ${cmd}`;
const calmDown = (array) => array.map((name) => {
return `${name} || ${calm()}`;
});
const calmIf = (array) => {
return !args.calm ? array : calmDown(array);
};
if (args.parallel.length)
cmd = parallel(args.parallel, params, scripts);
if (args.parallelCalm.length) {
const parallelCalm = calmDown(args.parallelCalm);
cmd = parallel(parallelCalm, params, scripts) + and(cmd);
}
if (args.seriesCalm.length) {
const seriesCalm = calmDown(args.seriesCalm);
cmd = series(seriesCalm, params, scripts) + and(cmd);
}
const names = calmIf(args._).concat(args.series);
if (names.length)
cmd = series(names, params, scripts) + and(cmd);
return cmd;
}
function calm() {
const platform = os.platform();
const isWin = platform === 'win32';
const cmd = isWin ? '(exit 0)' : 'true';
return cmd;
}
function series(names, params, scripts) {
const groupParse = require('./group-parse');
return groupParse(names, {params}, scripts);
}
function parallel(names, params, scripts) {
const groupParse = require('./group-parse');
return groupParse(names, {params, parallel: true}, scripts);
}