@bomb.sh/tab 0.0.1-pre.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/README.2.md +108 -0
- package/README.md +246 -0
- package/dist/bin/cli.d.ts +1 -0
- package/dist/bin/cli.js +487 -0
- package/dist/cac-DEQnX2V4.js +89 -0
- package/dist/citty-BJEnxM_0.js +525 -0
- package/dist/commander-gLSiUyHX.js +103 -0
- package/dist/consola.36c0034f-CftISWio.js +832 -0
- package/dist/dist-QXQoOtHE.js +487 -0
- package/dist/examples/demo.cac.d.ts +1 -0
- package/dist/examples/demo.cac.js +63 -0
- package/dist/examples/demo.citty.d.ts +1 -0
- package/dist/examples/demo.citty.js +173 -0
- package/dist/examples/demo.commander.d.ts +1 -0
- package/dist/examples/demo.commander.js +3035 -0
- package/dist/examples/demo.t.d.ts +1 -0
- package/dist/examples/demo.t.js +78 -0
- package/dist/prompt-BeRRtcd8.js +758 -0
- package/dist/shared-BGWjvggn.js +12 -0
- package/dist/shared.d-B8B93rZW.d.ts +11 -0
- package/dist/src/cac.d.ts +9 -0
- package/dist/src/cac.js +5 -0
- package/dist/src/citty.d.ts +9 -0
- package/dist/src/citty.js +6 -0
- package/dist/src/commander.d.ts +8 -0
- package/dist/src/commander.js +5 -0
- package/dist/src/t.d.ts +3 -0
- package/dist/src/t.js +3 -0
- package/dist/t-Zhhzaib1.js +1054 -0
- package/dist/t.d-keC1Qwmr.d.ts +68 -0
- package/package.json +74 -0
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { generate as generate$3, generate$1 as generate$2, generate$2 as generate$1, generate$3 as generate, t_default } from "./t-Zhhzaib1.js";
|
|
2
|
+
import { assertDoubleDashes } from "./shared-BGWjvggn.js";
|
|
3
|
+
import { colors, consola } from "./consola.36c0034f-CftISWio.js";
|
|
4
|
+
|
|
5
|
+
//#region node_modules/.pnpm/citty@0.1.6/node_modules/citty/dist/index.mjs
|
|
6
|
+
function toArray(val) {
|
|
7
|
+
if (Array.isArray(val)) return val;
|
|
8
|
+
return val === void 0 ? [] : [val];
|
|
9
|
+
}
|
|
10
|
+
function formatLineColumns(lines, linePrefix = "") {
|
|
11
|
+
const maxLengh = [];
|
|
12
|
+
for (const line of lines) for (const [i, element] of line.entries()) maxLengh[i] = Math.max(maxLengh[i] || 0, element.length);
|
|
13
|
+
return lines.map((l) => l.map((c, i) => linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLengh[i])).join(" ")).join("\n");
|
|
14
|
+
}
|
|
15
|
+
function resolveValue(input) {
|
|
16
|
+
return typeof input === "function" ? input() : input;
|
|
17
|
+
}
|
|
18
|
+
var CLIError = class extends Error {
|
|
19
|
+
constructor(message, code) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.name = "CLIError";
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const NUMBER_CHAR_RE = /\d/;
|
|
26
|
+
const STR_SPLITTERS = [
|
|
27
|
+
"-",
|
|
28
|
+
"_",
|
|
29
|
+
"/",
|
|
30
|
+
"."
|
|
31
|
+
];
|
|
32
|
+
function isUppercase(char = "") {
|
|
33
|
+
if (NUMBER_CHAR_RE.test(char)) return void 0;
|
|
34
|
+
return char !== char.toLowerCase();
|
|
35
|
+
}
|
|
36
|
+
function splitByCase(str, separators) {
|
|
37
|
+
const splitters = separators ?? STR_SPLITTERS;
|
|
38
|
+
const parts = [];
|
|
39
|
+
if (!str || typeof str !== "string") return parts;
|
|
40
|
+
let buff = "";
|
|
41
|
+
let previousUpper;
|
|
42
|
+
let previousSplitter;
|
|
43
|
+
for (const char of str) {
|
|
44
|
+
const isSplitter = splitters.includes(char);
|
|
45
|
+
if (isSplitter === true) {
|
|
46
|
+
parts.push(buff);
|
|
47
|
+
buff = "";
|
|
48
|
+
previousUpper = void 0;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const isUpper = isUppercase(char);
|
|
52
|
+
if (previousSplitter === false) {
|
|
53
|
+
if (previousUpper === false && isUpper === true) {
|
|
54
|
+
parts.push(buff);
|
|
55
|
+
buff = char;
|
|
56
|
+
previousUpper = isUpper;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (previousUpper === true && isUpper === false && buff.length > 1) {
|
|
60
|
+
const lastChar = buff.at(-1);
|
|
61
|
+
parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
|
|
62
|
+
buff = lastChar + char;
|
|
63
|
+
previousUpper = isUpper;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
buff += char;
|
|
68
|
+
previousUpper = isUpper;
|
|
69
|
+
previousSplitter = isSplitter;
|
|
70
|
+
}
|
|
71
|
+
parts.push(buff);
|
|
72
|
+
return parts;
|
|
73
|
+
}
|
|
74
|
+
function upperFirst(str) {
|
|
75
|
+
return str ? str[0].toUpperCase() + str.slice(1) : "";
|
|
76
|
+
}
|
|
77
|
+
function lowerFirst(str) {
|
|
78
|
+
return str ? str[0].toLowerCase() + str.slice(1) : "";
|
|
79
|
+
}
|
|
80
|
+
function pascalCase(str, opts) {
|
|
81
|
+
return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("") : "";
|
|
82
|
+
}
|
|
83
|
+
function camelCase(str, opts) {
|
|
84
|
+
return lowerFirst(pascalCase(str || "", opts));
|
|
85
|
+
}
|
|
86
|
+
function kebabCase(str, joiner) {
|
|
87
|
+
return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => p.toLowerCase()).join(joiner ?? "-") : "";
|
|
88
|
+
}
|
|
89
|
+
function toArr(any) {
|
|
90
|
+
return any == void 0 ? [] : Array.isArray(any) ? any : [any];
|
|
91
|
+
}
|
|
92
|
+
function toVal(out, key, val, opts) {
|
|
93
|
+
let x$1;
|
|
94
|
+
const old = out[key];
|
|
95
|
+
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$1 = +val, x$1 * 0 === 0) ? x$1 : val), !!val) : (x$1 = +val, x$1 * 0 === 0) ? x$1 : val;
|
|
96
|
+
out[key] = old == void 0 ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
|
|
97
|
+
}
|
|
98
|
+
function parseRawArgs(args = [], opts = {}) {
|
|
99
|
+
let k;
|
|
100
|
+
let arr;
|
|
101
|
+
let arg;
|
|
102
|
+
let name;
|
|
103
|
+
let val;
|
|
104
|
+
const out = { _: [] };
|
|
105
|
+
let i = 0;
|
|
106
|
+
let j = 0;
|
|
107
|
+
let idx = 0;
|
|
108
|
+
const len = args.length;
|
|
109
|
+
const alibi = opts.alias !== void 0;
|
|
110
|
+
const strict = opts.unknown !== void 0;
|
|
111
|
+
const defaults = opts.default !== void 0;
|
|
112
|
+
opts.alias = opts.alias || {};
|
|
113
|
+
opts.string = toArr(opts.string);
|
|
114
|
+
opts.boolean = toArr(opts.boolean);
|
|
115
|
+
if (alibi) for (k in opts.alias) {
|
|
116
|
+
arr = opts.alias[k] = toArr(opts.alias[k]);
|
|
117
|
+
for (i = 0; i < arr.length; i++) (opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
|
|
118
|
+
}
|
|
119
|
+
for (i = opts.boolean.length; i-- > 0;) {
|
|
120
|
+
arr = opts.alias[opts.boolean[i]] || [];
|
|
121
|
+
for (j = arr.length; j-- > 0;) opts.boolean.push(arr[j]);
|
|
122
|
+
}
|
|
123
|
+
for (i = opts.string.length; i-- > 0;) {
|
|
124
|
+
arr = opts.alias[opts.string[i]] || [];
|
|
125
|
+
for (j = arr.length; j-- > 0;) opts.string.push(arr[j]);
|
|
126
|
+
}
|
|
127
|
+
if (defaults) for (k in opts.default) {
|
|
128
|
+
name = typeof opts.default[k];
|
|
129
|
+
arr = opts.alias[k] = opts.alias[k] || [];
|
|
130
|
+
if (opts[name] !== void 0) {
|
|
131
|
+
opts[name].push(k);
|
|
132
|
+
for (i = 0; i < arr.length; i++) opts[name].push(arr[i]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const keys = strict ? Object.keys(opts.alias) : [];
|
|
136
|
+
for (i = 0; i < len; i++) {
|
|
137
|
+
arg = args[i];
|
|
138
|
+
if (arg === "--") {
|
|
139
|
+
out._ = out._.concat(args.slice(++i));
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
for (j = 0; j < arg.length; j++) if (arg.charCodeAt(j) !== 45) break;
|
|
143
|
+
if (j === 0) out._.push(arg);
|
|
144
|
+
else if (arg.substring(j, j + 3) === "no-") {
|
|
145
|
+
name = arg.slice(Math.max(0, j + 3));
|
|
146
|
+
if (strict && !~keys.indexOf(name)) return opts.unknown(arg);
|
|
147
|
+
out[name] = false;
|
|
148
|
+
} else {
|
|
149
|
+
for (idx = j + 1; idx < arg.length; idx++) if (arg.charCodeAt(idx) === 61) break;
|
|
150
|
+
name = arg.substring(j, idx);
|
|
151
|
+
val = arg.slice(Math.max(0, ++idx)) || i + 1 === len || ("" + args[i + 1]).charCodeAt(0) === 45 || args[++i];
|
|
152
|
+
arr = j === 2 ? [name] : name;
|
|
153
|
+
for (idx = 0; idx < arr.length; idx++) {
|
|
154
|
+
name = arr[idx];
|
|
155
|
+
if (strict && !~keys.indexOf(name)) return opts.unknown("-".repeat(j) + name);
|
|
156
|
+
toVal(out, name, idx + 1 < arr.length || val, opts);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (defaults) {
|
|
161
|
+
for (k in opts.default) if (out[k] === void 0) out[k] = opts.default[k];
|
|
162
|
+
}
|
|
163
|
+
if (alibi) for (k in out) {
|
|
164
|
+
arr = opts.alias[k] || [];
|
|
165
|
+
while (arr.length > 0) out[arr.shift()] = out[k];
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
function parseArgs(rawArgs, argsDef) {
|
|
170
|
+
const parseOptions = {
|
|
171
|
+
boolean: [],
|
|
172
|
+
string: [],
|
|
173
|
+
mixed: [],
|
|
174
|
+
alias: {},
|
|
175
|
+
default: {}
|
|
176
|
+
};
|
|
177
|
+
const args = resolveArgs(argsDef);
|
|
178
|
+
for (const arg of args) {
|
|
179
|
+
if (arg.type === "positional") continue;
|
|
180
|
+
if (arg.type === "string") parseOptions.string.push(arg.name);
|
|
181
|
+
else if (arg.type === "boolean") parseOptions.boolean.push(arg.name);
|
|
182
|
+
if (arg.default !== void 0) parseOptions.default[arg.name] = arg.default;
|
|
183
|
+
if (arg.alias) parseOptions.alias[arg.name] = arg.alias;
|
|
184
|
+
}
|
|
185
|
+
const parsed = parseRawArgs(rawArgs, parseOptions);
|
|
186
|
+
const [ ...positionalArguments] = parsed._;
|
|
187
|
+
const parsedArgsProxy = new Proxy(parsed, { get(target, prop) {
|
|
188
|
+
return target[prop] ?? target[camelCase(prop)] ?? target[kebabCase(prop)];
|
|
189
|
+
} });
|
|
190
|
+
for (const [, arg] of args.entries()) if (arg.type === "positional") {
|
|
191
|
+
const nextPositionalArgument = positionalArguments.shift();
|
|
192
|
+
if (nextPositionalArgument !== void 0) parsedArgsProxy[arg.name] = nextPositionalArgument;
|
|
193
|
+
else if (arg.default === void 0 && arg.required !== false) throw new CLIError(`Missing required positional argument: ${arg.name.toUpperCase()}`, "EARG");
|
|
194
|
+
else parsedArgsProxy[arg.name] = arg.default;
|
|
195
|
+
} else if (arg.required && parsedArgsProxy[arg.name] === void 0) throw new CLIError(`Missing required argument: --${arg.name}`, "EARG");
|
|
196
|
+
return parsedArgsProxy;
|
|
197
|
+
}
|
|
198
|
+
function resolveArgs(argsDef) {
|
|
199
|
+
const args = [];
|
|
200
|
+
for (const [name, argDef] of Object.entries(argsDef || {})) args.push({
|
|
201
|
+
...argDef,
|
|
202
|
+
name,
|
|
203
|
+
alias: toArray(argDef.alias)
|
|
204
|
+
});
|
|
205
|
+
return args;
|
|
206
|
+
}
|
|
207
|
+
function defineCommand(def) {
|
|
208
|
+
return def;
|
|
209
|
+
}
|
|
210
|
+
async function runCommand(cmd, opts) {
|
|
211
|
+
const cmdArgs = await resolveValue(cmd.args || {});
|
|
212
|
+
const parsedArgs = parseArgs(opts.rawArgs, cmdArgs);
|
|
213
|
+
const context = {
|
|
214
|
+
rawArgs: opts.rawArgs,
|
|
215
|
+
args: parsedArgs,
|
|
216
|
+
data: opts.data,
|
|
217
|
+
cmd
|
|
218
|
+
};
|
|
219
|
+
if (typeof cmd.setup === "function") await cmd.setup(context);
|
|
220
|
+
let result;
|
|
221
|
+
try {
|
|
222
|
+
const subCommands = await resolveValue(cmd.subCommands);
|
|
223
|
+
if (subCommands && Object.keys(subCommands).length > 0) {
|
|
224
|
+
const subCommandArgIndex = opts.rawArgs.findIndex((arg) => !arg.startsWith("-"));
|
|
225
|
+
const subCommandName = opts.rawArgs[subCommandArgIndex];
|
|
226
|
+
if (subCommandName) {
|
|
227
|
+
if (!subCommands[subCommandName]) throw new CLIError(`Unknown command \`${subCommandName}\``, "E_UNKNOWN_COMMAND");
|
|
228
|
+
const subCommand = await resolveValue(subCommands[subCommandName]);
|
|
229
|
+
if (subCommand) await runCommand(subCommand, { rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1) });
|
|
230
|
+
} else if (!cmd.run) throw new CLIError(`No command specified.`, "E_NO_COMMAND");
|
|
231
|
+
}
|
|
232
|
+
if (typeof cmd.run === "function") result = await cmd.run(context);
|
|
233
|
+
} finally {
|
|
234
|
+
if (typeof cmd.cleanup === "function") await cmd.cleanup(context);
|
|
235
|
+
}
|
|
236
|
+
return { result };
|
|
237
|
+
}
|
|
238
|
+
async function resolveSubCommand(cmd, rawArgs, parent) {
|
|
239
|
+
const subCommands = await resolveValue(cmd.subCommands);
|
|
240
|
+
if (subCommands && Object.keys(subCommands).length > 0) {
|
|
241
|
+
const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
|
|
242
|
+
const subCommandName = rawArgs[subCommandArgIndex];
|
|
243
|
+
const subCommand = await resolveValue(subCommands[subCommandName]);
|
|
244
|
+
if (subCommand) return resolveSubCommand(subCommand, rawArgs.slice(subCommandArgIndex + 1), cmd);
|
|
245
|
+
}
|
|
246
|
+
return [cmd, parent];
|
|
247
|
+
}
|
|
248
|
+
async function showUsage(cmd, parent) {
|
|
249
|
+
try {
|
|
250
|
+
consola.log(await renderUsage(cmd, parent) + "\n");
|
|
251
|
+
} catch (error) {
|
|
252
|
+
consola.error(error);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
async function renderUsage(cmd, parent) {
|
|
256
|
+
const cmdMeta = await resolveValue(cmd.meta || {});
|
|
257
|
+
const cmdArgs = resolveArgs(await resolveValue(cmd.args || {}));
|
|
258
|
+
const parentMeta = await resolveValue(parent?.meta || {});
|
|
259
|
+
const commandName = `${parentMeta.name ? `${parentMeta.name} ` : ""}` + (cmdMeta.name || process.argv[1]);
|
|
260
|
+
const argLines = [];
|
|
261
|
+
const posLines = [];
|
|
262
|
+
const commandsLines = [];
|
|
263
|
+
const usageLine = [];
|
|
264
|
+
for (const arg of cmdArgs) if (arg.type === "positional") {
|
|
265
|
+
const name = arg.name.toUpperCase();
|
|
266
|
+
const isRequired = arg.required !== false && arg.default === void 0;
|
|
267
|
+
const defaultHint = arg.default ? `="${arg.default}"` : "";
|
|
268
|
+
posLines.push([
|
|
269
|
+
"`" + name + defaultHint + "`",
|
|
270
|
+
arg.description || "",
|
|
271
|
+
arg.valueHint ? `<${arg.valueHint}>` : ""
|
|
272
|
+
]);
|
|
273
|
+
usageLine.push(isRequired ? `<${name}>` : `[${name}]`);
|
|
274
|
+
} else {
|
|
275
|
+
const isRequired = arg.required === true && arg.default === void 0;
|
|
276
|
+
const argStr = (arg.type === "boolean" && arg.default === true ? [...(arg.alias || []).map((a) => `--no-${a}`), `--no-${arg.name}`].join(", ") : [...(arg.alias || []).map((a) => `-${a}`), `--${arg.name}`].join(", ")) + (arg.type === "string" && (arg.valueHint || arg.default) ? `=${arg.valueHint ? `<${arg.valueHint}>` : `"${arg.default || ""}"`}` : "");
|
|
277
|
+
argLines.push(["`" + argStr + (isRequired ? " (required)" : "") + "`", arg.description || ""]);
|
|
278
|
+
if (isRequired) usageLine.push(argStr);
|
|
279
|
+
}
|
|
280
|
+
if (cmd.subCommands) {
|
|
281
|
+
const commandNames = [];
|
|
282
|
+
const subCommands = await resolveValue(cmd.subCommands);
|
|
283
|
+
for (const [name, sub] of Object.entries(subCommands)) {
|
|
284
|
+
const subCmd = await resolveValue(sub);
|
|
285
|
+
const meta = await resolveValue(subCmd?.meta);
|
|
286
|
+
commandsLines.push([`\`${name}\``, meta?.description || ""]);
|
|
287
|
+
commandNames.push(name);
|
|
288
|
+
}
|
|
289
|
+
usageLine.push(commandNames.join("|"));
|
|
290
|
+
}
|
|
291
|
+
const usageLines = [];
|
|
292
|
+
const version = cmdMeta.version || parentMeta.version;
|
|
293
|
+
usageLines.push(colors.gray(`${cmdMeta.description} (${commandName + (version ? ` v${version}` : "")})`), "");
|
|
294
|
+
const hasOptions = argLines.length > 0 || posLines.length > 0;
|
|
295
|
+
usageLines.push(`${colors.underline(colors.bold("USAGE"))} \`${commandName}${hasOptions ? " [OPTIONS]" : ""} ${usageLine.join(" ")}\``, "");
|
|
296
|
+
if (posLines.length > 0) {
|
|
297
|
+
usageLines.push(colors.underline(colors.bold("ARGUMENTS")), "");
|
|
298
|
+
usageLines.push(formatLineColumns(posLines, " "));
|
|
299
|
+
usageLines.push("");
|
|
300
|
+
}
|
|
301
|
+
if (argLines.length > 0) {
|
|
302
|
+
usageLines.push(colors.underline(colors.bold("OPTIONS")), "");
|
|
303
|
+
usageLines.push(formatLineColumns(argLines, " "));
|
|
304
|
+
usageLines.push("");
|
|
305
|
+
}
|
|
306
|
+
if (commandsLines.length > 0) {
|
|
307
|
+
usageLines.push(colors.underline(colors.bold("COMMANDS")), "");
|
|
308
|
+
usageLines.push(formatLineColumns(commandsLines, " "));
|
|
309
|
+
usageLines.push("", `Use \`${commandName} <command> --help\` for more information about a command.`);
|
|
310
|
+
}
|
|
311
|
+
return usageLines.filter((l) => typeof l === "string").join("\n");
|
|
312
|
+
}
|
|
313
|
+
async function runMain(cmd, opts = {}) {
|
|
314
|
+
const rawArgs = opts.rawArgs || process.argv.slice(2);
|
|
315
|
+
const showUsage$1 = opts.showUsage || showUsage;
|
|
316
|
+
try {
|
|
317
|
+
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
|
|
318
|
+
await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
319
|
+
process.exit(0);
|
|
320
|
+
} else if (rawArgs.length === 1 && rawArgs[0] === "--version") {
|
|
321
|
+
const meta = typeof cmd.meta === "function" ? await cmd.meta() : await cmd.meta;
|
|
322
|
+
if (!meta?.version) throw new CLIError("No version specified", "E_NO_VERSION");
|
|
323
|
+
consola.log(meta.version);
|
|
324
|
+
} else await runCommand(cmd, { rawArgs });
|
|
325
|
+
} catch (error) {
|
|
326
|
+
const isCLIError = error instanceof CLIError;
|
|
327
|
+
if (!isCLIError) consola.error(error, "\n");
|
|
328
|
+
if (isCLIError) await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
329
|
+
consola.error(error.message);
|
|
330
|
+
process.exit(1);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
function createMain(cmd) {
|
|
334
|
+
return (opts = {}) => runMain(cmd, opts);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region src/fig.ts
|
|
339
|
+
async function processArgs$1(args) {
|
|
340
|
+
const options = [];
|
|
341
|
+
const positionalArgs = [];
|
|
342
|
+
for (const [name, arg] of Object.entries(args)) if (arg.type === "positional") {
|
|
343
|
+
const positionalArg = arg;
|
|
344
|
+
positionalArgs.push({
|
|
345
|
+
name,
|
|
346
|
+
description: positionalArg.description,
|
|
347
|
+
isOptional: !positionalArg.required,
|
|
348
|
+
isVariadic: name.startsWith("[...") || name.startsWith("<...")
|
|
349
|
+
});
|
|
350
|
+
} else {
|
|
351
|
+
const option = {
|
|
352
|
+
name: `--${name}`,
|
|
353
|
+
description: arg.description || "",
|
|
354
|
+
isRequired: arg.required
|
|
355
|
+
};
|
|
356
|
+
if ("alias" in arg && arg.alias) {
|
|
357
|
+
const aliases = Array.isArray(arg.alias) ? arg.alias : [arg.alias];
|
|
358
|
+
aliases.forEach((alias) => {
|
|
359
|
+
options.push({
|
|
360
|
+
...option,
|
|
361
|
+
name: `-${alias}`
|
|
362
|
+
});
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
options.push(option);
|
|
366
|
+
}
|
|
367
|
+
return {
|
|
368
|
+
options,
|
|
369
|
+
args: positionalArgs
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
async function processCommand(command, parentName = "") {
|
|
373
|
+
const resolvedMeta = await Promise.resolve(command.meta);
|
|
374
|
+
const meta = resolvedMeta;
|
|
375
|
+
const subCommands = await Promise.resolve(command.subCommands);
|
|
376
|
+
if (!meta || !meta.name) throw new Error("Command meta or name is missing");
|
|
377
|
+
const spec = {
|
|
378
|
+
name: parentName ? `${parentName} ${meta.name}` : meta.name,
|
|
379
|
+
description: meta.description || ""
|
|
380
|
+
};
|
|
381
|
+
if (command.args) {
|
|
382
|
+
const resolvedArgs = await Promise.resolve(command.args);
|
|
383
|
+
const { options, args } = await processArgs$1(resolvedArgs);
|
|
384
|
+
if (options.length > 0) spec.options = options;
|
|
385
|
+
if (args.length > 0) spec.args = args;
|
|
386
|
+
}
|
|
387
|
+
if (subCommands) spec.subcommands = await Promise.all(Object.entries(subCommands).map(async ([_, subCmd]) => {
|
|
388
|
+
const resolved = await Promise.resolve(subCmd);
|
|
389
|
+
return processCommand(resolved, spec.name);
|
|
390
|
+
}));
|
|
391
|
+
return spec;
|
|
392
|
+
}
|
|
393
|
+
async function generateFigSpec(command) {
|
|
394
|
+
const spec = await processCommand(command);
|
|
395
|
+
return JSON.stringify(spec, null, 2);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
//#endregion
|
|
399
|
+
//#region src/citty.ts
|
|
400
|
+
function quoteIfNeeded(path) {
|
|
401
|
+
return path.includes(" ") ? `'${path}'` : path;
|
|
402
|
+
}
|
|
403
|
+
const execPath = process.execPath;
|
|
404
|
+
const processArgs = process.argv.slice(1);
|
|
405
|
+
const quotedExecPath = quoteIfNeeded(execPath);
|
|
406
|
+
const quotedProcessArgs = processArgs.map(quoteIfNeeded);
|
|
407
|
+
const quotedProcessExecArgs = process.execArgv.map(quoteIfNeeded);
|
|
408
|
+
const x = `${quotedExecPath} ${quotedProcessExecArgs.join(" ")} ${quotedProcessArgs[0]}`;
|
|
409
|
+
function isConfigPositional(config) {
|
|
410
|
+
return config.args && Object.values(config.args).some((arg) => arg.type === "positional");
|
|
411
|
+
}
|
|
412
|
+
async function handleSubCommands(subCommands, parentCmd, completionConfig) {
|
|
413
|
+
for (const [cmd, resolvableConfig] of Object.entries(subCommands)) {
|
|
414
|
+
const config = await resolve(resolvableConfig);
|
|
415
|
+
const meta = await resolve(config.meta);
|
|
416
|
+
const subCommands$1 = await resolve(config.subCommands);
|
|
417
|
+
const subCompletionConfig = completionConfig?.[cmd];
|
|
418
|
+
if (!meta || typeof meta?.description !== "string") throw new Error("Invalid meta or missing description.");
|
|
419
|
+
const isPositional = isConfigPositional(config);
|
|
420
|
+
const commandName = parentCmd ? `${parentCmd} ${cmd}` : cmd;
|
|
421
|
+
const command = t_default.command(commandName, meta.description);
|
|
422
|
+
if (isPositional && config.args) for (const [argName, argConfig] of Object.entries(config.args)) {
|
|
423
|
+
const conf = argConfig;
|
|
424
|
+
if (conf.type === "positional") {
|
|
425
|
+
const isVariadic = conf.required === false;
|
|
426
|
+
const argHandler = subCompletionConfig?.args?.[argName];
|
|
427
|
+
if (argHandler) command.argument(argName, argHandler, isVariadic);
|
|
428
|
+
else command.argument(argName, void 0, isVariadic);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
if (subCommands$1) await handleSubCommands(subCommands$1, commandName, subCompletionConfig?.subCommands);
|
|
432
|
+
if (config.args) for (const [argName, argConfig] of Object.entries(config.args)) {
|
|
433
|
+
const conf = argConfig;
|
|
434
|
+
const shortFlag = typeof conf === "object" && "alias" in conf ? Array.isArray(conf.alias) ? conf.alias[0] : conf.alias : void 0;
|
|
435
|
+
const handler = subCompletionConfig?.options?.[argName];
|
|
436
|
+
if (handler) if (shortFlag) command.option(argName, conf.description ?? "", handler, shortFlag);
|
|
437
|
+
else command.option(argName, conf.description ?? "", handler);
|
|
438
|
+
else if (shortFlag) command.option(argName, conf.description ?? "", shortFlag);
|
|
439
|
+
else command.option(argName, conf.description ?? "");
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
async function tab(instance, completionConfig) {
|
|
444
|
+
const meta = await resolve(instance.meta);
|
|
445
|
+
if (!meta) throw new Error("Invalid meta.");
|
|
446
|
+
const name = meta.name;
|
|
447
|
+
if (!name) throw new Error("Invalid meta or missing name.");
|
|
448
|
+
const subCommands = await resolve(instance.subCommands);
|
|
449
|
+
if (!subCommands) throw new Error("Invalid or missing subCommands.");
|
|
450
|
+
const isPositional = isConfigPositional(instance);
|
|
451
|
+
if (isPositional && instance.args) for (const [argName, argConfig] of Object.entries(instance.args)) {
|
|
452
|
+
const conf = argConfig;
|
|
453
|
+
if (conf.type === "positional") {
|
|
454
|
+
const isVariadic = conf.required === false;
|
|
455
|
+
const argHandler = completionConfig?.args?.[argName];
|
|
456
|
+
if (argHandler) t_default.argument(argName, argHandler, isVariadic);
|
|
457
|
+
else t_default.argument(argName, void 0, isVariadic);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
await handleSubCommands(subCommands, void 0, completionConfig?.subCommands);
|
|
461
|
+
if (instance.args) for (const [argName, argConfig] of Object.entries(instance.args)) {
|
|
462
|
+
const conf = argConfig;
|
|
463
|
+
const shortFlag = typeof conf === "object" && "alias" in conf ? Array.isArray(conf.alias) ? conf.alias[0] : conf.alias : void 0;
|
|
464
|
+
const handler = completionConfig?.options?.[argName];
|
|
465
|
+
if (handler) if (shortFlag) t_default.option(argName, conf.description ?? "", handler, shortFlag);
|
|
466
|
+
else t_default.option(argName, conf.description ?? "", handler);
|
|
467
|
+
else if (shortFlag) t_default.option(argName, conf.description ?? "", shortFlag);
|
|
468
|
+
else t_default.option(argName, conf.description ?? "");
|
|
469
|
+
}
|
|
470
|
+
const completeCommand = defineCommand({
|
|
471
|
+
meta: {
|
|
472
|
+
name: "complete",
|
|
473
|
+
description: "Generate shell completion scripts"
|
|
474
|
+
},
|
|
475
|
+
args: { shell: {
|
|
476
|
+
type: "positional",
|
|
477
|
+
description: "Shell type (zsh, bash, fish, powershell, fig)",
|
|
478
|
+
required: false
|
|
479
|
+
} },
|
|
480
|
+
async run(ctx) {
|
|
481
|
+
let shell = ctx.rawArgs[0];
|
|
482
|
+
if (shell === "--") shell = void 0;
|
|
483
|
+
switch (shell) {
|
|
484
|
+
case "zsh": {
|
|
485
|
+
const script = generate(name, x);
|
|
486
|
+
console.log(script);
|
|
487
|
+
break;
|
|
488
|
+
}
|
|
489
|
+
case "bash": {
|
|
490
|
+
const script = generate$1(name, x);
|
|
491
|
+
console.log(script);
|
|
492
|
+
break;
|
|
493
|
+
}
|
|
494
|
+
case "fish": {
|
|
495
|
+
const script = generate$2(name, x);
|
|
496
|
+
console.log(script);
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
case "powershell": {
|
|
500
|
+
const script = generate$3(name, x);
|
|
501
|
+
console.log(script);
|
|
502
|
+
break;
|
|
503
|
+
}
|
|
504
|
+
case "fig": {
|
|
505
|
+
const spec = await generateFigSpec(instance);
|
|
506
|
+
console.log(spec);
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
default: {
|
|
510
|
+
assertDoubleDashes(name);
|
|
511
|
+
const extra = ctx.rawArgs.slice(ctx.rawArgs.indexOf("--") + 1);
|
|
512
|
+
return t_default.parse(extra);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
subCommands.complete = completeCommand;
|
|
518
|
+
return t_default;
|
|
519
|
+
}
|
|
520
|
+
async function resolve(resolvable) {
|
|
521
|
+
return resolvable instanceof Function ? await resolvable() : await resolvable;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
//#endregion
|
|
525
|
+
export { createMain, defineCommand, tab as tab$1 };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { generate as generate$3, generate$1 as generate$2, generate$2 as generate$1, generate$3 as generate, t_default } from "./t-Zhhzaib1.js";
|
|
2
|
+
import { assertDoubleDashes } from "./shared-BGWjvggn.js";
|
|
3
|
+
|
|
4
|
+
//#region src/commander.ts
|
|
5
|
+
const execPath = process.execPath;
|
|
6
|
+
const processArgs = process.argv.slice(1);
|
|
7
|
+
const quotedExecPath = quoteIfNeeded(execPath);
|
|
8
|
+
const quotedProcessArgs = processArgs.map(quoteIfNeeded);
|
|
9
|
+
const quotedProcessExecArgs = process.execArgv.map(quoteIfNeeded);
|
|
10
|
+
const x = `${quotedExecPath} ${quotedProcessExecArgs.join(" ")} ${quotedProcessArgs[0]}`;
|
|
11
|
+
function quoteIfNeeded(path) {
|
|
12
|
+
return path.includes(" ") ? `'${path}'` : path;
|
|
13
|
+
}
|
|
14
|
+
function tab(instance) {
|
|
15
|
+
const programName = instance.name();
|
|
16
|
+
processRootCommand(instance, programName);
|
|
17
|
+
processSubcommands(instance, programName);
|
|
18
|
+
instance.command("complete [shell]").description("Generate shell completion scripts").action(async (shell) => {
|
|
19
|
+
switch (shell) {
|
|
20
|
+
case "zsh": {
|
|
21
|
+
const script = generate(programName, x);
|
|
22
|
+
console.log(script);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
case "bash": {
|
|
26
|
+
const script = generate$1(programName, x);
|
|
27
|
+
console.log(script);
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
case "fish": {
|
|
31
|
+
const script = generate$2(programName, x);
|
|
32
|
+
console.log(script);
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
case "powershell": {
|
|
36
|
+
const script = generate$3(programName, x);
|
|
37
|
+
console.log(script);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
case "debug": {
|
|
41
|
+
const commandMap = new Map();
|
|
42
|
+
collectCommands(instance, "", commandMap);
|
|
43
|
+
console.log("Collected commands:");
|
|
44
|
+
for (const [path, cmd] of commandMap.entries()) console.log(`- ${path || "<root>"}: ${cmd.description() || "No description"}`);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
default: {
|
|
48
|
+
console.error(`Unknown shell: ${shell}`);
|
|
49
|
+
console.error("Supported shells: zsh, bash, fish, powershell");
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
const originalParse = instance.parse.bind(instance);
|
|
55
|
+
instance.parse = function(argv, options) {
|
|
56
|
+
const args = argv || process.argv;
|
|
57
|
+
const completeIndex = args.findIndex((arg) => arg === "complete");
|
|
58
|
+
const dashDashIndex = args.findIndex((arg) => arg === "--");
|
|
59
|
+
if (completeIndex !== -1 && dashDashIndex !== -1 && dashDashIndex > completeIndex) {
|
|
60
|
+
const extra = args.slice(dashDashIndex + 1);
|
|
61
|
+
assertDoubleDashes(programName);
|
|
62
|
+
t_default.parse(extra);
|
|
63
|
+
return instance;
|
|
64
|
+
}
|
|
65
|
+
return originalParse(argv, options);
|
|
66
|
+
};
|
|
67
|
+
return t_default;
|
|
68
|
+
}
|
|
69
|
+
function processRootCommand(command, programName) {
|
|
70
|
+
for (const option of command.options) {
|
|
71
|
+
const flags = option.flags;
|
|
72
|
+
const shortFlag = flags.match(/^-([a-zA-Z]), --/)?.[1];
|
|
73
|
+
const longFlag = flags.match(/--([a-zA-Z0-9-]+)/)?.[1];
|
|
74
|
+
if (longFlag) if (shortFlag) t_default.option(longFlag, option.description || "", shortFlag);
|
|
75
|
+
else t_default.option(longFlag, option.description || "");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function processSubcommands(rootCommand, programName) {
|
|
79
|
+
const commandMap = new Map();
|
|
80
|
+
collectCommands(rootCommand, "", commandMap);
|
|
81
|
+
for (const [path, cmd] of commandMap.entries()) {
|
|
82
|
+
if (path === "") continue;
|
|
83
|
+
const command = t_default.command(path, cmd.description() || "");
|
|
84
|
+
for (const option of cmd.options) {
|
|
85
|
+
const flags = option.flags;
|
|
86
|
+
const shortFlag = flags.match(/^-([a-zA-Z]), --/)?.[1];
|
|
87
|
+
const longFlag = flags.match(/--([a-zA-Z0-9-]+)/)?.[1];
|
|
88
|
+
if (longFlag) if (shortFlag) command.option(longFlag, option.description || "", shortFlag);
|
|
89
|
+
else command.option(longFlag, option.description || "");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function collectCommands(command, parentPath, commandMap) {
|
|
94
|
+
commandMap.set(parentPath, command);
|
|
95
|
+
for (const subcommand of command.commands) {
|
|
96
|
+
if (subcommand.name() === "complete") continue;
|
|
97
|
+
const subcommandPath = parentPath ? `${parentPath} ${subcommand.name()}` : subcommand.name();
|
|
98
|
+
collectCommands(subcommand, subcommandPath, commandMap);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
export { tab as tab$2 };
|