1 | #!/usr/bin/env node
|
2 |
|
3 | const chalk = require("chalk");
|
4 | const generateFScripts = require("./lib/generateFScripts.js");
|
5 | const parseScriptFile = require("./lib/parseScriptsMd.js");
|
6 |
|
7 | const generateToc = require("./lib/generateToc");
|
8 | const runSequence = require("./lib/runSequence");
|
9 | const runParallel = require("./lib/runParallel");
|
10 | const runCLICommand = require("./lib/runCLICommand");
|
11 | const { startPackageScripts, startScripts, clearRecent } = require("./lib/startScripts.js");
|
12 | const taskName = chalk.rgb(39, 173, 96).bold.underline;
|
13 | const textDescription = chalk.rgb(159, 161, 181);
|
14 | const optionList = require("./lib/optionList");
|
15 |
|
16 | const argv = require("yargs")
|
17 | .usage("Usage: $0 <command> [options]")
|
18 | .command("", "Choose a script runner command", yargs => {}, async function() {})
|
19 | .example(`${taskName("$0")}`, `${textDescription("Choose a script runner command")}`)
|
20 | .command("start", "Choose category then task to run", yargs => {}, async function() {
|
21 | if ((await startScripts()) === false) {
|
22 | await startPackageScripts();
|
23 | }
|
24 | })
|
25 | .example(`${taskName("$0 start")}`, `${textDescription("Open a task selection selector")}`)
|
26 | .command("scripts", "Choose a script from package.json", yargs => {}, async function() {
|
27 | await startPackageScripts();
|
28 | })
|
29 | .example(`${taskName("$0 scripts")}`, `${textDescription("Choose a script from package.json")}`)
|
30 | .command("list", "Select any task with text autocompletion", () => {}, async function(argv) {
|
31 | await startScripts(false);
|
32 |
|
33 | })
|
34 | .example(`${taskName("$0 list")}`, `${textDescription("Show you all tasks you can run")}`)
|
35 | .command("run", "Run a specific task", () => {}, async function(argv) {
|
36 | let tempArg = {...argv};
|
37 | let task = tempArg._[1];
|
38 | let tasks = argv._.slice(1);
|
39 | delete tempArg._;
|
40 | delete tempArg.$0;
|
41 | let args = Object.keys(tempArg).map(e => `--${e}=${tempArg[e]}`);
|
42 | const FcScripts = await parseScriptFile();
|
43 | let taskIndex = FcScripts.allTasks.findIndex(t => t.name === task);
|
44 | let script = task;
|
45 | let params = tasks.join(" ");
|
46 | let type = "node";
|
47 | if (taskIndex !== -1) {
|
48 | script = FcScripts.allTasks[taskIndex].script;
|
49 | params += " " + script.pop(" ");
|
50 | }
|
51 | console.warn("-- Console tas", script);
|
52 | await runCLICommand({
|
53 | task: { name: taskName },
|
54 | script: {
|
55 | type: type,
|
56 | rest: params
|
57 | }
|
58 | });
|
59 | })
|
60 | .example(`${taskName("$0 run start:web")}`, `${textDescription("Run task 'start:web'")}`)
|
61 | .command("run-s", "Run a set of tasks one after another", () => {}, async function(argv) {
|
62 | let tasks = argv._.slice();
|
63 | tasks.shift();
|
64 | const FcScripts = await parseScriptFile();
|
65 | await runSequence(tasks, FcScripts);
|
66 | })
|
67 | .example(
|
68 | `${taskName("$0 run-s start:web start:desktop")}`,
|
69 | `${textDescription("Run task 'start:web' and afterwards 'start:desktop'")}`
|
70 | )
|
71 | .command("run-p", "Run tasks in parallel", () => {}, async function(argv) {
|
72 | let tasks = argv._.slice();
|
73 | tasks.shift();
|
74 | const FcScripts = await parseScriptFile();
|
75 | await runParallel(tasks, FcScripts);
|
76 | })
|
77 | .example(
|
78 | `${taskName("$0 run-p start:web start:desktop")}`,
|
79 | `${textDescription("Run task 'start:web' and at the same time 'start:desktop'")}`
|
80 | )
|
81 | .command("clear", "Clear recent task history", () => {}, async function(argv) {
|
82 | await clearRecent();
|
83 | })
|
84 | .example(`${taskName("$0 clear")}`, `${textDescription("Clear your recently run tasks")}`)
|
85 | .command(
|
86 | "generate",
|
87 | "Generate a sample fscripts.md file from the package.json",
|
88 | () => {},
|
89 | async function(argv) {
|
90 | await generateFScripts();
|
91 | }
|
92 | )
|
93 | .example(
|
94 | `${taskName("$0 generate")}`,
|
95 | `${textDescription(
|
96 | "Generates a sample.fscripts.md you can use as template for your fscripts file"
|
97 | )}`
|
98 | )
|
99 | .command(
|
100 | "toc",
|
101 | "Generate updated Table of Contents on top of the fscripts.md file",
|
102 | () => {},
|
103 | async function(argv) {
|
104 | await generateToc();
|
105 | }
|
106 | )
|
107 | .example(
|
108 | `${taskName("$0 toc")}`,
|
109 | `${textDescription("Generate updated Table of Contents on top of the fscripts.md file")}`
|
110 | ).argv;
|
111 |
|
112 | if (argv._.length === 0) {
|
113 | (async function() {
|
114 | const choice = await optionList();
|
115 | if (choice) {
|
116 | await runCLICommand(
|
117 | { task: { name: choice }, script: { type: "fsr", rest: [choice] } },
|
118 | true
|
119 | );
|
120 | } else {
|
121 | console.log(chalk.green.bold("See you soon!"));
|
122 | }
|
123 | })();
|
124 | }
|