-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·66 lines (55 loc) · 2.21 KB
/
cli.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
#!/usr/bin/env node
const readmix = require("./dist");
const { commands, helpers } = readmix;
function main () {
const [,, ...args] = process.argv;
const singleFlags = args.filter(helpers.isSingleFlag);
const doubleFlags = args.filter(helpers.isDoubleFlag);
const cmdOptions = commands.getDefaultCmdOptions();
readmix.flags.forEach(flag => {
if (singleFlags.includes(flag.singleFlag) || doubleFlags.includes(flag.doubleFlag)) {
cmdOptions[flag.name] = true;
}
});
const commandFlags = readmix.flags.filter(flag => flag.isCommand);
const trueCommandFlags = commandFlags.filter(flag => cmdOptions[flag.name] === true);
if (trueCommandFlags.length === 0) {
// No command flags were specified
console.log(`No commands to run!`);
console.log(`\tYou must supply exactly 1 of the following flags:`);
commandFlags.forEach((c, i) => console.log(`\t\tcommand #${i+1}: ${c.doubleFlag.padEnd(12)} => ${c.description}`));
return;
} else if (trueCommandFlags.length > 1) {
// Multiple command flags were specified
console.log(`Multiple commands cannot be run at the same time!`);
trueCommandFlags.forEach((c, i) => console.log(`\tcommand #${i+1}: ${c.doubleFlag.padEnd(12)} => ${c.description}`));
return;
}
const pathArgs = args.filter(helpers.isNotFlag);
if (pathArgs.length === 0) {
pathArgs.push(".");
}
const goodPaths = pathArgs.filter(helpers.pathExists);
const rxFiles = helpers.getRxFilesInDirectories(goodPaths, cmdOptions.recursive);
if (cmdOptions.api) {
commands.apiCommand(rxFiles, cmdOptions);
} else if (cmdOptions.compile) {
commands.compileCommand(rxFiles, cmdOptions);
} else if (cmdOptions.init) {
commands.initCommand(rxFiles, cmdOptions);
} else if (cmdOptions.list) {
commands.listCommand(rxFiles, cmdOptions);
} else if (cmdOptions.preview || cmdOptions.open) {
// open will run preview and then open it in the browser
commands.previewCommand(rxFiles, cmdOptions);
} else if (cmdOptions.version) {
commands.versionCommand(rxFiles, cmdOptions);
} else if (cmdOptions.watch) {
commands.watchCommand(rxFiles, cmdOptions);
}
const badPaths = pathArgs.filter(helpers.pathDoesNotExist);
for (const badPath of badPaths) {
console.log(`Path Not Found: '${badPath}'`);
}
}
main();