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 task = argv._[1];
|
37 | const FcScripts = await parseScriptFile();
|
38 | let taskIndex = FcScripts.allTasks.findIndex(t => t.name === task);
|
39 | let taskData = FcScripts.allTasks[taskIndex];
|
40 | let runCommand = taskData["script"].split(" ");
|
41 | let type = runCommand.shift();
|
42 | let params = runCommand.join(" ");
|
43 | let args = Object.keys(argv)
|
44 | .filter(e => e !== "_" && e !== "$0")
|
45 | .map(e => ` --${e}=${argv[e]}`);
|
46 | params += " " + args.join(" ");
|
47 | await runCLICommand({
|
48 | task: { name: task },
|
49 | script: {
|
50 | type: type,
|
51 | rest: params.split(" ")
|
52 | }
|
53 | });
|
54 | })
|
55 | .example(`${taskName("$0 run start:web")}`, `${textDescription("Run task 'start:web'")}`)
|
56 | .command("run-s", "Run a set of tasks one after another", () => {}, async function(argv) {
|
57 | let tasks = argv._.slice();
|
58 | tasks.shift();
|
59 | const FcScripts = await parseScriptFile();
|
60 | await runSequence(tasks, FcScripts);
|
61 | })
|
62 | .example(
|
63 | `${taskName("$0 run-s start:web start:desktop")}`,
|
64 | `${textDescription("Run task 'start:web' and afterwards 'start:desktop'")}`
|
65 | )
|
66 | .command("run-p", "Run tasks in parallel", () => {}, async function(argv) {
|
67 | let tasks = argv._.slice();
|
68 | tasks.shift();
|
69 | const FcScripts = await parseScriptFile();
|
70 | await runParallel(tasks, FcScripts);
|
71 | })
|
72 | .example(
|
73 | `${taskName("$0 run-p start:web start:desktop")}`,
|
74 | `${textDescription("Run task 'start:web' and at the same time 'start:desktop'")}`
|
75 | )
|
76 | .command("clear", "Clear recent task history", () => {}, async function(argv) {
|
77 | await clearRecent();
|
78 | })
|
79 | .example(`${taskName("$0 clear")}`, `${textDescription("Clear your recently run tasks")}`)
|
80 | .command(
|
81 | "generate",
|
82 | "Generate a sample fscripts.md file from the package.json",
|
83 | () => {},
|
84 | async function(argv) {
|
85 | await generateFScripts();
|
86 | }
|
87 | )
|
88 | .example(
|
89 | `${taskName("$0 generate")}`,
|
90 | `${textDescription(
|
91 | "Generates a sample.fscripts.md you can use as template for your fscripts file"
|
92 | )}`
|
93 | )
|
94 | .command(
|
95 | "toc",
|
96 | "Generate updated Table of Contents on top of the fscripts.md file",
|
97 | () => {},
|
98 | async function(argv) {
|
99 | await generateToc();
|
100 | }
|
101 | )
|
102 | .example(
|
103 | `${taskName("$0 toc")}`,
|
104 | `${textDescription("Generate updated Table of Contents on top of the fscripts.md file")}`
|
105 | ).argv;
|
106 |
|
107 | if (argv._.length === 0) {
|
108 | (async function() {
|
109 | const choice = await optionList();
|
110 | if (choice) {
|
111 | await runCLICommand(
|
112 | { task: { name: choice }, script: { type: "fsr", rest: [choice] } },
|
113 | true
|
114 | );
|
115 | } else {
|
116 | console.log(chalk.green.bold("See you soon!"));
|
117 | }
|
118 | })();
|
119 | }
|