forked from webpack/webpack-cli
/
cli-executer.js
64 lines (54 loc) · 1.79 KB
/
cli-executer.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
const { MultiSelect, Input } = require('enquirer');
const { cyan } = require('colorette');
const logger = require('./logger');
const cliArgs = require('./cli-flags').core;
const prompter = async () => {
const args = [];
const typePrompt = new MultiSelect({
name: 'type',
message: 'Which flags do you want to use?',
choices: cliArgs.reduce((prev, curr) => {
return [...prev, `--${curr.name}: ${curr.description}`];
}, []),
result: (value) => {
return value.map((flag) => flag.split(':')[0]);
},
});
const selections = await typePrompt.run();
const boolArgs = [];
const questions = [];
selections.forEach((selection) => {
const options = cliArgs.find((flag) => {
return flag.name === selection.slice(2);
});
if (options.type === Boolean) {
boolArgs.push(selection);
return;
}
const valuePrompt = new Input({
name: 'value',
message: `Enter value of the ${selection} flag`,
initial: options.defaultValue,
result: (value) => [selection, value],
validate: (value) => Boolean(value),
});
questions.push(valuePrompt);
});
// Create promise chain to force synchronous prompt of question
for await (const question of questions) {
const flagArgs = await question.run();
args.push(...flagArgs);
}
return [...args, ...boolArgs];
};
const run = async () => {
try {
const args = await prompter();
logger.info('\nExecuting CLI\n');
return args;
} catch (err) {
logger.error(`Action Interrupted, use ${cyan('webpack-cli help')} to see possible options.`);
process.exit(2);
}
};
module.exports = run;