citty 0.1.5 → 0.2.0
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/LICENSE +0 -15
- package/README.md +13 -42
- package/dist/_chunks/libs/scule.mjs +68 -0
- package/dist/index.d.mts +77 -56
- package/dist/index.mjs +285 -426
- package/package.json +23 -31
- package/dist/index.cjs +0 -471
- package/dist/index.d.cts +0 -80
- package/dist/index.d.ts +0 -80
package/dist/index.mjs
CHANGED
|
@@ -1,459 +1,318 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { n as kebabCase, t as camelCase } from "./_chunks/libs/scule.mjs";
|
|
2
|
+
import { parseArgs as parseArgs$1 } from "node:util";
|
|
3
3
|
|
|
4
|
+
//#region src/_utils.ts
|
|
4
5
|
function toArray(val) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
return val === void 0 ? [] : [val];
|
|
6
|
+
if (Array.isArray(val)) return val;
|
|
7
|
+
return val === void 0 ? [] : [val];
|
|
9
8
|
}
|
|
10
9
|
function formatLineColumns(lines, linePrefix = "") {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
maxLengh[i] = Math.max(maxLengh[i] || 0, element.length);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return lines.map(
|
|
18
|
-
(l) => l.map(
|
|
19
|
-
(c, i) => linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLengh[i])
|
|
20
|
-
).join(" ")
|
|
21
|
-
).join("\n");
|
|
10
|
+
const maxLength = [];
|
|
11
|
+
for (const line of lines) for (const [i, element] of line.entries()) maxLength[i] = Math.max(maxLength[i] || 0, element.length);
|
|
12
|
+
return lines.map((l) => l.map((c, i) => linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLength[i])).join(" ")).join("\n");
|
|
22
13
|
}
|
|
23
14
|
function resolveValue(input) {
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
class CLIError extends Error {
|
|
27
|
-
constructor(message, code) {
|
|
28
|
-
super(message);
|
|
29
|
-
this.code = code;
|
|
30
|
-
this.name = "CLIError";
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const NUMBER_CHAR_RE = /\d/;
|
|
35
|
-
const STR_SPLITTERS = ["-", "_", "/", "."];
|
|
36
|
-
function isUppercase(char = "") {
|
|
37
|
-
if (NUMBER_CHAR_RE.test(char)) {
|
|
38
|
-
return void 0;
|
|
39
|
-
}
|
|
40
|
-
return char.toUpperCase() === char;
|
|
41
|
-
}
|
|
42
|
-
function splitByCase(string_, separators) {
|
|
43
|
-
const splitters = separators ?? STR_SPLITTERS;
|
|
44
|
-
const parts = [];
|
|
45
|
-
if (!string_ || typeof string_ !== "string") {
|
|
46
|
-
return parts;
|
|
47
|
-
}
|
|
48
|
-
let buff = "";
|
|
49
|
-
let previousUpper;
|
|
50
|
-
let previousSplitter;
|
|
51
|
-
for (const char of string_) {
|
|
52
|
-
const isSplitter = splitters.includes(char);
|
|
53
|
-
if (isSplitter === true) {
|
|
54
|
-
parts.push(buff);
|
|
55
|
-
buff = "";
|
|
56
|
-
previousUpper = void 0;
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
const isUpper = isUppercase(char);
|
|
60
|
-
if (previousSplitter === false) {
|
|
61
|
-
if (previousUpper === false && isUpper === true) {
|
|
62
|
-
parts.push(buff);
|
|
63
|
-
buff = char;
|
|
64
|
-
previousUpper = isUpper;
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
if (previousUpper === true && isUpper === false && buff.length > 1) {
|
|
68
|
-
const lastChar = buff[buff.length - 1];
|
|
69
|
-
parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
|
|
70
|
-
buff = lastChar + char;
|
|
71
|
-
previousUpper = isUpper;
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
buff += char;
|
|
76
|
-
previousUpper = isUpper;
|
|
77
|
-
previousSplitter = isSplitter;
|
|
78
|
-
}
|
|
79
|
-
parts.push(buff);
|
|
80
|
-
return parts;
|
|
81
|
-
}
|
|
82
|
-
function upperFirst(string_) {
|
|
83
|
-
return !string_ ? "" : string_[0].toUpperCase() + string_.slice(1);
|
|
84
|
-
}
|
|
85
|
-
function lowerFirst(string_) {
|
|
86
|
-
return !string_ ? "" : string_[0].toLowerCase() + string_.slice(1);
|
|
87
|
-
}
|
|
88
|
-
function pascalCase(string_) {
|
|
89
|
-
return !string_ ? "" : (Array.isArray(string_) ? string_ : splitByCase(string_)).map((p) => upperFirst(p)).join("");
|
|
90
|
-
}
|
|
91
|
-
function camelCase(string_) {
|
|
92
|
-
return lowerFirst(pascalCase(string_));
|
|
93
|
-
}
|
|
94
|
-
function kebabCase(string_, joiner) {
|
|
95
|
-
return !string_ ? "" : (Array.isArray(string_) ? string_ : splitByCase(string_)).map((p) => p.toLowerCase()).join(joiner ?? "-");
|
|
15
|
+
return typeof input === "function" ? input() : input;
|
|
96
16
|
}
|
|
17
|
+
var CLIError = class extends Error {
|
|
18
|
+
code;
|
|
19
|
+
constructor(message, code) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "CLIError";
|
|
22
|
+
this.code = code;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
97
25
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
function toVal(out, key, val, opts) {
|
|
102
|
-
let x;
|
|
103
|
-
const old = out[key];
|
|
104
|
-
const nxt = ~opts.string.indexOf(key) ? val == void 0 || val === true ? "" : String(val) : typeof val === "boolean" ? val : ~opts.boolean.indexOf(key) ? val === "false" ? false : val === "true" || (out._.push((x = +val, x * 0 === 0) ? x : val), !!val) : (x = +val, x * 0 === 0) ? x : val;
|
|
105
|
-
out[key] = old == void 0 ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
|
|
106
|
-
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/_parser.ts
|
|
107
28
|
function parseRawArgs(args = [], opts = {}) {
|
|
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
|
-
name = arg.substring(j, idx);
|
|
183
|
-
val = arg.slice(Math.max(0, ++idx)) || i + 1 === len || ("" + args[i + 1]).charCodeAt(0) === 45 || args[++i];
|
|
184
|
-
arr = j === 2 ? [name] : name;
|
|
185
|
-
for (idx = 0; idx < arr.length; idx++) {
|
|
186
|
-
name = arr[idx];
|
|
187
|
-
if (strict && !~keys.indexOf(name)) {
|
|
188
|
-
return opts.unknown("-".repeat(j) + name);
|
|
189
|
-
}
|
|
190
|
-
toVal(out, name, idx + 1 < arr.length || val, opts);
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
if (defaults) {
|
|
195
|
-
for (k in opts.default) {
|
|
196
|
-
if (out[k] === void 0) {
|
|
197
|
-
out[k] = opts.default[k];
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
if (alibi) {
|
|
202
|
-
for (k in out) {
|
|
203
|
-
arr = opts.alias[k] || [];
|
|
204
|
-
while (arr.length > 0) {
|
|
205
|
-
out[arr.shift()] = out[k];
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
return out;
|
|
29
|
+
const booleans = new Set(opts.boolean || []);
|
|
30
|
+
const strings = new Set(opts.string || []);
|
|
31
|
+
const aliasMap = opts.alias || {};
|
|
32
|
+
const defaults = opts.default || {};
|
|
33
|
+
const aliasToMain = /* @__PURE__ */ new Map();
|
|
34
|
+
const mainToAliases = /* @__PURE__ */ new Map();
|
|
35
|
+
for (const [key, value] of Object.entries(aliasMap)) {
|
|
36
|
+
const targets = value;
|
|
37
|
+
for (const target of targets) {
|
|
38
|
+
aliasToMain.set(key, target);
|
|
39
|
+
if (!mainToAliases.has(target)) mainToAliases.set(target, []);
|
|
40
|
+
mainToAliases.get(target).push(key);
|
|
41
|
+
aliasToMain.set(target, key);
|
|
42
|
+
if (!mainToAliases.has(key)) mainToAliases.set(key, []);
|
|
43
|
+
mainToAliases.get(key).push(target);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const options = {};
|
|
47
|
+
function getType(name) {
|
|
48
|
+
if (booleans.has(name)) return "boolean";
|
|
49
|
+
const aliases = mainToAliases.get(name) || [];
|
|
50
|
+
for (const alias of aliases) if (booleans.has(alias)) return "boolean";
|
|
51
|
+
return "string";
|
|
52
|
+
}
|
|
53
|
+
const allOptions = new Set([
|
|
54
|
+
...booleans,
|
|
55
|
+
...strings,
|
|
56
|
+
...Object.keys(aliasMap),
|
|
57
|
+
...Object.values(aliasMap).flat(),
|
|
58
|
+
...Object.keys(defaults)
|
|
59
|
+
]);
|
|
60
|
+
for (const name of allOptions) if (!options[name]) options[name] = {
|
|
61
|
+
type: getType(name),
|
|
62
|
+
default: defaults[name]
|
|
63
|
+
};
|
|
64
|
+
for (const [alias, main] of aliasToMain.entries()) if (alias.length === 1 && options[main] && !options[main].short) options[main].short = alias;
|
|
65
|
+
const processedArgs = [];
|
|
66
|
+
const negatedFlags = {};
|
|
67
|
+
for (let i = 0; i < args.length; i++) {
|
|
68
|
+
const arg = args[i];
|
|
69
|
+
if (arg === "--") {
|
|
70
|
+
processedArgs.push(...args.slice(i));
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
if (arg.startsWith("--no-")) {
|
|
74
|
+
const flagName = arg.slice(5);
|
|
75
|
+
negatedFlags[flagName] = true;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
processedArgs.push(arg);
|
|
79
|
+
}
|
|
80
|
+
let parsed;
|
|
81
|
+
try {
|
|
82
|
+
parsed = parseArgs$1({
|
|
83
|
+
args: processedArgs,
|
|
84
|
+
options: Object.keys(options).length > 0 ? options : void 0,
|
|
85
|
+
allowPositionals: true,
|
|
86
|
+
strict: false
|
|
87
|
+
});
|
|
88
|
+
} catch {
|
|
89
|
+
parsed = {
|
|
90
|
+
values: {},
|
|
91
|
+
positionals: processedArgs
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const out = { _: [] };
|
|
95
|
+
out._ = parsed.positionals;
|
|
96
|
+
for (const [key, value] of Object.entries(parsed.values)) out[key] = value;
|
|
97
|
+
for (const [name] of Object.entries(negatedFlags)) out[name] = false;
|
|
98
|
+
for (const [alias, main] of aliasToMain.entries()) {
|
|
99
|
+
if (out[alias] !== void 0 && out[main] === void 0) out[main] = out[alias];
|
|
100
|
+
if (out[main] !== void 0 && out[alias] === void 0) out[alias] = out[main];
|
|
101
|
+
}
|
|
102
|
+
return out;
|
|
210
103
|
}
|
|
211
104
|
|
|
105
|
+
//#endregion
|
|
106
|
+
//#region src/_color.ts
|
|
107
|
+
const noColor = /* @__PURE__ */ (() => {
|
|
108
|
+
const env = globalThis.process?.env ?? {};
|
|
109
|
+
return env.NO_COLOR === "1" || env.TERM === "dumb" || env.TEST || env.CI;
|
|
110
|
+
})();
|
|
111
|
+
const _c = (c, r = 39) => (t) => noColor ? t : `\u001B[${c}m${t}\u001B[${r}m`;
|
|
112
|
+
const bold = /* @__PURE__ */ _c(1, 22);
|
|
113
|
+
const cyan = /* @__PURE__ */ _c(36);
|
|
114
|
+
const gray = /* @__PURE__ */ _c(90);
|
|
115
|
+
const underline = /* @__PURE__ */ _c(4, 24);
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/args.ts
|
|
212
119
|
function parseArgs(rawArgs, argsDef) {
|
|
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
|
-
`Missing required positional argument: ${arg.name.toUpperCase()}`,
|
|
252
|
-
"EARG"
|
|
253
|
-
);
|
|
254
|
-
} else {
|
|
255
|
-
parsedArgsProxy[arg.name] = arg.default;
|
|
256
|
-
}
|
|
257
|
-
} else if (arg.required && parsedArgsProxy[arg.name] === void 0) {
|
|
258
|
-
throw new CLIError(`Missing required argument: --${arg.name}`, "EARG");
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
return parsedArgsProxy;
|
|
120
|
+
const parseOptions = {
|
|
121
|
+
boolean: [],
|
|
122
|
+
string: [],
|
|
123
|
+
alias: {},
|
|
124
|
+
default: {}
|
|
125
|
+
};
|
|
126
|
+
const args = resolveArgs(argsDef);
|
|
127
|
+
for (const arg of args) {
|
|
128
|
+
if (arg.type === "positional") continue;
|
|
129
|
+
if (arg.type === "string" || arg.type === "enum") parseOptions.string.push(arg.name);
|
|
130
|
+
else if (arg.type === "boolean") parseOptions.boolean.push(arg.name);
|
|
131
|
+
if (arg.default !== void 0) parseOptions.default[arg.name] = arg.default;
|
|
132
|
+
if (arg.alias) parseOptions.alias[arg.name] = arg.alias;
|
|
133
|
+
const camelName = camelCase(arg.name);
|
|
134
|
+
const kebabName = kebabCase(arg.name);
|
|
135
|
+
if (camelName !== arg.name || kebabName !== arg.name) {
|
|
136
|
+
const existingAliases = toArray(parseOptions.alias[arg.name] || []);
|
|
137
|
+
if (camelName !== arg.name && !existingAliases.includes(camelName)) existingAliases.push(camelName);
|
|
138
|
+
if (kebabName !== arg.name && !existingAliases.includes(kebabName)) existingAliases.push(kebabName);
|
|
139
|
+
if (existingAliases.length > 0) parseOptions.alias[arg.name] = existingAliases;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const parsed = parseRawArgs(rawArgs, parseOptions);
|
|
143
|
+
const [ ...positionalArguments] = parsed._;
|
|
144
|
+
const parsedArgsProxy = new Proxy(parsed, { get(target, prop) {
|
|
145
|
+
return target[prop] ?? target[camelCase(prop)] ?? target[kebabCase(prop)];
|
|
146
|
+
} });
|
|
147
|
+
for (const [, arg] of args.entries()) if (arg.type === "positional") {
|
|
148
|
+
const nextPositionalArgument = positionalArguments.shift();
|
|
149
|
+
if (nextPositionalArgument !== void 0) parsedArgsProxy[arg.name] = nextPositionalArgument;
|
|
150
|
+
else if (arg.default === void 0 && arg.required !== false) throw new CLIError(`Missing required positional argument: ${arg.name.toUpperCase()}`, "EARG");
|
|
151
|
+
else parsedArgsProxy[arg.name] = arg.default;
|
|
152
|
+
} else if (arg.type === "enum") {
|
|
153
|
+
const argument = parsedArgsProxy[arg.name];
|
|
154
|
+
const options = arg.options || [];
|
|
155
|
+
if (argument !== void 0 && options.length > 0 && !options.includes(argument)) throw new CLIError(`Invalid value for argument: ${cyan(`--${arg.name}`)} (${cyan(argument)}). Expected one of: ${options.map((o) => cyan(o)).join(", ")}.`, "EARG");
|
|
156
|
+
} else if (arg.required && parsedArgsProxy[arg.name] === void 0) throw new CLIError(`Missing required argument: --${arg.name}`, "EARG");
|
|
157
|
+
return parsedArgsProxy;
|
|
262
158
|
}
|
|
263
159
|
function resolveArgs(argsDef) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
return args;
|
|
160
|
+
const args = [];
|
|
161
|
+
for (const [name, argDef] of Object.entries(argsDef || {})) args.push({
|
|
162
|
+
...argDef,
|
|
163
|
+
name,
|
|
164
|
+
alias: toArray(argDef.alias)
|
|
165
|
+
});
|
|
166
|
+
return args;
|
|
273
167
|
}
|
|
274
168
|
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/command.ts
|
|
275
171
|
function defineCommand(def) {
|
|
276
|
-
|
|
172
|
+
return def;
|
|
277
173
|
}
|
|
278
174
|
async function runCommand(cmd, opts) {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const subCommand = await resolveValue(subCommands[subCommandName]);
|
|
306
|
-
if (subCommand) {
|
|
307
|
-
await runCommand(subCommand, {
|
|
308
|
-
rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1)
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
} else if (!cmd.run) {
|
|
312
|
-
throw new CLIError(`No command specified.`, "E_NO_COMMAND");
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
if (typeof cmd.run === "function") {
|
|
316
|
-
result = await cmd.run(context);
|
|
317
|
-
}
|
|
318
|
-
} finally {
|
|
319
|
-
if (typeof cmd.cleanup === "function") {
|
|
320
|
-
await cmd.cleanup(context);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
return { result };
|
|
175
|
+
const cmdArgs = await resolveValue(cmd.args || {});
|
|
176
|
+
const parsedArgs = parseArgs(opts.rawArgs, cmdArgs);
|
|
177
|
+
const context = {
|
|
178
|
+
rawArgs: opts.rawArgs,
|
|
179
|
+
args: parsedArgs,
|
|
180
|
+
data: opts.data,
|
|
181
|
+
cmd
|
|
182
|
+
};
|
|
183
|
+
if (typeof cmd.setup === "function") await cmd.setup(context);
|
|
184
|
+
let result;
|
|
185
|
+
try {
|
|
186
|
+
const subCommands = await resolveValue(cmd.subCommands);
|
|
187
|
+
if (subCommands && Object.keys(subCommands).length > 0) {
|
|
188
|
+
const subCommandArgIndex = opts.rawArgs.findIndex((arg) => !arg.startsWith("-"));
|
|
189
|
+
const subCommandName = opts.rawArgs[subCommandArgIndex];
|
|
190
|
+
if (subCommandName) {
|
|
191
|
+
if (!subCommands[subCommandName]) throw new CLIError(`Unknown command ${cyan(subCommandName)}`, "E_UNKNOWN_COMMAND");
|
|
192
|
+
const subCommand = await resolveValue(subCommands[subCommandName]);
|
|
193
|
+
if (subCommand) await runCommand(subCommand, { rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1) });
|
|
194
|
+
} else if (!cmd.run) throw new CLIError(`No command specified.`, "E_NO_COMMAND");
|
|
195
|
+
}
|
|
196
|
+
if (typeof cmd.run === "function") result = await cmd.run(context);
|
|
197
|
+
} finally {
|
|
198
|
+
if (typeof cmd.cleanup === "function") await cmd.cleanup(context);
|
|
199
|
+
}
|
|
200
|
+
return { result };
|
|
324
201
|
}
|
|
325
202
|
async function resolveSubCommand(cmd, rawArgs, parent) {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
rawArgs.slice(subCommandArgIndex + 1),
|
|
335
|
-
cmd
|
|
336
|
-
);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return [cmd, parent];
|
|
203
|
+
const subCommands = await resolveValue(cmd.subCommands);
|
|
204
|
+
if (subCommands && Object.keys(subCommands).length > 0) {
|
|
205
|
+
const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
|
|
206
|
+
const subCommandName = rawArgs[subCommandArgIndex];
|
|
207
|
+
const subCommand = await resolveValue(subCommands[subCommandName]);
|
|
208
|
+
if (subCommand) return resolveSubCommand(subCommand, rawArgs.slice(subCommandArgIndex + 1), cmd);
|
|
209
|
+
}
|
|
210
|
+
return [cmd, parent];
|
|
340
211
|
}
|
|
341
212
|
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region src/usage.ts
|
|
342
215
|
async function showUsage(cmd, parent) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
216
|
+
try {
|
|
217
|
+
console.log(await renderUsage(cmd, parent) + "\n");
|
|
218
|
+
} catch (error) {
|
|
219
|
+
console.error(error);
|
|
220
|
+
}
|
|
348
221
|
}
|
|
222
|
+
const negativePrefixRe = /^no[-A-Z]/;
|
|
349
223
|
async function renderUsage(cmd, parent) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
}
|
|
416
|
-
if (commandsLines.length > 0) {
|
|
417
|
-
usageLines.push(colors.underline(colors.bold("COMMANDS")), "");
|
|
418
|
-
usageLines.push(formatLineColumns(commandsLines, " "));
|
|
419
|
-
usageLines.push(
|
|
420
|
-
"",
|
|
421
|
-
`Use \`${commandName} <command> --help\` for more information about a command.`
|
|
422
|
-
);
|
|
423
|
-
}
|
|
424
|
-
return usageLines.filter((l) => typeof l === "string").join("\n");
|
|
224
|
+
const cmdMeta = await resolveValue(cmd.meta || {});
|
|
225
|
+
const cmdArgs = resolveArgs(await resolveValue(cmd.args || {}));
|
|
226
|
+
const parentMeta = await resolveValue(parent?.meta || {});
|
|
227
|
+
const commandName = `${parentMeta.name ? `${parentMeta.name} ` : ""}` + (cmdMeta.name || process.argv[1]);
|
|
228
|
+
const argLines = [];
|
|
229
|
+
const posLines = [];
|
|
230
|
+
const commandsLines = [];
|
|
231
|
+
const usageLine = [];
|
|
232
|
+
for (const arg of cmdArgs) if (arg.type === "positional") {
|
|
233
|
+
const name = arg.name.toUpperCase();
|
|
234
|
+
const isRequired = arg.required !== false && arg.default === void 0;
|
|
235
|
+
const defaultHint = arg.default ? `="${arg.default}"` : "";
|
|
236
|
+
posLines.push([
|
|
237
|
+
cyan(name + defaultHint),
|
|
238
|
+
arg.description || "",
|
|
239
|
+
arg.valueHint ? `<${arg.valueHint}>` : ""
|
|
240
|
+
]);
|
|
241
|
+
usageLine.push(isRequired ? `<${name}>` : `[${name}]`);
|
|
242
|
+
} else {
|
|
243
|
+
const isRequired = arg.required === true && arg.default === void 0;
|
|
244
|
+
const argStr = [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(", ") + (arg.type === "string" && (arg.valueHint || arg.default) ? `=${arg.valueHint ? `<${arg.valueHint}>` : `"${arg.default || ""}"`}` : "") + (arg.type === "enum" && arg.options ? `=<${arg.options.join("|")}>` : "");
|
|
245
|
+
argLines.push([cyan(argStr + (isRequired ? " (required)" : "")), arg.description || ""]);
|
|
246
|
+
/**
|
|
247
|
+
* print negative boolean arg variant usage when
|
|
248
|
+
* - enabled by default or has `negativeDescription`
|
|
249
|
+
* - not prefixed with `no-` or `no[A-Z]`
|
|
250
|
+
*/
|
|
251
|
+
if (arg.type === "boolean" && (arg.default === true || arg.negativeDescription) && !negativePrefixRe.test(arg.name)) {
|
|
252
|
+
const negativeArgStr = [...(arg.alias || []).map((a) => `--no-${a}`), `--no-${arg.name}`].join(", ");
|
|
253
|
+
argLines.push([cyan(negativeArgStr + (isRequired ? " (required)" : "")), arg.negativeDescription || ""]);
|
|
254
|
+
}
|
|
255
|
+
if (isRequired) usageLine.push(argStr);
|
|
256
|
+
}
|
|
257
|
+
if (cmd.subCommands) {
|
|
258
|
+
const commandNames = [];
|
|
259
|
+
const subCommands = await resolveValue(cmd.subCommands);
|
|
260
|
+
for (const [name, sub] of Object.entries(subCommands)) {
|
|
261
|
+
const meta = await resolveValue((await resolveValue(sub))?.meta);
|
|
262
|
+
if (meta?.hidden) continue;
|
|
263
|
+
commandsLines.push([cyan(name), meta?.description || ""]);
|
|
264
|
+
commandNames.push(name);
|
|
265
|
+
}
|
|
266
|
+
usageLine.push(commandNames.join("|"));
|
|
267
|
+
}
|
|
268
|
+
const usageLines = [];
|
|
269
|
+
const version = cmdMeta.version || parentMeta.version;
|
|
270
|
+
usageLines.push(gray(`${cmdMeta.description} (${commandName + (version ? ` v${version}` : "")})`), "");
|
|
271
|
+
const hasOptions = argLines.length > 0 || posLines.length > 0;
|
|
272
|
+
usageLines.push(`${underline(bold("USAGE"))} ${cyan(`${commandName}${hasOptions ? " [OPTIONS]" : ""} ${usageLine.join(" ")}`)}`, "");
|
|
273
|
+
if (posLines.length > 0) {
|
|
274
|
+
usageLines.push(underline(bold("ARGUMENTS")), "");
|
|
275
|
+
usageLines.push(formatLineColumns(posLines, " "));
|
|
276
|
+
usageLines.push("");
|
|
277
|
+
}
|
|
278
|
+
if (argLines.length > 0) {
|
|
279
|
+
usageLines.push(underline(bold("OPTIONS")), "");
|
|
280
|
+
usageLines.push(formatLineColumns(argLines, " "));
|
|
281
|
+
usageLines.push("");
|
|
282
|
+
}
|
|
283
|
+
if (commandsLines.length > 0) {
|
|
284
|
+
usageLines.push(underline(bold("COMMANDS")), "");
|
|
285
|
+
usageLines.push(formatLineColumns(commandsLines, " "));
|
|
286
|
+
usageLines.push("", `Use ${cyan(`${commandName} <command> --help`)} for more information about a command.`);
|
|
287
|
+
}
|
|
288
|
+
return usageLines.filter((l) => typeof l === "string").join("\n");
|
|
425
289
|
}
|
|
426
290
|
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region src/main.ts
|
|
427
293
|
async function runMain(cmd, opts = {}) {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
consola.error(error, "\n");
|
|
447
|
-
}
|
|
448
|
-
if (isCLIError) {
|
|
449
|
-
await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
450
|
-
}
|
|
451
|
-
consola.error(error.message);
|
|
452
|
-
process.exit(1);
|
|
453
|
-
}
|
|
294
|
+
const rawArgs = opts.rawArgs || process.argv.slice(2);
|
|
295
|
+
const showUsage$1 = opts.showUsage || showUsage;
|
|
296
|
+
try {
|
|
297
|
+
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
|
|
298
|
+
await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
299
|
+
process.exit(0);
|
|
300
|
+
} else if (rawArgs.length === 1 && rawArgs[0] === "--version") {
|
|
301
|
+
const meta = typeof cmd.meta === "function" ? await cmd.meta() : await cmd.meta;
|
|
302
|
+
if (!meta?.version) throw new CLIError("No version specified", "E_NO_VERSION");
|
|
303
|
+
console.log(meta.version);
|
|
304
|
+
} else await runCommand(cmd, { rawArgs });
|
|
305
|
+
} catch (error) {
|
|
306
|
+
if (error instanceof CLIError) {
|
|
307
|
+
await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
308
|
+
console.error(error.message);
|
|
309
|
+
} else console.error(error, "\n");
|
|
310
|
+
process.exit(1);
|
|
311
|
+
}
|
|
454
312
|
}
|
|
455
313
|
function createMain(cmd) {
|
|
456
|
-
|
|
314
|
+
return (opts = {}) => runMain(cmd, opts);
|
|
457
315
|
}
|
|
458
316
|
|
|
459
|
-
|
|
317
|
+
//#endregion
|
|
318
|
+
export { createMain, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
|