@jpillora/take 0.8.5 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/take.mjs +25 -36
package/package.json
CHANGED
package/take.mjs
CHANGED
|
@@ -1,30 +1,19 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
// Take is a mini-CLI library for building typescript-based command-line tools
|
|
3
2
|
// Works with Deno, Node.js (with type stripping), and Bun
|
|
4
3
|
// deno-lint-ignore-file no-explicit-any
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
exports.newFlags = newFlags;
|
|
11
|
-
exports.help = help;
|
|
12
|
-
exports.spawn = spawn;
|
|
13
|
-
exports.Register = Register;
|
|
14
|
-
exports.Command = Command;
|
|
15
|
-
const promises_1 = require("node:fs/promises");
|
|
16
|
-
const node_path_1 = require("node:path");
|
|
17
|
-
const node_child_process_1 = require("node:child_process");
|
|
18
|
-
const node_process_1 = __importDefault(require("node:process"));
|
|
19
|
-
function newFlags(flags) {
|
|
4
|
+
import { readFile } from "node:fs/promises";
|
|
5
|
+
import { basename } from "node:path";
|
|
6
|
+
import { spawn as nodeSpawn, } from "node:child_process";
|
|
7
|
+
import process from "node:process";
|
|
8
|
+
export function newFlags(flags) {
|
|
20
9
|
return flags;
|
|
21
10
|
}
|
|
22
|
-
function help(str) {
|
|
11
|
+
export function help(str) {
|
|
23
12
|
throw str;
|
|
24
13
|
}
|
|
25
14
|
const exit = (...args) => {
|
|
26
15
|
console.log(...args);
|
|
27
|
-
|
|
16
|
+
process.exit(1);
|
|
28
17
|
};
|
|
29
18
|
function namedFlags(record) {
|
|
30
19
|
return Object.entries(record).map((kv) => ({
|
|
@@ -49,9 +38,9 @@ function convert(val, type) {
|
|
|
49
38
|
throw `unknown type: ${type}`;
|
|
50
39
|
}
|
|
51
40
|
}
|
|
52
|
-
async function spawn(options) {
|
|
41
|
+
export async function spawn(options) {
|
|
53
42
|
return await new Promise((resolve, reject) => {
|
|
54
|
-
const child = (
|
|
43
|
+
const child = nodeSpawn(options.program, options.args ?? [], options);
|
|
55
44
|
child.on("close", (code) => {
|
|
56
45
|
if (code === 0) {
|
|
57
46
|
resolve(0);
|
|
@@ -63,7 +52,7 @@ async function spawn(options) {
|
|
|
63
52
|
});
|
|
64
53
|
}
|
|
65
54
|
// helper for measuring time
|
|
66
|
-
|
|
55
|
+
export const timer = (() => {
|
|
67
56
|
const scale = [
|
|
68
57
|
[1000, "ms"],
|
|
69
58
|
[60, "sec"],
|
|
@@ -93,7 +82,7 @@ exports.timer = (() => {
|
|
|
93
82
|
// Load .env file if it exists
|
|
94
83
|
async function loadEnvFile(path) {
|
|
95
84
|
try {
|
|
96
|
-
const content = await
|
|
85
|
+
const content = await readFile(path, "utf-8");
|
|
97
86
|
for (const line of content.split("\n")) {
|
|
98
87
|
const trimmed = line.trim();
|
|
99
88
|
if (!trimmed || trimmed.startsWith("#"))
|
|
@@ -107,7 +96,7 @@ async function loadEnvFile(path) {
|
|
|
107
96
|
(value.startsWith("'") && value.endsWith("'"))) {
|
|
108
97
|
value = value.slice(1, -1);
|
|
109
98
|
}
|
|
110
|
-
|
|
99
|
+
process.env[key] = value;
|
|
111
100
|
}
|
|
112
101
|
}
|
|
113
102
|
return true;
|
|
@@ -116,11 +105,11 @@ async function loadEnvFile(path) {
|
|
|
116
105
|
return false;
|
|
117
106
|
}
|
|
118
107
|
}
|
|
119
|
-
async function Register(...commands) {
|
|
108
|
+
export async function Register(...commands) {
|
|
120
109
|
// Load .env by default
|
|
121
110
|
await loadEnvFile(".env");
|
|
122
111
|
// Get script name from argv[1]
|
|
123
|
-
const scriptName =
|
|
112
|
+
const scriptName = basename(process.argv[1] || "cli");
|
|
124
113
|
// validate commands
|
|
125
114
|
if (!Array.isArray(commands)) {
|
|
126
115
|
exit(`CLI(commands) must be an array`);
|
|
@@ -176,7 +165,7 @@ async function Register(...commands) {
|
|
|
176
165
|
function help(msg) {
|
|
177
166
|
// build command list
|
|
178
167
|
const content = joinColumns(commands.filter((cmd) => {
|
|
179
|
-
return cmd.name !== "debug" ||
|
|
168
|
+
return cmd.name !== "debug" || process.env.DEBUG === "1";
|
|
180
169
|
}).map((cmd) => ({
|
|
181
170
|
left: ` • ${cmd.name}`,
|
|
182
171
|
right: cmd.description ? `- ${cmd.description}` : "",
|
|
@@ -187,7 +176,7 @@ async function Register(...commands) {
|
|
|
187
176
|
console.log("ERROR:", msg);
|
|
188
177
|
console.log("");
|
|
189
178
|
}
|
|
190
|
-
|
|
179
|
+
process.exit(msg ? 1 : 0);
|
|
191
180
|
}
|
|
192
181
|
// help text builder for a given command
|
|
193
182
|
function helpFor(cmd, flagSpecs, msg) {
|
|
@@ -228,7 +217,7 @@ async function Register(...commands) {
|
|
|
228
217
|
console.log("ERROR:", msg);
|
|
229
218
|
console.log("");
|
|
230
219
|
}
|
|
231
|
-
|
|
220
|
+
process.exit(0);
|
|
232
221
|
}
|
|
233
222
|
// run 1 command from the command tree.
|
|
234
223
|
// this function can be called from other commands
|
|
@@ -314,8 +303,8 @@ async function Register(...commands) {
|
|
|
314
303
|
if (name in flagVals) {
|
|
315
304
|
continue;
|
|
316
305
|
}
|
|
317
|
-
if (env &&
|
|
318
|
-
flagVals[name] = convert(
|
|
306
|
+
if (env && process.env[env]) {
|
|
307
|
+
flagVals[name] = convert(process.env[env], typeof initial);
|
|
319
308
|
continue;
|
|
320
309
|
}
|
|
321
310
|
if (initial !== undefined) {
|
|
@@ -323,7 +312,7 @@ async function Register(...commands) {
|
|
|
323
312
|
}
|
|
324
313
|
}
|
|
325
314
|
// exec targets 'run' function
|
|
326
|
-
const t =
|
|
315
|
+
const t = timer();
|
|
327
316
|
try {
|
|
328
317
|
await match.run({
|
|
329
318
|
flags: flagVals,
|
|
@@ -343,7 +332,7 @@ async function Register(...commands) {
|
|
|
343
332
|
}
|
|
344
333
|
// "root" command
|
|
345
334
|
// process.argv: [node, script, ...args]
|
|
346
|
-
const args =
|
|
335
|
+
const args = process.argv.slice(2);
|
|
347
336
|
// help intercept
|
|
348
337
|
if (args.length === 0 || args[0] === "-h" || args[0] === "--help") {
|
|
349
338
|
help();
|
|
@@ -361,20 +350,20 @@ async function Register(...commands) {
|
|
|
361
350
|
if (eo && typeof eo === "object") {
|
|
362
351
|
// handle command output
|
|
363
352
|
if (typeof eo.code === "number") {
|
|
364
|
-
|
|
353
|
+
process.exit(eo.code);
|
|
365
354
|
}
|
|
366
355
|
// handle js error
|
|
367
356
|
const msg = err ? eo.stack || eo.message : `${err}`;
|
|
368
357
|
if (/exit with (\d+)/.test(msg)) {
|
|
369
|
-
|
|
358
|
+
process.exit(parseInt(RegExp.$1, 10));
|
|
370
359
|
}
|
|
371
360
|
console.log("ERROR: " + msg + "\n");
|
|
372
361
|
}
|
|
373
362
|
// caught error -> exit 1
|
|
374
|
-
|
|
363
|
+
process.exit(1);
|
|
375
364
|
}
|
|
376
365
|
}
|
|
377
|
-
function Command(command) {
|
|
366
|
+
export function Command(command) {
|
|
378
367
|
return { ...command, flagValues: null, input: null };
|
|
379
368
|
}
|
|
380
369
|
// deno-lint-ignore no-constant-condition
|