UNPKG

7.53 kBJavaScriptView Raw
1#!/usr/bin/env node
2const bump = require("./lib/release/bump.js");
3const chalk = require("chalk");
4const generateFScripts = require("./lib/generateFScripts.js");
5const parseScriptFile = require("./lib/parseScriptsMd.js");
6// const parseScriptPackage = require("./lib/parseScriptsPackage");
7const generateToc = require("./lib/generateToc");
8const upgradePackages = require("./lib/upgradePackages");
9const runSequence = require("./lib/runSequence");
10const runParallel = require("./lib/runParallel");
11const runCLICommand = require("./lib/runCLICommand");
12const { startPackageScripts, startScripts, clearRecent } = require("./lib/startScripts.js");
13const taskName = chalk.rgb(39, 173, 96).bold.underline;
14const textDescription = chalk.rgb(159, 161, 181);
15const optionList = require("./lib/optionList");
16const validateNotInDev = require("./lib/git/validateNotDev.js");
17const encrypt = require("./lib/encryption/encryption");
18
19(async () => {
20 const argv = require("yargs")
21 .usage("Usage: $0 <command> [options]")
22
23 /**
24 * fsr
25 */
26 .command("", "Choose a script runner command", yargs => {}, async function() {})
27 .example(`${taskName("$0")}`, `${textDescription("Choose a script runner command")}`)
28
29 /**
30 * fsr
31 */
32 .command(
33 "branch",
34 "Create new branch instead of Development",
35 yargs => {},
36 async function() {
37 await validateNotInDev();
38 }
39 )
40 .example(`${taskName("$0")}`, `${textDescription("Validates branch and creates new")}`)
41
42 /**
43 * fsr
44 * start --
45 */
46 .command("start", "Choose category then task to run", yargs => {}, async function() {
47 if ((await startScripts()) === false) {
48 await startPackageScripts();
49 }
50 })
51 .example(`${taskName("$0 start")}`, `${textDescription("Open a task selection selector")}`)
52
53 /**
54 * fsr
55 * scripts --
56 */
57 .command("scripts", "Choose a script from package.json", yargs => {}, async function() {
58 await startPackageScripts();
59 })
60 .example(
61 `${taskName("$0 scripts")}`,
62 `${textDescription("Choose a script from package.json")}`
63 )
64
65 /**
66 * fsr
67 * list --
68 */
69 .command("list", "Select any task with text autocompletion", () => {}, async function(
70 argv
71 ) {
72 await startScripts(false);
73 // const tasks = await scriptsParsed().allTasks;
74 })
75 .example(`${taskName("$0 list")}`, `${textDescription("Show you all tasks you can run")}`)
76
77 /**
78 * fsr
79 * run --
80 */
81 .command("run", "Run a specific task", () => {}, async function(argv) {
82 let task = argv._[1];
83 const FcScripts = await parseScriptFile();
84 let taskIndex = FcScripts.allTasks.findIndex(t => t.name === task);
85 let taskData = FcScripts.allTasks[taskIndex];
86 let runCommand = taskData["script"].split(" ");
87 let type = runCommand.shift();
88 let params = runCommand.join(" ");
89 let args = Object.keys(argv)
90 .filter(e => e !== "_" && e !== "$0")
91 .map(e => ` --${e}=${argv[e]}`);
92 params += " " + args.join(" ");
93 await runCLICommand({
94 task: { name: task },
95 script: {
96 type: type,
97 rest: params.split(" ")
98 }
99 });
100 })
101 .example(`${taskName("$0 run start:web")}`, `${textDescription("Run task 'start:web'")}`)
102
103 /**
104 * fsr
105 * upgrade --
106 */
107 .command(
108 "upgrade",
109 "Upgrade all your packages except ones specified by 'ignore-upgrade':[]",
110 () => {},
111 async function(argv) {
112 let task = argv._[1];
113 await upgradePackages();
114 }
115 )
116 .example(`${taskName("$0 upgrade")}`, `${textDescription("Upgraded!")}`)
117
118 /**
119 * fsr
120 * bump --
121 */
122 .command("bump", "Bump package.json and beautify it!", () => {}, async function(argv) {
123 let shouldNotPretty = argv.pretty;
124 let type = argv.type;
125 await bump(shouldNotPretty, type);
126 })
127 .example(`${taskName("$0 bump")}`, `${textDescription("BUMPED AND PRETTY!")}`)
128
129 /**
130 * fsr
131 * run-s --
132 */
133 .command("run-s", "Run a set of tasks one after another", () => {}, async function(argv) {
134 let tasks = argv._.slice();
135 tasks.shift();
136 const FcScripts = await parseScriptFile();
137 await runSequence(tasks, FcScripts);
138 })
139 .example(
140 `${taskName("$0 run-s start:web start:desktop")}`,
141 `${textDescription("Run task 'start:web' and afterwards 'start:desktop'")}`
142 )
143
144 /**
145 * fsr
146 * run-p --
147 */
148 .command("run-p", "Run tasks in parallel", () => {}, async function(argv) {
149 let tasks = argv._.slice();
150 tasks.shift();
151 const FcScripts = await parseScriptFile();
152 await runParallel(tasks, FcScripts);
153 })
154 .example(
155 `${taskName("$0 run-p start:web start:desktop")}`,
156 `${textDescription("Run task 'start:web' and at the same time 'start:desktop'")}`
157 )
158
159 /**
160 * fsr
161 * encryption --
162 */
163 .command("encryption", "Encrypt/Decrypt secret files", () => {}, async function(argv) {
164 await encrypt.init();
165 })
166 .example(
167 `${taskName("$0 encryption")}`,
168 `${textDescription("Encrypt/Decrypt secret files")}`
169 )
170
171 /**
172 * fsr
173 * clear --
174 */
175 .command("clear", "Clear recent task history", () => {}, async function(argv) {
176 await clearRecent();
177 })
178 .example(`${taskName("$0 clear")}`, `${textDescription("Clear your recently run tasks")}`)
179 .command(
180 "generate",
181 "Generate a sample fscripts.md file from the package.json",
182 () => {},
183 async function(argv) {
184 await generateFScripts();
185 }
186 )
187 .example(
188 `${taskName("$0 generate")}`,
189 `${textDescription(
190 "Generates a sample.fscripts.md you can use as template for your fscripts file"
191 )}`
192 )
193 .command(
194 "toc",
195 "Generate updated Table of Contents on top of the fscripts.md file",
196 () => {},
197 async function(argv) {
198 await generateToc();
199 }
200 )
201 .example(
202 `${taskName("$0 toc")}`,
203 `${textDescription(
204 "Generate updated Table of Contents on top of the fscripts.md file"
205 )}`
206 ).argv;
207
208 if (argv._.length === 0) {
209 (async function() {
210 const choice = await optionList();
211 if (choice) {
212 await runCLICommand(
213 { task: { name: choice }, script: { type: "fsr", rest: [choice] } },
214 true
215 );
216 } else {
217 console.log(chalk.green.bold("See you soon!"));
218 }
219 })();
220 }
221})();