forked from ruyadorno/ntl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·295 lines (262 loc) · 6.95 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env node
"use strict";
import os from "os"
import path from "path"
import { execSync } from "child_process"
import yargs from "yargs/yargs"
import ipt from "ipt"
import out from "simple-output"
import { readPackageSync } from "read-pkg"
import Cache from "lru-cache-fs"
const sep = os.EOL;
const defaultRunner = "npm";
const { argv } = yargs(getMainArgs())
.usage(
"Usage:\n ntl [<path>] Build an interactive interface and run any script"
)
.usage(" nt [<path>] Rerun last executed script")
.alias("a", "all")
.describe("a", "Includes pre and post scripts on the list")
.alias("A", "autocomplete")
.describe("A", "Starts in autocomplete mode")
.alias("D", "debug")
.describe("D", "Prints to stderr any internal error")
.alias("d", "descriptions")
.describe("d", "Displays the descriptions of each script")
.alias("o", "descriptions-only")
.describe("o", "Limits output to scripts with a description")
.help("h")
.alias("h", "help")
.describe("h", "Shows this help message")
.alias("i", "info")
.describe("i", "Displays the contents of each script")
.alias("e", "exclude")
.describe("e", "Excludes specific scripts")
.alias("m", "multiple")
.describe("m", "Allows the selection of multiple items")
.alias("O", "ordered")
.describe("O", "Selects multiple items in order")
.alias("s", "size")
.describe("s", "Amount of lines to display at once")
.alias("v", "version")
.describe("rerun", "Repeat last executed script")
.alias("r", "rerun")
.boolean(["a", "A", "D", "d", "o", "h", "i", "m", "O", "v", "r", "no-rerun-cache"])
.number(["s"])
.array(["e"])
.string(["rerun-cache-dir", "rerun-cache-name"])
.describe("rerun-cache-dir", "Defines the rerun task cache location")
.describe("rerun-cache-name", "Defines the rerun task cache filename")
.describe("no-rerun-cache", "Never write to or read from cache")
.epilog("Visit https://github.com/ruyadorno/ntl for more info");
let cache;
const cwd = argv._[0] ? path.resolve(process.cwd(), argv._[0]) : process.cwd();
const {
autocomplete,
multiple,
ordered,
noRerunCache,
rerun,
rerunCacheDir,
rerunCacheName,
size
} = argv;
const { ntl, scripts, packageManager = '' } = getCwdPackage() || {};
const [corepackRunner] = packageManager.split('@')
const runner = (ntl && ntl.runner) || process.env.NTL_RUNNER || corepackRunner || defaultRunner;
const { descriptions = {} } = ntl || {};
const scriptKeys = Object.keys(scripts || {});
const noScriptsFound = !scripts || scriptKeys.length < 1;
const avoidCache = noRerunCache || process.env.NTL_NO_RERUN_CACHE;
const shouldRerun = !avoidCache && (rerun || process.env.NTL_RERUN);
// Exits program execution on ESC
process.stdin.on("keypress", (ch, key) => {
if (key && key.name === "escape") {
process.exit(0);
}
});
function error(e, msg) {
out.error(argv.debug ? e : msg);
process.exit(1);
}
function getMainArgs() {
let i = -1;
const result = [];
const mainArgs = process.argv.slice(2);
while (++i < mainArgs.length) {
if (mainArgs[i] === "--") break;
result.push(mainArgs[i]);
}
return result;
}
function getTrailingOptions() {
let sepFound = false;
return process.argv.slice(2).reduce((res, i) => {
if (i === "--") {
sepFound = true;
}
if (sepFound) {
return `${res} ${i}`;
}
return res;
}, "");
}
function getCwdPackage() {
try {
return readPackageSync({ cwd });
} catch (e) {
const [errorType] = Object.values(e);
error(
e,
errorType === "JSONError"
? "package.json contains malformed JSON"
: "No package.json found"
);
}
}
function retrieveCache() {
if (avoidCache) {
return;
}
if (!cache) {
cache = new Cache({
cacheName:
rerunCacheName || process.env.NTL_RERUN_CACHE_NAME || "ntl-rerun-cache",
cwd: rerunCacheDir || process.env.NTL_RERUN_CACHE_DIR,
max: parseInt(process.env.NTL_RERUN_CACHE_MAX, 10) || 10
});
}
return cache;
}
function hasCachedTasks() {
if (!shouldRerun) {
return;
}
function runCachedTask() {
let rerunCachedTasks;
const warn = () => {
out.warn("Unable to retrieve commands to rerun");
return false;
};
try {
rerunCachedTasks = retrieveCache().get(cacheKey(cwd));
} catch (e) {
return warn();
}
if (!rerunCachedTasks || !rerunCachedTasks.length) {
return warn();
}
executeCommands(rerunCachedTasks);
return true;
}
return runCachedTask();
}
function cacheKey(str) {
return str.split("\\").join("/");
}
function setCachedTasks(keys) {
try {
retrieveCache().set(cacheKey(cwd), keys);
retrieveCache().fsDump();
} catch (e) {
// should ignore rerun set cache errors
}
}
function getDefaultTask() {
try {
return retrieveCache()
.get(cacheKey(cwd))
.join(sep);
} catch (e) {
return undefined;
}
}
function executeCommands(keys) {
keys.forEach(key => {
execSync(`${runner} run ${key}${getTrailingOptions()}`, {
cwd,
stdio: [process.stdin, process.stdout, process.stderr]
});
});
}
function run() {
const descriptionsKeys = Object.keys(descriptions);
const hasDescriptions =
descriptionsKeys.length > 0 && descriptionsKeys.some(key => scripts[key]);
const shouldWarnNoDescriptions = argv.descriptions && !hasDescriptions;
if (shouldWarnNoDescriptions) {
out.warn(`No descriptions for your ${runner} scripts found`);
}
const longestScriptName = scriptKeys.reduce(
(acc, curr) => (curr.length > acc.length ? curr : acc),
""
).length;
const getLongName = (name, message = "", pad) =>
`${name.padStart(longestScriptName)} › ${message}`;
// defines the items that will be printed to the user
const input = scriptKeys
.map(key => ({
name:
argv.info || argv.descriptions
? getLongName(
key,
argv.descriptions && descriptions[key]
? descriptions[key]
: scripts[key]
)
: hasDescriptions
? getLongName(key, descriptions[key])
: key,
value: key
}))
.filter(
// filter out prefixed scripts
item =>
argv.all
? true
: ["pre", "post"].every(prefix => !item.value.startsWith(prefix))
)
.filter(
// filter out scripts without a description if --descriptions-only option
item => (argv.descriptionsOnly ? descriptions[item.value] : true)
)
.filter(
// filter excluded scripts
item =>
!argv.exclude ||
!argv.exclude.some(e =>
new RegExp(e + (e.includes("*") ? "" : "$"), "i").test(item.value)
)
);
const message = `Select a task to run${runner !== defaultRunner ? ` (using ${runner})` : ""
}:`;
if (hasCachedTasks()) {
return;
}
if (!input || input.length === 0) {
return out.error("No tasks remained, maybe try less options?");
}
out.node("Node Task List");
// creates interactive interface using ipt
ipt(input, {
autocomplete,
default: getDefaultTask(),
"default-separator": sep,
message,
multiple,
ordered,
size
})
.then(keys => {
setCachedTasks(keys);
executeCommands(keys);
})
.catch(err => {
error(err, "Error building interactive interface");
});
}
if (noScriptsFound) {
out.info(`No ${runner} scripts available in cwd`);
} else {
run();
}