citty 0.1.3 → 0.1.5

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 CHANGED
@@ -82,9 +82,13 @@ runMain(main);
82
82
 
83
83
  Runs a command with usage support and graceful error handling.
84
84
 
85
+ ### `createMain`
86
+
87
+ Create a wrapper around command that calls `runMain` when called.
88
+
85
89
  ### `runCommand`
86
90
 
87
- 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.
88
92
 
89
93
  ### `parseArgs`
90
94
 
package/dist/index.cjs CHANGED
@@ -293,31 +293,40 @@ async function runCommand(cmd, opts) {
293
293
  if (typeof cmd.setup === "function") {
294
294
  await cmd.setup(context);
295
295
  }
296
- const subCommands = await resolveValue(cmd.subCommands);
297
- if (subCommands && Object.keys(subCommands).length > 0) {
298
- const subCommandArgIndex = opts.rawArgs.findIndex(
299
- (arg) => !arg.startsWith("-")
300
- );
301
- const subCommandName = opts.rawArgs[subCommandArgIndex];
302
- if (!subCommandName && !cmd.run) {
303
- throw new CLIError(`No command specified.`, "E_NO_COMMAND");
304
- }
305
- if (!subCommands[subCommandName]) {
306
- throw new CLIError(
307
- `Unknown command \`${subCommandName}\``,
308
- "E_UNKNOWN_COMMAND"
296
+ let result;
297
+ try {
298
+ const subCommands = await resolveValue(cmd.subCommands);
299
+ if (subCommands && Object.keys(subCommands).length > 0) {
300
+ const subCommandArgIndex = opts.rawArgs.findIndex(
301
+ (arg) => !arg.startsWith("-")
309
302
  );
303
+ const subCommandName = opts.rawArgs[subCommandArgIndex];
304
+ if (subCommandName) {
305
+ if (!subCommands[subCommandName]) {
306
+ throw new CLIError(
307
+ `Unknown command \`${subCommandName}\``,
308
+ "E_UNKNOWN_COMMAND"
309
+ );
310
+ }
311
+ const subCommand = await resolveValue(subCommands[subCommandName]);
312
+ if (subCommand) {
313
+ await runCommand(subCommand, {
314
+ rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1)
315
+ });
316
+ }
317
+ } else if (!cmd.run) {
318
+ throw new CLIError(`No command specified.`, "E_NO_COMMAND");
319
+ }
310
320
  }
311
- const subCommand = await resolveValue(subCommands[subCommandName]);
312
- if (subCommand) {
313
- await runCommand(subCommand, {
314
- rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1)
315
- });
321
+ if (typeof cmd.run === "function") {
322
+ result = await cmd.run(context);
323
+ }
324
+ } finally {
325
+ if (typeof cmd.cleanup === "function") {
326
+ await cmd.cleanup(context);
316
327
  }
317
328
  }
318
- if (typeof cmd.run === "function") {
319
- await cmd.run(context);
320
- }
329
+ return { result };
321
330
  }
322
331
  async function resolveSubCommand(cmd, rawArgs, parent) {
323
332
  const subCommands = await resolveValue(cmd.subCommands);
@@ -423,10 +432,17 @@ async function renderUsage(cmd, parent) {
423
432
 
424
433
  async function runMain(cmd, opts = {}) {
425
434
  const rawArgs = opts.rawArgs || process.argv.slice(2);
435
+ const showUsage$1 = opts.showUsage || showUsage;
426
436
  try {
427
437
  if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
428
- await showUsage(...await resolveSubCommand(cmd, rawArgs));
438
+ await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
429
439
  process.exit(0);
440
+ } else if (rawArgs.length === 1 && rawArgs[0] === "--version") {
441
+ const meta = typeof cmd.meta === "function" ? await cmd.meta() : await cmd.meta;
442
+ if (!meta?.version) {
443
+ throw new CLIError("No version specified", "E_NO_VERSION");
444
+ }
445
+ consola__default.log(meta.version);
430
446
  } else {
431
447
  await runCommand(cmd, { rawArgs });
432
448
  }
@@ -436,13 +452,17 @@ async function runMain(cmd, opts = {}) {
436
452
  consola__default.error(error, "\n");
437
453
  }
438
454
  if (isCLIError) {
439
- await showUsage(...await resolveSubCommand(cmd, rawArgs));
455
+ await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
440
456
  }
441
457
  consola__default.error(error.message);
442
458
  process.exit(1);
443
459
  }
444
460
  }
461
+ function createMain(cmd) {
462
+ return (opts = {}) => runMain(cmd, opts);
463
+ }
445
464
 
465
+ exports.createMain = createMain;
446
466
  exports.defineCommand = defineCommand;
447
467
  exports.parseArgs = parseArgs;
448
468
  exports.renderUsage = renderUsage;
package/dist/index.d.cts CHANGED
@@ -61,16 +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<void>;
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>;
76
+ declare function createMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>): (opts?: RunMainOptions) => Promise<void>;
70
77
 
71
78
  declare function parseArgs<T extends ArgsDef = ArgsDef>(rawArgs: string[], argsDef: ArgsDef): ParsedArgs<T>;
72
79
 
73
- declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
74
- declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
75
-
76
- 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, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
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,16 +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<void>;
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>;
76
+ declare function createMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>): (opts?: RunMainOptions) => Promise<void>;
70
77
 
71
78
  declare function parseArgs<T extends ArgsDef = ArgsDef>(rawArgs: string[], argsDef: ArgsDef): ParsedArgs<T>;
72
79
 
73
- declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
74
- declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
75
-
76
- 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, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
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,16 +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<void>;
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>;
76
+ declare function createMain<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>): (opts?: RunMainOptions) => Promise<void>;
70
77
 
