citty 0.1.4 → 0.1.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/README.md +1 -1
- package/dist/index.cjs +27 -20
- package/dist/index.d.cts +7 -4
- package/dist/index.d.mts +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.mjs +27 -20
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -88,7 +88,7 @@ Create a wrapper around command that calls `runMain` when called.
|
|
|
88
88
|
|
|
89
89
|
### `runCommand`
|
|
90
90
|
|
|
91
|
-
Parses input args and runs command and sub-commands (unsupervised).
|
|
91
|
+
Parses input args and runs command and sub-commands (unsupervised). You can access `result` key from returnd/awaited value to access command's result.
|
|
92
92
|
|
|
93
93
|
### `parseArgs`
|
|
94
94
|
|
package/dist/index.cjs
CHANGED
|
@@ -43,18 +43,18 @@ function isUppercase(char = "") {
|
|
|
43
43
|
if (NUMBER_CHAR_RE.test(char)) {
|
|
44
44
|
return void 0;
|
|
45
45
|
}
|
|
46
|
-
return char.
|
|
46
|
+
return char !== char.toLowerCase();
|
|
47
47
|
}
|
|
48
|
-
function splitByCase(
|
|
48
|
+
function splitByCase(str, separators) {
|
|
49
49
|
const splitters = separators ?? STR_SPLITTERS;
|
|
50
50
|
const parts = [];
|
|
51
|
-
if (!
|
|
51
|
+
if (!str || typeof str !== "string") {
|
|
52
52
|
return parts;
|
|
53
53
|
}
|
|
54
54
|
let buff = "";
|
|
55
55
|
let previousUpper;
|
|
56
56
|
let previousSplitter;
|
|
57
|
-
for (const char of
|
|
57
|
+
for (const char of str) {
|
|
58
58
|
const isSplitter = splitters.includes(char);
|
|
59
59
|
if (isSplitter === true) {
|
|
60
60
|
parts.push(buff);
|
|
@@ -71,7 +71,7 @@ function splitByCase(string_, separators) {
|
|
|
71
71
|
continue;
|
|
72
72
|
}
|
|
73
73
|
if (previousUpper === true && isUpper === false && buff.length > 1) {
|
|
74
|
-
const lastChar = buff
|
|
74
|
+
const lastChar = buff.at(-1);
|
|
75
75
|
parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
|
|
76
76
|
buff = lastChar + char;
|
|
77
77
|
previousUpper = isUpper;
|
|
@@ -85,20 +85,20 @@ function splitByCase(string_, separators) {
|
|
|
85
85
|
parts.push(buff);
|
|
86
86
|
return parts;
|
|
87
87
|
}
|
|
88
|
-
function upperFirst(
|
|
89
|
-
return
|
|
88
|
+
function upperFirst(str) {
|
|
89
|
+
return str ? str[0].toUpperCase() + str.slice(1) : "";
|
|
90
90
|
}
|
|
91
|
-
function lowerFirst(
|
|
92
|
-
return
|
|
91
|
+
function lowerFirst(str) {
|
|
92
|
+
return str ? str[0].toLowerCase() + str.slice(1) : "";
|
|
93
93
|
}
|
|
94
|
-
function pascalCase(
|
|
95
|
-
return
|
|
94
|
+
function pascalCase(str, opts) {
|
|
95
|
+
return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("") : "";
|
|
96
96
|
}
|
|
97
|
-
function camelCase(
|
|
98
|
-
return lowerFirst(pascalCase(
|
|
97
|
+
function camelCase(str, opts) {
|
|
98
|
+
return lowerFirst(pascalCase(str || "", opts));
|
|
99
99
|
}
|
|
100
|
-
function kebabCase(
|
|
101
|
-
return
|
|
100
|
+
function kebabCase(str, joiner) {
|
|
101
|
+
return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => p.toLowerCase()).join(joiner ?? "-") : "";
|
|
102
102
|
}
|
|
103
103
|
|
|
104
104
|
function toArr(any) {
|
|
@@ -293,6 +293,7 @@ async function runCommand(cmd, opts) {
|
|
|
293
293
|
if (typeof cmd.setup === "function") {
|
|
294
294
|
await cmd.setup(context);
|
|
295
295
|
}
|
|
296
|
+
let result;
|
|
296
297
|
try {
|
|
297
298
|
const subCommands = await resolveValue(cmd.subCommands);
|
|
298
299
|
if (subCommands && Object.keys(subCommands).length > 0) {
|
|
@@ -318,13 +319,14 @@ async function runCommand(cmd, opts) {
|
|
|
318
319
|
}
|
|
319
320
|
}
|
|
320
321
|
if (typeof cmd.run === "function") {
|
|
321
|
-
await cmd.run(context);
|
|
322
|
+
result = await cmd.run(context);
|
|
322
323
|
}
|
|
323
324
|
} finally {
|
|
324
325
|
if (typeof cmd.cleanup === "function") {
|
|
325
326
|
await cmd.cleanup(context);
|
|
326
327
|
}
|
|
327
328
|
}
|
|
329
|
+
return { result };
|
|
328
330
|
}
|
|
329
331
|
async function resolveSubCommand(cmd, rawArgs, parent) {
|
|
330
332
|
const subCommands = await resolveValue(cmd.subCommands);
|
|
@@ -363,8 +365,12 @@ async function renderUsage(cmd, parent) {
|
|
|
363
365
|
if (arg.type === "positional") {
|
|
364
366
|
const name = arg.name.toUpperCase();
|
|
365
367
|
const isRequired = arg.required !== false && arg.default === void 0;
|
|
366
|
-
const
|
|
367
|
-
posLines.push([
|
|
368
|
+
const defaultHint = arg.default ? `="${arg.default}"` : "";
|
|
369
|
+
posLines.push([
|
|
370
|
+
"`" + name + defaultHint + "`",
|
|
371
|
+
arg.description || "",
|
|
372
|
+
arg.valueHint ? `<${arg.valueHint}>` : ""
|
|
373
|
+
]);
|
|
368
374
|
usageLine.push(isRequired ? `<${name}>` : `[${name}]`);
|
|
369
375
|
} else {
|
|
370
376
|
const isRequired = arg.required === true && arg.default === void 0;
|
|
@@ -430,9 +436,10 @@ async function renderUsage(cmd, parent) {
|
|
|
430
436
|
|
|
431
437
|
async function runMain(cmd, opts = {}) {
|
|
432
438
|
const rawArgs = opts.rawArgs || process.argv.slice(2);
|
|
439
|
+
const showUsage$1 = opts.showUsage || showUsage;
|
|
433
440
|
try {
|
|
434
441
|
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
|
|
435
|
-
await showUsage(...await resolveSubCommand(cmd, rawArgs));
|
|
442
|
+
await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
436
443
|
process.exit(0);
|
|
437
444
|
} else if (rawArgs.length === 1 && rawArgs[0] === "--version") {
|
|
438
445
|
const meta = typeof cmd.meta === "function" ? await cmd.meta() : await cmd.meta;
|
|
@@ -449,7 +456,7 @@ async function runMain(cmd, opts = {}) {
|
|
|
449
456
|
consola__default.error(error, "\n");
|
|
450
457
|
}
|
|
451
458
|
if (isCLIError) {
|
|
452
|
-
await showUsage(...await resolveSubCommand(cmd, rawArgs));
|
|
459
|
+
await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
453
460
|
}
|
|
454
461
|
consola__default.error(error.message);
|
|
455
462
|
process.exit(1);
|
package/dist/index.d.cts
CHANGED
|
@@ -61,17 +61,20 @@ interface RunCommandOptions {
|
|
|
61
61
|
data?: any;
|
|
62
62
|
showUsage?: boolean;
|
|
63
63
|
}
|
|
64
|
-
declare function runCommand<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts: RunCommandOptions): Promise<
|
|
64
|
+
declare function runCommand<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts: RunCommandOptions): Promise<{
|
|
65
|
+
result: unknown;
|
|
66
|
+
}>;
|
|
67
|
+
|
|
68
|
+
declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
|
|
69
|
+
declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
|
|
65
70
|
|
|
66
71
|
interface RunMainOptions {
|
|
67
72
|
rawArgs?: string[];
|
|
73
|
+
showUsage?: typeof showUsage;
|
|
68
74
|
}
|
|
69
75
|
declare function runMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts?: RunMainOptions): Promise<void>;
|
|
70
76
|
declare function createMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>): (opts?: RunMainOptions) => Promise<void>;
|
|
71
77
|
|
|
72
78
|
declare function parseArgs<T extends ArgsDef = ArgsDef>(rawArgs: string[], argsDef: ArgsDef): ParsedArgs<T>;
|
|
73
79
|
|
|
74
|
-
declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
|
|
75
|
-
declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
|
|
76
|
-
|
|
77
80
|
export { type Arg, type ArgDef, type ArgType, type ArgsDef, type Awaitable, type BooleanArgDef, type CommandContext, type CommandDef, type CommandMeta, type ParsedArgs, type PositionalArgDef, type Resolvable, type RunCommandOptions, type RunMainOptions, type StringArgDef, type SubCommandsDef, type _ArgDef, createMain, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
|
package/dist/index.d.mts
CHANGED
|
@@ -61,17 +61,20 @@ interface RunCommandOptions {
|
|
|
61
61
|
data?: any;
|
|
62
62
|
showUsage?: boolean;
|
|
63
63
|
}
|
|
64
|
-
declare function runCommand<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts: RunCommandOptions): Promise<
|
|
64
|
+
declare function runCommand<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts: RunCommandOptions): Promise<{
|
|
65
|
+
result: unknown;
|
|
66
|
+
}>;
|
|
67
|
+
|
|
68
|
+
declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
|
|
69
|
+
declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
|
|
65
70
|
|
|
66
71
|
interface RunMainOptions {
|
|
67
72
|
rawArgs?: string[];
|
|
73
|
+
showUsage?: typeof showUsage;
|
|
68
74
|
}
|
|
69
75
|
declare function runMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts?: RunMainOptions): Promise<void>;
|
|
70
76
|
declare function createMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>): (opts?: RunMainOptions) => Promise<void>;
|
|
71
77
|
|
|
72
78
|
declare function parseArgs<T extends ArgsDef = ArgsDef>(rawArgs: string[], argsDef: ArgsDef): ParsedArgs<T>;
|
|
73
79
|
|
|
74
|
-
declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
|
|
75
|
-
declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
|
|
76
|
-
|
|
77
80
|
export { type Arg, type ArgDef, type ArgType, type ArgsDef, type Awaitable, type BooleanArgDef, type CommandContext, type CommandDef, type CommandMeta, type ParsedArgs, type PositionalArgDef, type Resolvable, type RunCommandOptions, type RunMainOptions, type StringArgDef, type SubCommandsDef, type _ArgDef, createMain, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
|
package/dist/index.d.ts
CHANGED
|
@@ -61,17 +61,20 @@ interface RunCommandOptions {
|
|
|
61
61
|
data?: any;
|
|
62
62
|
showUsage?: boolean;
|
|
63
63
|
}
|
|
64
|
-
declare function runCommand<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts: RunCommandOptions): Promise<
|
|
64
|
+
declare function runCommand<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts: RunCommandOptions): Promise<{
|
|
65
|
+
result: unknown;
|
|
66
|
+
}>;
|
|
67
|
+
|
|
68
|
+
declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
|
|
69
|
+
declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
|
|
65
70
|
|
|
66
71
|
interface RunMainOptions {
|
|
67
72
|
rawArgs?: string[];
|
|
73
|
+
showUsage?: typeof showUsage;
|
|
68
74
|
}
|
|
69
75
|
declare function runMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, opts?: RunMainOptions): Promise<void>;
|
|
70
76
|
declare function createMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>): (opts?: RunMainOptions) => Promise<void>;
|
|
71
77
|
|
|
72
78
|
declare function parseArgs<T extends ArgsDef = ArgsDef>(rawArgs: string[], argsDef: ArgsDef): ParsedArgs<T>;
|
|
73
79
|
|
|
74
|
-
declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
|
|
75
|
-
declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
|
|
76
|
-
|
|
77
80
|
export { type Arg, type ArgDef, type ArgType, type ArgsDef, type Awaitable, type BooleanArgDef, type CommandContext, type CommandDef, type CommandMeta, type ParsedArgs, type PositionalArgDef, type Resolvable, type RunCommandOptions, type RunMainOptions, type StringArgDef, type SubCommandsDef, type _ArgDef, createMain, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
|
package/dist/index.mjs
CHANGED
|
@@ -37,18 +37,18 @@ function isUppercase(char = "") {
|
|
|
37
37
|
if (NUMBER_CHAR_RE.test(char)) {
|
|
38
38
|
return void 0;
|
|
39
39
|
}
|
|
40
|
-
return char.
|
|
40
|
+
return char !== char.toLowerCase();
|
|
41
41
|
}
|
|
42
|
-
function splitByCase(
|
|
42
|
+
function splitByCase(str, separators) {
|
|
43
43
|
const splitters = separators ?? STR_SPLITTERS;
|
|
44
44
|
const parts = [];
|
|
45
|
-
if (!
|
|
45
|
+
if (!str || typeof str !== "string") {
|
|
46
46
|
return parts;
|
|
47
47
|
}
|
|
48
48
|
let buff = "";
|
|
49
49
|
let previousUpper;
|
|
50
50
|
let previousSplitter;
|
|
51
|
-
for (const char of
|
|
51
|
+
for (const char of str) {
|
|
52
52
|
const isSplitter = splitters.includes(char);
|
|
53
53
|
if (isSplitter === true) {
|
|
54
54
|
parts.push(buff);
|
|
@@ -65,7 +65,7 @@ function splitByCase(string_, separators) {
|
|
|
65
65
|
continue;
|
|
66
66
|
}
|
|
67
67
|
if (previousUpper === true && isUpper === false && buff.length > 1) {
|
|
68
|
-
const lastChar = buff
|
|
68
|
+
const lastChar = buff.at(-1);
|
|
69
69
|
parts.push(buff.slice(0, Math.max(0, buff.length - 1)));
|
|
70
70
|
buff = lastChar + char;
|
|
71
71
|
previousUpper = isUpper;
|
|
@@ -79,20 +79,20 @@ function splitByCase(string_, separators) {
|
|
|
79
79
|
parts.push(buff);
|
|
80
80
|
return parts;
|
|
81
81
|
}
|
|
82
|
-
function upperFirst(
|
|
83
|
-
return
|
|
82
|
+
function upperFirst(str) {
|
|
83
|
+
return str ? str[0].toUpperCase() + str.slice(1) : "";
|
|
84
84
|
}
|
|
85
|
-
function lowerFirst(
|
|
86
|
-
return
|
|
85
|
+
function lowerFirst(str) {
|
|
86
|
+
return str ? str[0].toLowerCase() + str.slice(1) : "";
|
|
87
87
|
}
|
|
88
|
-
function pascalCase(
|
|
89
|
-
return
|
|
88
|
+
function pascalCase(str, opts) {
|
|
89
|
+
return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => upperFirst(opts?.normalize ? p.toLowerCase() : p)).join("") : "";
|
|
90
90
|
}
|
|
91
|
-
function camelCase(
|
|
92
|
-
return lowerFirst(pascalCase(
|
|
91
|
+
function camelCase(str, opts) {
|
|
92
|
+
return lowerFirst(pascalCase(str || "", opts));
|
|
93
93
|
}
|
|
94
|
-
function kebabCase(
|
|
95
|
-
return
|
|
94
|
+
function kebabCase(str, joiner) {
|
|
95
|
+
return str ? (Array.isArray(str) ? str : splitByCase(str)).map((p) => p.toLowerCase()).join(joiner ?? "-") : "";
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
function toArr(any) {
|
|
@@ -287,6 +287,7 @@ async function runCommand(cmd, opts) {
|
|
|
287
287
|
if (typeof cmd.setup === "function") {
|
|
288
288
|
await cmd.setup(context);
|
|
289
289
|
}
|
|
290
|
+
let result;
|
|
290
291
|
try {
|
|
291
292
|
const subCommands = await resolveValue(cmd.subCommands);
|
|
292
293
|
if (subCommands && Object.keys(subCommands).length > 0) {
|
|
@@ -312,13 +313,14 @@ async function runCommand(cmd, opts) {
|
|
|
312
313
|
}
|
|
313
314
|
}
|
|
314
315
|
if (typeof cmd.run === "function") {
|
|
315
|
-
await cmd.run(context);
|
|
316
|
+
result = await cmd.run(context);
|
|
316
317
|
}
|
|
317
318
|
} finally {
|
|
318
319
|
if (typeof cmd.cleanup === "function") {
|
|
319
320
|
await cmd.cleanup(context);
|
|
320
321
|
}
|
|
321
322
|
}
|
|
323
|
+
return { result };
|
|
322
324
|
}
|
|
323
325
|
async function resolveSubCommand(cmd, rawArgs, parent) {
|
|
324
326
|
const subCommands = await resolveValue(cmd.subCommands);
|
|
@@ -357,8 +359,12 @@ async function renderUsage(cmd, parent) {
|
|
|
357
359
|
if (arg.type === "positional") {
|
|
358
360
|
const name = arg.name.toUpperCase();
|
|
359
361
|
const isRequired = arg.required !== false && arg.default === void 0;
|
|
360
|
-
const
|
|
361
|
-
posLines.push([
|
|
362
|
+
const defaultHint = arg.default ? `="${arg.default}"` : "";
|
|
363
|
+
posLines.push([
|
|
364
|
+
"`" + name + defaultHint + "`",
|
|
365
|
+
arg.description || "",
|
|
366
|
+
arg.valueHint ? `<${arg.valueHint}>` : ""
|
|
367
|
+
]);
|
|
362
368
|
usageLine.push(isRequired ? `<${name}>` : `[${name}]`);
|
|
363
369
|
} else {
|
|
364
370
|
const isRequired = arg.required === true && arg.default === void 0;
|
|
@@ -424,9 +430,10 @@ async function renderUsage(cmd, parent) {
|
|
|
424
430
|
|
|
425
431
|
async function runMain(cmd, opts = {}) {
|
|
426
432
|
const rawArgs = opts.rawArgs || process.argv.slice(2);
|
|
433
|
+
const showUsage$1 = opts.showUsage || showUsage;
|
|
427
434
|
try {
|
|
428
435
|
if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
|
|
429
|
-
await showUsage(...await resolveSubCommand(cmd, rawArgs));
|
|
436
|
+
await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
430
437
|
process.exit(0);
|
|
431
438
|
} else if (rawArgs.length === 1 && rawArgs[0] === "--version") {
|
|
432
439
|
const meta = typeof cmd.meta === "function" ? await cmd.meta() : await cmd.meta;
|
|
@@ -443,7 +450,7 @@ async function runMain(cmd, opts = {}) {
|
|
|
443
450
|
consola.error(error, "\n");
|
|
444
451
|
}
|
|
445
452
|
if (isCLIError) {
|
|
446
|
-
await showUsage(...await resolveSubCommand(cmd, rawArgs));
|
|
453
|
+
await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
|
|
447
454
|
}
|
|
448
455
|
consola.error(error.message);
|
|
449
456
|
process.exit(1);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "citty",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "Elegant CLI Builder",
|
|
5
5
|
"repository": "unjs/citty",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,17 +33,17 @@
|
|
|
33
33
|
"consola": "^3.2.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@types/node": "^20.
|
|
37
|
-
"@vitest/coverage-v8": "^
|
|
36
|
+
"@types/node": "^20.11.17",
|
|
37
|
+
"@vitest/coverage-v8": "^1.2.2",
|
|
38
38
|
"changelogen": "^0.5.5",
|
|
39
|
-
"eslint": "^8.
|
|
39
|
+
"eslint": "^8.56.0",
|
|
40
40
|
"eslint-config-unjs": "^0.2.1",
|
|
41
|
-
"jiti": "^1.
|
|
42
|
-
"prettier": "^3.
|
|
43
|
-
"scule": "^1.
|
|
44
|
-
"typescript": "^5.
|
|
41
|
+
"jiti": "^1.21.0",
|
|
42
|
+
"prettier": "^3.2.5",
|
|
43
|
+
"scule": "^1.3.0",
|
|
44
|
+
"typescript": "^5.3.3",
|
|
45
45
|
"unbuild": "^2.0.0",
|
|
46
|
-
"vitest": "^
|
|
46
|
+
"vitest": "^1.2.2"
|
|
47
47
|
},
|
|
48
|
-
"packageManager": "pnpm@8.
|
|
48
|
+
"packageManager": "pnpm@8.15.1"
|
|
49
49
|
}
|