/
cli.js
executable file
·73 lines (61 loc) · 1.58 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
67
68
69
70
71
72
73
#!/usr/bin/env node
const chalk = require("chalk");
const inquirer = require("inquirer");
const inquirerAutocompletePrompt = require("inquirer-autocomplete-prompt");
const fuzzy = require("fuzzy");
const execa = require("execa");
const loadScripts = require("./loadScripts");
const processScripts = require("./processScripts");
try {
execa.sync("nps", ["-v"], {
preferLocal: true
});
} catch (_) {
console.error(chalk.red("Could not find: nps"));
process.exit(1);
}
function npsExec(str) {
execa("nps", [str], {
preferLocal: true,
stdio: "inherit"
}).catch(({ exitCode }) => {
process.exit(exitCode);
});
}
if (process.argv.length > 2) {
return npsExec(process.argv.slice(2).join(" "));
}
const scripts = loadScripts();
const tasks = processScripts(scripts);
const name = "task";
const separator = chalk.cyan(" --- ");
inquirer.registerPrompt("autocomplete", inquirerAutocompletePrompt);
function taskInfo(info) {
if (typeof info === "string") {
return chalk.dim(info);
}
return `${info[0]} - ${chalk.dim(info[1])}`;
}
function searchTask(_, input) {
const fuzzyResult = fuzzy.filter(input || "", Object.keys(tasks));
const result = fuzzyResult.map(el => el.original);
return Promise.resolve(
result.map(key => `${key}${separator}${taskInfo(tasks[key])}`)
);
}
inquirer
.prompt([
{
type: "autocomplete",
name,
message: "Select an nps task",
source: searchTask
}
])
.then(answers => {
const result = answers[name].split(separator)[0];
npsExec(result);
});
process.on("SIGINT", () => {
process.exit();
});