71
78
  declare function parseArgs<T extends ArgsDef = ArgsDef>(rawArgs: string[], argsDef: ArgsDef): ParsedArgs<T>;
72
79
 
73
- declare function showUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<void>;
74
- declare function renderUsage<T extends ArgsDef = ArgsDef>(cmd: CommandDef<T>, parent?: CommandDef<T>): Promise<string>;
75
-
76
- 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, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
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
@@ -287,31 +287,40 @@ async function runCommand(cmd, opts) {
287
287
  if (typeof cmd.setup === "function") {
288
288
  await cmd.setup(context);
289
289
  }
290
- const subCommands = await resolveValue(cmd.subCommands);
291
- if (subCommands && Object.keys(subCommands).length > 0) {
292
- const subCommandArgIndex = opts.rawArgs.findIndex(
293
- (arg) => !arg.startsWith("-")
294
- );
295
- const subCommandName = opts.rawArgs[subCommandArgIndex];
296
- if (!subCommandName && !cmd.run) {
297
- throw new CLIError(`No command specified.`, "E_NO_COMMAND");
298
- }
299
- if (!subCommands[subCommandName]) {
300
- throw new CLIError(
301
- `Unknown command \`${subCommandName}\``,
302
- "E_UNKNOWN_COMMAND"
290
+ let result;
291
+ try {
292
+ const subCommands = await resolveValue(cmd.subCommands);
293
+ if (subCommands && Object.keys(subCommands).length > 0) {
294
+ const subCommandArgIndex = opts.rawArgs.findIndex(
295
+ (arg) => !arg.startsWith("-")
303
296
  );
297
+ const subCommandName = opts.rawArgs[subCommandArgIndex];
298
+ if (subCommandName) {
299
+ if (!subCommands[subCommandName]) {
300
+ throw new CLIError(
301
+ `Unknown command \`${subCommandName}\``,
302
+ "E_UNKNOWN_COMMAND"
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
+ }
304
314
  }
305
- const subCommand = await resolveValue(subCommands[subCommandName]);
306
- if (subCommand) {
307
- await runCommand(subCommand, {
308
- rawArgs: opts.rawArgs.slice(subCommandArgIndex + 1)
309
- });
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);
310
321
  }
311
322
  }
312
- if (typeof cmd.run === "function") {
313
- await cmd.run(context);
314
- }
323
+ return { result };
315
324
  }
316
325
  async function resolveSubCommand(cmd, rawArgs, parent) {
317
326
  const subCommands = await resolveValue(cmd.subCommands);
@@ -417,10 +426,17 @@ async function renderUsage(cmd, parent) {
417
426
 
418
427
  async function runMain(cmd, opts = {}) {
419
428
  const rawArgs = opts.rawArgs || process.argv.slice(2);
429
+ const showUsage$1 = opts.showUsage || showUsage;
420
430
  try {
421
431
  if (rawArgs.includes("--help") || rawArgs.includes("-h")) {
422
- await showUsage(...await resolveSubCommand(cmd, rawArgs));
432
+ await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
423
433
  process.exit(0);
434
+ } else if (rawArgs.length === 1 && rawArgs[0] === "--version") {
435
+ const meta = typeof cmd.meta === "function" ? await cmd.meta() : await cmd.meta;
436
+ if (!meta?.version) {
437
+ throw new CLIError("No version specified", "E_NO_VERSION");
438
+ }
439
+ consola.log(meta.version);
424
440
  } else {
425
441
  await runCommand(cmd, { rawArgs });
426
442
  }
@@ -430,11 +446,14 @@ async function runMain(cmd, opts = {}) {
430
446
  consola.error(error, "\n");
431
447
  }
432
448
  if (isCLIError) {
433
- await showUsage(...await resolveSubCommand(cmd, rawArgs));
449
+ await showUsage$1(...await resolveSubCommand(cmd, rawArgs));
434
450
  }
435
451
  consola.error(error.message);
436
452
  process.exit(1);
437
453
  }
438
454
  }
455
+ function createMain(cmd) {
456
+ return (opts = {}) => runMain(cmd, opts);
457
+ }
439
458
 
440
- export { defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
459
+ export { createMain, defineCommand, parseArgs, renderUsage, runCommand, runMain, showUsage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "citty",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
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.5.4",
37
- "@vitest/coverage-v8": "^0.34.2",
36
+ "@types/node": "^20.9.0",
37
+ "@vitest/coverage-v8": "^0.34.6",
38
38
  "changelogen": "^0.5.5",
39
- "eslint": "^8.47.0",
39
+ "eslint": "^8.53.0",
40
40
  "eslint-config-unjs": "^0.2.1",
41
- "jiti": "^1.19.3",
42
- "prettier": "^3.0.2",
41
+ "jiti": "^1.21.0",
42
+ "prettier": "^3.1.0",
43
43
  "scule": "^1.0.0",
44
44
  "typescript": "^5.2.2",
45
45
  "unbuild": "^2.0.0",
46
- "vitest": "^0.34.2"
46
+ "vitest": "^0.34.6"
47
47
  },
48
- "packageManager": "pnpm@8.6.12"
48
+ "packageManager": "pnpm@8.10.3"
49
49
  }