zod-commander 0.0.5 → 0.1.1

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
@@ -45,4 +45,19 @@ program
45
45
  - **Async actions**: The `action` function can be async and receives parsed args and opts.
46
46
  - **No boilerplate**: Just export your command; integrate with your CLI runner as needed.
47
47
  - **Aliases for options**: If you start a description with a letter and a semicolon (e.g. `"f;The file to export to"`), that letter will be used as a short alias (e.g. `-f`).
48
- - **Perfect help output**: The generated help text is clear and complete—try running your command with `--help` to see for yourself!
48
+ - **Perfect help output**: The generated help text is clear and complete—try running your command with `--help` to see for yourself!
49
+
50
+ ### Zod 4 support
51
+
52
+ Currently, the package uses zod `v3` by default. Zod version can be specified by importing the appropriate version from the `zod3` or `zod4` submodules.
53
+
54
+ ```ts
55
+ import { zodCommand } from 'zod-commander/zod4'
56
+ // or
57
+ import { zodCommand } from 'zod-commander/zod3' // equivalent to `import { zodCommand } from 'zod-commander'`
58
+ ```
59
+
60
+ In the future, the default version will be changed to zod `v4`.
61
+
62
+ > [!NOTE]
63
+ > To use `zod-commander/zod4` in the same way as `zod-commander/zod3`, you should use [`.prefault()`](https://zod.dev/v4/changelog#default-updates) instead of `.default()`.
@@ -0,0 +1,15 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __name = (target, value) => __defProp(target, "name", {
4
+ value,
5
+ configurable: true
6
+ });
7
+
8
+ //#endregion
9
+
10
+ Object.defineProperty(exports, '__name', {
11
+ enumerable: true,
12
+ get: function () {
13
+ return __name;
14
+ }
15
+ });
@@ -0,0 +1,9 @@
1
+ //#region \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __name = (target, value) => __defProp(target, "name", {
4
+ value,
5
+ configurable: true
6
+ });
7
+
8
+ //#endregion
9
+ export { __name as t };
@@ -0,0 +1,8 @@
1
+ import { t as __name } from "./chunk-Dco9itRc.mjs";
2
+
3
+ //#region src/common.d.ts
4
+ type BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S;
5
+ type Prettify<T> = { [K in keyof T]: T[K] } & {};
6
+ //#endregion
7
+ export { Prettify as n, BeforeFirstUnderscore as t };
8
+ //# sourceMappingURL=common--ZfG2Kuo.d.mts.map
@@ -0,0 +1,7 @@
1
+ //#endregion
2
+ //#region src/common.d.ts
3
+ type BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S;
4
+ type Prettify<T> = { [K in keyof T]: T[K] } & {};
5
+ //#endregion
6
+ export { Prettify as n, __name as r, BeforeFirstUnderscore as t };
7
+ //# sourceMappingURL=common-x9DEqOZ0.d.cts.map
@@ -0,0 +1,104 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ const require_chunk = require('../chunk-Cmkb9WIj.cjs');
3
+ let commander = require("commander");
4
+ let zod_v3 = require("zod/v3");
5
+
6
+ //#region src/zod3/utils.ts
7
+ const zodCore = (zod, fn) => {
8
+ const types = [
9
+ zod_v3.z.ZodDefault,
10
+ zod_v3.z.ZodNullable,
11
+ zod_v3.z.ZodOptional
12
+ ];
13
+ for (const type of types) if (zod instanceof type) return zodCore(zod._def.innerType, fn);
14
+ if (zod instanceof zod_v3.z.ZodEffects) return zodCore(zod._def.schema, fn);
15
+ return fn(zod);
16
+ };
17
+ const zodEnumVals = (zod) => zodCore(zod, (zod) => zod instanceof zod_v3.z.ZodEnum ? zod._def.values : null);
18
+ const zodIsBoolean = (zod) => zodCore(zod, (zod) => zod instanceof zod_v3.z.ZodBoolean);
19
+ const zodDefault = (zod) => zod instanceof zod_v3.z.ZodEffects ? zodDefault(zod._def.schema) : zod instanceof zod_v3.z.ZodDefault ? zod._def.defaultValue() : void 0;
20
+ const utils = {
21
+ zodCore,
22
+ zodEnumVals,
23
+ zodIsBoolean,
24
+ zodDefault
25
+ };
26
+
27
+ //#endregion
28
+ //#region src/zod3/index.ts
29
+ /**
30
+ * Utilities for building type-safe Commander commands using Zod schemas.
31
+ * @module
32
+ */
33
+ const zodParser = (zod, opt) => (value) => {
34
+ const result = zod.safeParse(value);
35
+ if (result.success) return result.data;
36
+ const msg = result.error.issues[0].message;
37
+ if (opt) throw new commander.InvalidOptionArgumentError(msg);
38
+ throw new commander.InvalidArgumentError(msg);
39
+ };
40
+ /**
41
+ * Creates a Commander.js Argument from a Zod schema.
42
+ * Handles optionality, default values, and enum choices.
43
+ * @param key - The argument name
44
+ * @param zod - The Zod schema for the argument
45
+ * @returns A Commander Argument instance
46
+ */
47
+ const zodArgument = (key, zod) => {
48
+ const arg = new commander.Argument(zod.isOptional() ? `[${key}]` : `<${key}>`, zod.description);
49
+ const def = utils.zodDefault(zod);
50
+ if (def !== void 0) arg.default(zod.parse(def));
51
+ const choices = utils.zodEnumVals(zod);
52
+ if (choices) arg.choices(choices);
53
+ return arg.argParser(zodParser(zod));
54
+ };
55
+ /**
56
+ * Creates a Commander.js Option from a Zod schema.
57
+ * Handles optionality, default values, enum choices, and boolean flags.
58
+ * Supports short flags via description prefix (e.g., 's;...').
59
+ * @param key - The option name (can include underscores for grouping)
60
+ * @param zod - The Zod schema for the option
61
+ * @returns A Commander Option instance
62
+ */
63
+ const zodOption = (key, zod) => {
64
+ const abbr = zod.description?.match(/^(\w);/)?.[1];
65
+ const description = abbr ? zod.description.slice(2) : zod.description;
66
+ const arg = key.includes("_") ? key.split("_").slice(1).join("-") : key;
67
+ if (key.includes("_")) [key] = key.split("_");
68
+ const isBoolean = utils.zodIsBoolean(zod);
69
+ const flag = `--${key}${isBoolean ? "" : ` <${arg}>`}`;
70
+ const opt = new commander.Option(abbr ? `-${abbr}, ${flag}` : flag, description);
71
+ if (isBoolean) opt.optional = true;
72
+ else if (!zod.isOptional()) opt.makeOptionMandatory();
73
+ const def = utils.zodDefault(zod);
74
+ if (def !== void 0) opt.default(zod.parse(def));
75
+ const choices = utils.zodEnumVals(zod);
76
+ if (choices) opt.choices(choices);
77
+ return opt.argParser(zodParser(zod, "opt"));
78
+ };
79
+ /**
80
+ * Defines a Commander.js Command using Zod schemas for arguments and options.
81
+ * Automatically wires up parsing, validation, and help configuration.
82
+ * @template A - ZodRawShape for arguments
83
+ * @template O - ZodRawShape for options
84
+ * @param props - Command properties (name, description, args, opts, action)
85
+ * @returns A Commander Command instance
86
+ */
87
+ const zodCommand = ({ name, description, args, opts, action }) => {
88
+ const command = new commander.Command(name);
89
+ if (description) command.description(description);
90
+ for (const key in args) command.addArgument(zodArgument(key, args[key]));
91
+ for (const key in opts) command.addOption(zodOption(key, opts[key]));
92
+ if (action) command.action(async (...all) => {
93
+ const resultArgs = Object.fromEntries(Object.keys(args ?? {}).map((key, i) => [key, all[i]]));
94
+ const resultOpts = all[Object.keys(args ?? {}).length];
95
+ await action(resultArgs, resultOpts);
96
+ });
97
+ return command;
98
+ };
99
+
100
+ //#endregion
101
+ exports.zodArgument = zodArgument;
102
+ exports.zodCommand = zodCommand;
103
+ exports.zodOption = zodOption;
104
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["z","InvalidOptionArgumentError","InvalidArgumentError","Argument","Option","Command"],"sources":["../../src/zod3/utils.ts","../../src/zod3/index.ts"],"sourcesContent":["import { z } from 'zod/v3'\n\nconst zodCore = <T>(zod: z.ZodTypeAny, fn: (zod: z.ZodTypeAny) => T): T => {\n\tconst types = [z.ZodDefault, z.ZodNullable, z.ZodOptional]\n\tfor (const type of types)\n\t\tif (zod instanceof type) return zodCore(zod._def.innerType, fn)\n\tif (zod instanceof z.ZodEffects) return zodCore(zod._def.schema, fn)\n\treturn fn(zod)\n}\n\nconst zodEnumVals = (zod: z.ZodTypeAny): string[] | null =>\n\tzodCore(zod, (zod) => (zod instanceof z.ZodEnum ? zod._def.values : null))\n\nconst zodIsBoolean = (zod: z.ZodTypeAny): boolean =>\n\tzodCore(zod, (zod) => zod instanceof z.ZodBoolean)\n\nconst zodDefault = <Output, Def extends z.ZodTypeDef, Input>(\n\tzod: z.ZodType<Output, Def, Input>,\n): Input | undefined =>\n\tzod instanceof z.ZodEffects\n\t\t? zodDefault(zod._def.schema)\n\t\t: zod instanceof z.ZodDefault\n\t\t\t? zod._def.defaultValue()\n\t\t\t: undefined\n\nconst utils = {\n\tzodCore,\n\tzodEnumVals,\n\tzodIsBoolean,\n\tzodDefault,\n}\n\nexport default utils\n","/**\n * Utilities for building type-safe Commander commands using Zod schemas.\n * @module\n */\n\nimport {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod/v3'\nimport type { BeforeFirstUnderscore, Prettify } from '#/common.js'\nimport utils from './utils.js'\n\nexport type { Argument, Command, Option } from 'commander'\n\ntype ReplaceKeyTypes<Type extends z.ZodRawShape> = {\n\t[Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key]\n}\n\n/**\n * The action function signature for a Zod-powered command.\n * @template A - ZodRawShape for arguments\n * @template O - ZodRawShape for options\n * @param args - Parsed and validated arguments\n * @param opts - Parsed and validated options (with key normalization)\n * @returns A Promise or void\n */\nexport type ZodCommandAction<\n\tA extends z.ZodRawShape,\n\tO extends z.ZodRawShape,\n> = ZodCommandProps<A, O>['action']\n\ntype ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {\n\tname?: string\n\tdescription?: string\n\targs?: A\n\topts?: O\n\taction?: (\n\t\targs: Prettify<z.infer<z.ZodObject<A>>>,\n\t\topts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>,\n\t) => Promise<void> | void\n}\n\nconst zodParser = (zod: z.ZodTypeAny, opt?: 'opt') => (value: string) => {\n\tconst result = zod.safeParse(value)\n\tif (result.success) return result.data\n\tconst msg = result.error.issues[0].message\n\tif (opt) throw new InvalidOptionArgumentError(msg)\n\tthrow new InvalidArgumentError(msg)\n}\n\n/**\n * Creates a Commander.js Argument from a Zod schema.\n * Handles optionality, default values, and enum choices.\n * @param key - The argument name\n * @param zod - The Zod schema for the argument\n * @returns A Commander Argument instance\n */\nexport const zodArgument = (key: string, zod: z.ZodTypeAny): Argument => {\n\tconst flag = zod.isOptional() ? `[${key}]` : `<${key}>`\n\tconst arg = new Argument(flag, zod.description)\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) arg.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)\n\tif (choices) arg.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn arg.argParser(zodParser(zod))\n}\n\n/**\n * Creates a Commander.js Option from a Zod schema.\n * Handles optionality, default values, enum choices, and boolean flags.\n * Supports short flags via description prefix (e.g., 's;...').\n * @param key - The option name (can include underscores for grouping)\n * @param zod - The Zod schema for the option\n * @returns A Commander Option instance\n */\nexport const zodOption = (key: string, zod: z.ZodTypeAny): Option => {\n\tconst abbr = zod.description?.match(/^(\\w);/)?.[1]\n\tconst description = abbr ? zod.description.slice(2) : zod.description\n\tconst arg = key.includes('_') ? key.split('_').slice(1).join('-') : key\n\tif (key.includes('_')) [key] = key.split('_')\n\tconst isBoolean = utils.zodIsBoolean(zod)\n\tconst flag = `--${key}${isBoolean ? '' : ` <${arg}>`}`\n\tconst flags = abbr ? `-${abbr}, ${flag}` : flag\n\tconst opt = new Option(flags, description)\n\n\t// required for boolean flags\n\tif (isBoolean) opt.optional = true\n\telse if (!zod.isOptional()) opt.makeOptionMandatory()\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) opt.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)\n\tif (choices) opt.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn opt.argParser(zodParser(zod, 'opt'))\n}\n\n/**\n * Defines a Commander.js Command using Zod schemas for arguments and options.\n * Automatically wires up parsing, validation, and help configuration.\n * @template A - ZodRawShape for arguments\n * @template O - ZodRawShape for options\n * @param props - Command properties (name, description, args, opts, action)\n * @returns A Commander Command instance\n */\nexport const zodCommand = <A extends z.ZodRawShape, O extends z.ZodRawShape>({\n\tname,\n\tdescription,\n\targs,\n\topts,\n\taction,\n}: ZodCommandProps<A, O>): Command => {\n\tconst command = new Command(name)\n\tif (description) command.description(description)\n\tfor (const key in args) command.addArgument(zodArgument(key, args[key]))\n\tfor (const key in opts) command.addOption(zodOption(key, opts[key]))\n\tif (action)\n\t\tcommand.action(async (...all) => {\n\t\t\tconst resultArgs = Object.fromEntries(\n\t\t\t\tObject.keys(args ?? {}).map((key, i) => [key, all[i]]),\n\t\t\t) as z.infer<z.ZodObject<A>>\n\t\t\tconst resultOpts = all[Object.keys(args ?? {}).length] as z.infer<\n\t\t\t\tz.ZodObject<ReplaceKeyTypes<O>>\n\t\t\t>\n\t\t\tawait action(resultArgs, resultOpts)\n\t\t})\n\treturn command\n}\n"],"mappings":";;;;;;AAEA,MAAM,WAAc,KAAmB,OAAoC;CAC1E,MAAM,QAAQ;EAACA,SAAE;EAAYA,SAAE;EAAaA,SAAE;EAAY;AAC1D,MAAK,MAAM,QAAQ,MAClB,KAAI,eAAe,KAAM,QAAO,QAAQ,IAAI,KAAK,WAAW,GAAG;AAChE,KAAI,eAAeA,SAAE,WAAY,QAAO,QAAQ,IAAI,KAAK,QAAQ,GAAG;AACpE,QAAO,GAAG,IAAI;;AAGf,MAAM,eAAe,QACpB,QAAQ,MAAM,QAAS,eAAeA,SAAE,UAAU,IAAI,KAAK,SAAS,KAAM;AAE3E,MAAM,gBAAgB,QACrB,QAAQ,MAAM,QAAQ,eAAeA,SAAE,WAAW;AAEnD,MAAM,cACL,QAEA,eAAeA,SAAE,aACd,WAAW,IAAI,KAAK,OAAO,GAC3B,eAAeA,SAAE,aAChB,IAAI,KAAK,cAAc,GACvB;AAEL,MAAM,QAAQ;CACb;CACA;CACA;CACA;CACA;;;;;;;;ACgBD,MAAM,aAAa,KAAmB,SAAiB,UAAkB;CACxE,MAAM,SAAS,IAAI,UAAU,MAAM;AACnC,KAAI,OAAO,QAAS,QAAO,OAAO;CAClC,MAAM,MAAM,OAA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M,CAAC;;;;;;;;;;AAW5C,MAAa,cAAgE,EAC5E,MACA,aACA,MACA,MACA,aACqC;CACrC,MAAM,UAAU,IAAIC,kBAAQ,KAAK;AACjC,KAAI,YAAa,SAAQ,YAAY,YAAY;AACjD,MAAK,MAAM,OAAO,KAAM,SAAQ,YAAY,YAAY,KAAK,KAAK,KAAK,CAAC;AACxE,MAAK,MAAM,OAAO,KAAM,SAAQ,UAAU,UAAU,KAAK,KAAK,KAAK,CAAC;AACpE,KAAI,OACH,SAAQ,OAAO,OAAO,GAAG,QAAQ;EAChC,MAAM,aAAa,OAAO,YACzB,OAAO,KAAK,QAAQ,EAAE,CAAC,CAAC,KAAK,KAAK,MAAM,CAAC,KAAK,IAAI,GAAG,CAAC,CACtD;EACD,MAAM,aAAa,IAAI,OAAO,KAAK,QAAQ,EAAE,CAAC,CAAC;AAG/C,QAAM,OAAO,YAAY,WAAW;GACnC;AACH,QAAO"}
@@ -1,13 +1,9 @@
1
- import { Argument, Option, Command } from 'commander';
2
- import { z } from 'zod';
1
+ import { n as Prettify, r as __name, t as BeforeFirstUnderscore } from "../common-x9DEqOZ0.cjs";
2
+ import { Argument, Argument as Argument$1, Command, Command as Command$1, Option, Option as Option$1 } from "commander";
3
+ import { z } from "zod/v3";
3
4
 
4
- type BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S;
5
- type ReplaceKeyTypes<Type extends z.ZodRawShape> = {
6
- [Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key];
7
- };
8
- type Prettify<T> = {
9
- [K in keyof T]: T[K];
10
- } & {};
5
+ //#region src/zod3/index.d.ts
6
+ type ReplaceKeyTypes<Type extends z.ZodRawShape> = { [Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key] };
11
7
  /**
12
8
  * The action function signature for a Zod-powered command.
13
9
  * @template A - ZodRawShape for arguments
@@ -18,11 +14,11 @@ type Prettify<T> = {
18
14
  */
19
15
  type ZodCommandAction<A extends z.ZodRawShape, O extends z.ZodRawShape> = ZodCommandProps<A, O>['action'];
20
16
  type ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {
21
- name?: string;
22
- description?: string;
23
- args?: A;
24
- opts?: O;
25
- action?: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
17
+ name?: string;
18
+ description?: string;
19
+ args?: A;
20
+ opts?: O;
21
+ action?: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
26
22
  };
27
23
  /**
28
24
  * Creates a Commander.js Argument from a Zod schema.
@@ -31,7 +27,7 @@ type ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {
31
27
  * @param zod - The Zod schema for the argument
32
28
  * @returns A Commander Argument instance
33
29
  */
34
- declare const zodArgument: (key: string, zod: z.ZodTypeAny) => Argument;
30
+ declare const zodArgument: (key: string, zod: z.ZodTypeAny) => Argument$1;
35
31
  /**
36
32
  * Creates a Commander.js Option from a Zod schema.
37
33
  * Handles optionality, default values, enum choices, and boolean flags.
@@ -40,7 +36,7 @@ declare const zodArgument: (key: string, zod: z.ZodTypeAny) => Argument;
40
36
  * @param zod - The Zod schema for the option
41
37
  * @returns A Commander Option instance
42
38
  */
43
- declare const zodOption: (key: string, zod: z.ZodTypeAny) => Option;
39
+ declare const zodOption: (key: string, zod: z.ZodTypeAny) => Option$1;
44
40
  /**
45
41
  * Defines a Commander.js Command using Zod schemas for arguments and options.
46
42
  * Automatically wires up parsing, validation, and help configuration.
@@ -49,6 +45,13 @@ declare const zodOption: (key: string, zod: z.ZodTypeAny) => Option;
49
45
  * @param props - Command properties (name, description, args, opts, action)
50
46
  * @returns A Commander Command instance
51
47
  */
52
- declare const zodCommand: <A extends z.ZodRawShape, O extends z.ZodRawShape>({ name, description, args, opts, action, }: ZodCommandProps<A, O>) => Command;
53
-
54
- export { type ZodCommandAction, zodArgument, zodCommand, zodOption };
48
+ declare const zodCommand: <A extends z.ZodRawShape, O extends z.ZodRawShape>({
49
+ name,
50
+ description,
51
+ args,
52
+ opts,
53
+ action
54
+ }: ZodCommandProps<A, O>) => Command$1;
55
+ //#endregion
56
+ export { type Argument, type Command, type Option, ZodCommandAction, zodArgument, zodCommand, zodOption };
57
+ //# sourceMappingURL=index.d.cts.map
@@ -1,13 +1,10 @@
1
- import { Argument, Option, Command } from 'commander';
2
- import { z } from 'zod';
1
+ import { t as __name } from "../chunk-Dco9itRc.mjs";
2
+ import { n as Prettify, t as BeforeFirstUnderscore } from "../common--ZfG2Kuo.mjs";
3
+ import { Argument, Argument as Argument$1, Command, Command as Command$1, Option, Option as Option$1 } from "commander";
4
+ import { z } from "zod/v3";
3
5
 
4
- type BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S;
5
- type ReplaceKeyTypes<Type extends z.ZodRawShape> = {
6
- [Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key];
7
- };
8
- type Prettify<T> = {
9
- [K in keyof T]: T[K];
10
- } & {};
6
+ //#region src/zod3/index.d.ts
7
+ type ReplaceKeyTypes<Type extends z.ZodRawShape> = { [Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key] };
11
8
  /**
12
9
  * The action function signature for a Zod-powered command.
13
10
  * @template A - ZodRawShape for arguments
@@ -18,11 +15,11 @@ type Prettify<T> = {
18
15
  */
19
16
  type ZodCommandAction<A extends z.ZodRawShape, O extends z.ZodRawShape> = ZodCommandProps<A, O>['action'];
20
17
  type ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {
21
- name?: string;
22
- description?: string;
23
- args?: A;
24
- opts?: O;
25
- action?: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
18
+ name?: string;
19
+ description?: string;
20
+ args?: A;
21
+ opts?: O;
22
+ action?: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
26
23
  };
27
24
  /**
28
25
  * Creates a Commander.js Argument from a Zod schema.
@@ -31,7 +28,7 @@ type ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {
31
28
  * @param zod - The Zod schema for the argument
32
29
  * @returns A Commander Argument instance
33
30
  */
34
- declare const zodArgument: (key: string, zod: z.ZodTypeAny) => Argument;
31
+ declare const zodArgument: (key: string, zod: z.ZodTypeAny) => Argument$1;
35
32
  /**
36
33
  * Creates a Commander.js Option from a Zod schema.
37
34
  * Handles optionality, default values, enum choices, and boolean flags.
@@ -40,7 +37,7 @@ declare const zodArgument: (key: string, zod: z.ZodTypeAny) => Argument;
40
37
  * @param zod - The Zod schema for the option
41
38
  * @returns A Commander Option instance
42
39
  */
43
- declare const zodOption: (key: string, zod: z.ZodTypeAny) => Option;
40
+ declare const zodOption: (key: string, zod: z.ZodTypeAny) => Option$1;
44
41
  /**
45
42
  * Defines a Commander.js Command using Zod schemas for arguments and options.
46
43
  * Automatically wires up parsing, validation, and help configuration.
@@ -49,6 +46,13 @@ declare const zodOption: (key: string, zod: z.ZodTypeAny) => Option;
49
46
  * @param props - Command properties (name, description, args, opts, action)
50
47
  * @returns A Commander Command instance
51
48
  */
52
- declare const zodCommand: <A extends z.ZodRawShape, O extends z.ZodRawShape>({ name, description, args, opts, action, }: ZodCommandProps<A, O>) => Command;
53
-
54
- export { type ZodCommandAction, zodArgument, zodCommand, zodOption };
49
+ declare const zodCommand: <A extends z.ZodRawShape, O extends z.ZodRawShape>({
50
+ name,
51
+ description,
52
+ args,
53
+ opts,
54
+ action
55
+ }: ZodCommandProps<A, O>) => Command$1;
56
+ //#endregion
57
+ export { type Argument, type Command, type Option, ZodCommandAction, zodArgument, zodCommand, zodOption };
58
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1,101 @@
1
+ import { t as __name } from "../chunk-Dco9itRc.mjs";
2
+ import { Argument, Command, InvalidArgumentError, InvalidOptionArgumentError, Option } from "commander";
3
+ import { z } from "zod/v3";
4
+
5
+ //#region src/zod3/utils.ts
6
+ const zodCore = (zod, fn) => {
7
+ const types = [
8
+ z.ZodDefault,
9
+ z.ZodNullable,
10
+ z.ZodOptional
11
+ ];
12
+ for (const type of types) if (zod instanceof type) return zodCore(zod._def.innerType, fn);
13
+ if (zod instanceof z.ZodEffects) return zodCore(zod._def.schema, fn);
14
+ return fn(zod);
15
+ };
16
+ const zodEnumVals = (zod) => zodCore(zod, (zod) => zod instanceof z.ZodEnum ? zod._def.values : null);
17
+ const zodIsBoolean = (zod) => zodCore(zod, (zod) => zod instanceof z.ZodBoolean);
18
+ const zodDefault = (zod) => zod instanceof z.ZodEffects ? zodDefault(zod._def.schema) : zod instanceof z.ZodDefault ? zod._def.defaultValue() : void 0;
19
+ const utils = {
20
+ zodCore,
21
+ zodEnumVals,
22
+ zodIsBoolean,
23
+ zodDefault
24
+ };
25
+
26
+ //#endregion
27
+ //#region src/zod3/index.ts
28
+ /**
29
+ * Utilities for building type-safe Commander commands using Zod schemas.
30
+ * @module
31
+ */
32
+ const zodParser = (zod, opt) => (value) => {
33
+ const result = zod.safeParse(value);
34
+ if (result.success) return result.data;
35
+ const msg = result.error.issues[0].message;
36
+ if (opt) throw new InvalidOptionArgumentError(msg);
37
+ throw new InvalidArgumentError(msg);
38
+ };
39
+ /**
40
+ * Creates a Commander.js Argument from a Zod schema.
41
+ * Handles optionality, default values, and enum choices.
42
+ * @param key - The argument name
43
+ * @param zod - The Zod schema for the argument
44
+ * @returns A Commander Argument instance
45
+ */
46
+ const zodArgument = (key, zod) => {
47
+ const arg = new Argument(zod.isOptional() ? `[${key}]` : `<${key}>`, zod.description);
48
+ const def = utils.zodDefault(zod);
49
+ if (def !== void 0) arg.default(zod.parse(def));
50
+ const choices = utils.zodEnumVals(zod);
51
+ if (choices) arg.choices(choices);
52
+ return arg.argParser(zodParser(zod));
53
+ };
54
+ /**
55
+ * Creates a Commander.js Option from a Zod schema.
56
+ * Handles optionality, default values, enum choices, and boolean flags.
57
+ * Supports short flags via description prefix (e.g., 's;...').
58
+ * @param key - The option name (can include underscores for grouping)
59
+ * @param zod - The Zod schema for the option
60
+ * @returns A Commander Option instance
61
+ */
62
+ const zodOption = (key, zod) => {
63
+ const abbr = zod.description?.match(/^(\w);/)?.[1];
64
+ const description = abbr ? zod.description.slice(2) : zod.description;
65
+ const arg = key.includes("_") ? key.split("_").slice(1).join("-") : key;
66
+ if (key.includes("_")) [key] = key.split("_");
67
+ const isBoolean = utils.zodIsBoolean(zod);
68
+ const flag = `--${key}${isBoolean ? "" : ` <${arg}>`}`;
69
+ const opt = new Option(abbr ? `-${abbr}, ${flag}` : flag, description);
70
+ if (isBoolean) opt.optional = true;
71
+ else if (!zod.isOptional()) opt.makeOptionMandatory();
72
+ const def = utils.zodDefault(zod);
73
+ if (def !== void 0) opt.default(zod.parse(def));
74
+ const choices = utils.zodEnumVals(zod);
75
+ if (choices) opt.choices(choices);
76
+ return opt.argParser(zodParser(zod, "opt"));
77
+ };
78
+ /**
79
+ * Defines a Commander.js Command using Zod schemas for arguments and options.
80
+ * Automatically wires up parsing, validation, and help configuration.
81
+ * @template A - ZodRawShape for arguments
82
+ * @template O - ZodRawShape for options
83
+ * @param props - Command properties (name, description, args, opts, action)
84
+ * @returns A Commander Command instance
85
+ */
86
+ const zodCommand = ({ name, description, args, opts, action }) => {
87
+ const command = new Command(name);
88
+ if (description) command.description(description);
89
+ for (const key in args) command.addArgument(zodArgument(key, args[key]));
90
+ for (const key in opts) command.addOption(zodOption(key, opts[key]));
91
+ if (action) command.action(async (...all) => {
92
+ const resultArgs = Object.fromEntries(Object.keys(args ?? {}).map((key, i) => [key, all[i]]));
93
+ const resultOpts = all[Object.keys(args ?? {}).length];
94
+ await action(resultArgs, resultOpts);
95
+ });
96
+ return command;
97
+ };
98
+
99
+ //#endregion
100
+ export { zodArgument, zodCommand, zodOption };
101
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../src/zod3/utils.ts","../../src/zod3/index.ts"],"sourcesContent":["import { z } from 'zod/v3'\n\nconst zodCore = <T>(zod: z.ZodTypeAny, fn: (zod: z.ZodTypeAny) => T): T => {\n\tconst types = [z.ZodDefault, z.ZodNullable, z.ZodOptional]\n\tfor (const type of types)\n\t\tif (zod instanceof type) return zodCore(zod._def.innerType, fn)\n\tif (zod instanceof z.ZodEffects) return zodCore(zod._def.schema, fn)\n\treturn fn(zod)\n}\n\nconst zodEnumVals = (zod: z.ZodTypeAny): string[] | null =>\n\tzodCore(zod, (zod) => (zod instanceof z.ZodEnum ? zod._def.values : null))\n\nconst zodIsBoolean = (zod: z.ZodTypeAny): boolean =>\n\tzodCore(zod, (zod) => zod instanceof z.ZodBoolean)\n\nconst zodDefault = <Output, Def extends z.ZodTypeDef, Input>(\n\tzod: z.ZodType<Output, Def, Input>,\n): Input | undefined =>\n\tzod instanceof z.ZodEffects\n\t\t? zodDefault(zod._def.schema)\n\t\t: zod instanceof z.ZodDefault\n\t\t\t? zod._def.defaultValue()\n\t\t\t: undefined\n\nconst utils = {\n\tzodCore,\n\tzodEnumVals,\n\tzodIsBoolean,\n\tzodDefault,\n}\n\nexport default utils\n","/**\n * Utilities for building type-safe Commander commands using Zod schemas.\n * @module\n */\n\nimport {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod/v3'\nimport type { BeforeFirstUnderscore, Prettify } from '#/common.js'\nimport utils from './utils.js'\n\nexport type { Argument, Command, Option } from 'commander'\n\ntype ReplaceKeyTypes<Type extends z.ZodRawShape> = {\n\t[Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key]\n}\n\n/**\n * The action function signature for a Zod-powered command.\n * @template A - ZodRawShape for arguments\n * @template O - ZodRawShape for options\n * @param args - Parsed and validated arguments\n * @param opts - Parsed and validated options (with key normalization)\n * @returns A Promise or void\n */\nexport type ZodCommandAction<\n\tA extends z.ZodRawShape,\n\tO extends z.ZodRawShape,\n> = ZodCommandProps<A, O>['action']\n\ntype ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {\n\tname?: string\n\tdescription?: string\n\targs?: A\n\topts?: O\n\taction?: (\n\t\targs: Prettify<z.infer<z.ZodObject<A>>>,\n\t\topts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>,\n\t) => Promise<void> | void\n}\n\nconst zodParser = (zod: z.ZodTypeAny, opt?: 'opt') => (value: string) => {\n\tconst result = zod.safeParse(value)\n\tif (result.success) return result.data\n\tconst msg = result.error.issues[0].message\n\tif (opt) throw new InvalidOptionArgumentError(msg)\n\tthrow new InvalidArgumentError(msg)\n}\n\n/**\n * Creates a Commander.js Argument from a Zod schema.\n * Handles optionality, default values, and enum choices.\n * @param key - The argument name\n * @param zod - The Zod schema for the argument\n * @returns A Commander Argument instance\n */\nexport const zodArgument = (key: string, zod: z.ZodTypeAny): Argument => {\n\tconst flag = zod.isOptional() ? `[${key}]` : `<${key}>`\n\tconst arg = new Argument(flag, zod.description)\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) arg.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)\n\tif (choices) arg.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn arg.argParser(zodParser(zod))\n}\n\n/**\n * Creates a Commander.js Option from a Zod schema.\n * Handles optionality, default values, enum choices, and boolean flags.\n * Supports short flags via description prefix (e.g., 's;...').\n * @param key - The option name (can include underscores for grouping)\n * @param zod - The Zod schema for the option\n * @returns A Commander Option instance\n */\nexport const zodOption = (key: string, zod: z.ZodTypeAny): Option => {\n\tconst abbr = zod.description?.match(/^(\\w);/)?.[1]\n\tconst description = abbr ? zod.description.slice(2) : zod.description\n\tconst arg = key.includes('_') ? key.split('_').slice(1).join('-') : key\n\tif (key.includes('_')) [key] = key.split('_')\n\tconst isBoolean = utils.zodIsBoolean(zod)\n\tconst flag = `--${key}${isBoolean ? '' : ` <${arg}>`}`\n\tconst flags = abbr ? `-${abbr}, ${flag}` : flag\n\tconst opt = new Option(flags, description)\n\n\t// required for boolean flags\n\tif (isBoolean) opt.optional = true\n\telse if (!zod.isOptional()) opt.makeOptionMandatory()\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) opt.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)\n\tif (choices) opt.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn opt.argParser(zodParser(zod, 'opt'))\n}\n\n/**\n * Defines a Commander.js Command using Zod schemas for arguments and options.\n * Automatically wires up parsing, validation, and help configuration.\n * @template A - ZodRawShape for arguments\n * @template O - ZodRawShape for options\n * @param props - Command properties (name, description, args, opts, action)\n * @returns A Commander Command instance\n */\nexport const zodCommand = <A extends z.ZodRawShape, O extends z.ZodRawShape>({\n\tname,\n\tdescription,\n\targs,\n\topts,\n\taction,\n}: ZodCommandProps<A, O>): Command => {\n\tconst command = new Command(name)\n\tif (description) command.description(description)\n\tfor (const key in args) command.addArgument(zodArgument(key, args[key]))\n\tfor (const key in opts) command.addOption(zodOption(key, opts[key]))\n\tif (action)\n\t\tcommand.action(async (...all) => {\n\t\t\tconst resultArgs = Object.fromEntries(\n\t\t\t\tObject.keys(args ?? {}).map((key, i) => [key, all[i]]),\n\t\t\t) as z.infer<z.ZodObject<A>>\n\t\t\tconst resultOpts = all[Object.keys(args ?? {}).length] as z.infer<\n\t\t\t\tz.ZodObject<ReplaceKeyTypes<O>>\n\t\t\t>\n\t\t\tawait action(resultArgs, resultOpts)\n\t\t})\n\treturn command\n}\n"],"mappings":";;;;;AAEA,MAAM,WAAc,KAAmB,OAAoC;CAC1E,MAAM,QAAQ;EAAC,EAAE;EAAY,EAAE;EAAa,EAAE;EAAY;AAC1D,MAAK,MAAM,QAAQ,MAClB,KAAI,eAAe,KAAM,QAAO,QAAQ,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Ij.cjs');
3
+ let commander = require("commander");
4
+ let zod_v4 = require("zod/v4");
5
+
6
+ //#region src/zod4/utils.ts
7
+ const zodCore = (zod, fn) => {
8
+ const types = [
9
+ zod_v4.z.ZodDefault,
10
+ zod_v4.z.ZodPrefault,
11
+ zod_v4.z.ZodNullable,
12
+ zod_v4.z.ZodOptional
13
+ ];
14
+ for (const type of types) if (zod instanceof type) return zodCore(zod.def.innerType, fn);
15
+ if (zod instanceof zod_v4.z.ZodPipe) return zodCore(zod.def.in, fn);
16
+ return fn(zod);
17
+ };
18
+ const zodEnumVals = (zod) => zodCore(zod, (zod) => zod instanceof zod_v4.z.ZodEnum ? zod.options : null);
19
+ const zodIsBoolean = (zod) => zodCore(zod, (zod) => zod instanceof zod_v4.z.ZodBoolean);
20
+ const zodIsOptional = (zod) => zod.safeParse(void 0).success;
21
+ const zodDefault = (zod) => zod instanceof zod_v4.z.ZodPipe ? zodDefault(zod.def.in) : zod instanceof zod_v4.z.ZodPrefault ? zod.def.defaultValue : void 0;
22
+ const utils = {
23
+ zodCore,
24
+ zodEnumVals,
25
+ zodIsBoolean,
26
+ zodIsOptional,
27
+ zodDefault
28
+ };
29
+
30
+ //#endregion
31
+ //#region src/zod4/index.ts
32
+ /**
33
+ * Utilities for building type-safe Commander commands using Zod schemas.
34
+ * @module
35
+ */
36
+ const zodParser = (zod, opt) => (value) => {
37
+ const result = zod.safeParse(value);
38
+ if (result.success) return result.data;
39
+ const msg = result.error.issues[0].message;
40
+ if (opt) throw new commander.InvalidOptionArgumentError(msg);
41
+ throw new commander.InvalidArgumentError(msg);
42
+ };
43
+ /**
44
+ * Creates a Commander.js Argument from a Zod schema.
45
+ * Handles optionality, default values, and enum choices.
46
+ * @param key - The argument name
47
+ * @param zod - The Zod schema for the argument
48
+ * @returns A Commander Argument instance
49
+ */
50
+ const zodArgument = (key, zod) => {
51
+ const arg = new commander.Argument(utils.zodIsOptional(zod) ? `[${key}]` : `<${key}>`, zod.description);
52
+ const def = utils.zodDefault(zod);
53
+ if (def !== void 0) arg.default(zod.parse(def));
54
+ const choices = utils.zodEnumVals(zod)?.map(String);
55
+ if (choices) arg.choices(choices);
56
+ return arg.argParser(zodParser(zod));
57
+ };
58
+ /**
59
+ * Creates a Commander.js Option from a Zod schema.
60
+ * Handles optionality, default values, enum choices, and boolean flags.
61
+ * Supports short flags via description prefix (e.g., 's;...').
62
+ * @param key - The option name (can include underscores for grouping)
63
+ * @param zod - The Zod schema for the option
64
+ * @returns A Commander Option instance
65
+ */
66
+ const zodOption = (key, zod) => {
67
+ const abbr = zod.description?.match(/^(\w);/)?.[1];
68
+ const description = abbr ? zod.description?.slice(2) : zod.description;
69
+ const arg = key.includes("_") ? key.split("_").slice(1).join("-") : key;
70
+ if (key.includes("_")) [key] = key.split("_");
71
+ const isBoolean = utils.zodIsBoolean(zod);
72
+ const flag = `--${key}${isBoolean ? "" : ` <${arg}>`}`;
73
+ const opt = new commander.Option(abbr ? `-${abbr}, ${flag}` : flag, description);
74
+ if (isBoolean) opt.optional = true;
75
+ else if (!utils.zodIsOptional(zod)) opt.makeOptionMandatory();
76
+ const def = utils.zodDefault(zod);
77
+ if (def !== void 0) opt.default(zod.parse(def));
78
+ const choices = utils.zodEnumVals(zod)?.map(String);
79
+ if (choices) opt.choices(choices);
80
+ return opt.argParser(zodParser(zod, "opt"));
81
+ };
82
+ /**
83
+ * Defines a Commander.js Command using Zod schemas for arguments and options.
84
+ * Automatically wires up parsing, validation, and help configuration.
85
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
86
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
87
+ * @param props - Command properties (name, description, args, opts, action)
88
+ * @returns A Commander Command instance
89
+ */
90
+ const zodCommand = ({ name, description, args, opts, action }) => {
91
+ const command = new commander.Command(name);
92
+ if (description) command.description(description);
93
+ for (const key in args) command.addArgument(zodArgument(key, args[key]));
94
+ for (const key in opts) command.addOption(zodOption(key, opts[key]));
95
+ if (action) command.action(async (...all) => {
96
+ const resultArgs = Object.fromEntries(Object.keys(args ?? {}).map((key, i) => [key, all[i]]));
97
+ const resultOpts = all[Object.keys(args ?? {}).length];
98
+ await action(resultArgs, resultOpts);
99
+ });
100
+ return command;
101
+ };
102
+
103
+ //#endregion
104
+ exports.zodArgument = zodArgument;
105
+ exports.zodCommand = zodCommand;
106
+ exports.zodOption = zodOption;
107
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","names":["z","InvalidOptionArgumentError","InvalidArgumentError","Argument","Option","Command"],"sources":["../../src/zod4/utils.ts","../../src/zod4/index.ts"],"sourcesContent":["import { z } from 'zod/v4'\n\nconst zodCore = <T>(\n\tzod: z.core.$ZodType,\n\tfn: (zod: z.core.$ZodType) => T,\n): T => {\n\tconst types = [z.ZodDefault, z.ZodPrefault, z.ZodNullable, z.ZodOptional]\n\tfor (const type of types)\n\t\tif (zod instanceof type) return zodCore(zod.def.innerType, fn)\n\tif (zod instanceof z.ZodPipe) return zodCore(zod.def.in, fn)\n\treturn fn(zod)\n}\n\nconst zodEnumVals = (zod: z.ZodTypeAny): z.core.util.EnumValue[] | null =>\n\tzodCore(zod, (zod) => (zod instanceof z.ZodEnum ? zod.options : null))\n\nconst zodIsBoolean = (zod: z.ZodTypeAny): boolean =>\n\tzodCore(zod, (zod) => zod instanceof z.ZodBoolean)\n\nconst zodIsOptional = (zod: z.ZodType): boolean =>\n\tzod.safeParse(undefined).success\n\nconst zodDefault = <Output, Input>(\n\tzod: z.ZodType<Output, Input>,\n): Input | undefined =>\n\tzod instanceof z.ZodPipe\n\t\t? zodDefault(zod.def.in as z.ZodType<unknown, Input>)\n\t\t: zod instanceof z.ZodPrefault\n\t\t\t? (zod.def.defaultValue as Input)\n\t\t\t: undefined\n\nconst utils = {\n\tzodCore,\n\tzodEnumVals,\n\tzodIsBoolean,\n\tzodIsOptional,\n\tzodDefault,\n}\n\nexport default utils\n","/**\n * Utilities for building type-safe Commander commands using Zod schemas.\n * @module\n */\n\nimport {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod/v4'\nimport type { BeforeFirstUnderscore, Prettify } from '#/common.js'\nimport utils from './utils.js'\n\nexport type { Argument, Command, Option } from 'commander'\n\ntype ReplaceKeyTypes<Type extends z.ZodRawShape> = {\n\t[Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key]\n}\n\n/**\n * The action function signature for a Zod-powered command.\n * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema\n * @template O - Record of key-value pairs where key is the option name and value is the Zod schema\n * @param args - Parsed and validated arguments\n * @param opts - Parsed and validated options (with key normalization)\n * @returns A Promise or void\n */\nexport type ZodCommandAction<\n\tA extends Record<string, z.ZodType>,\n\tO extends Record<string, z.ZodType>,\n> = ZodCommandProps<A, O>['action']\n\ntype ZodCommandProps<\n\tA extends Record<string, z.ZodType>,\n\tO extends Record<string, z.ZodType>,\n> = {\n\tname?: string\n\tdescription?: string\n\targs?: A\n\topts?: O\n\taction?: (\n\t\targs: Prettify<z.infer<z.ZodObject<A>>>,\n\t\topts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>,\n\t) => Promise<void> | void\n}\n\nconst zodParser = (zod: z.ZodType, opt?: 'opt') => (value: string) => {\n\tconst result = zod.safeParse(value)\n\tif (result.success) return result.data\n\tconst msg = result.error.issues[0].message\n\tif (opt) throw new InvalidOptionArgumentError(msg)\n\tthrow new InvalidArgumentError(msg)\n}\n\n/**\n * Creates a Commander.js Argument from a Zod schema.\n * Handles optionality, default values, and enum choices.\n * @param key - The argument name\n * @param zod - The Zod schema for the argument\n * @returns A Commander Argument instance\n */\nexport const zodArgument = (key: string, zod: z.ZodType): Argument => {\n\tconst flag = utils.zodIsOptional(zod) ? `[${key}]` : `<${key}>`\n\tconst arg = new Argument(flag, zod.description)\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) arg.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)?.map(String)\n\tif (choices) arg.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn arg.argParser(zodParser(zod))\n}\n\n/**\n * Creates a Commander.js Option from a Zod schema.\n * Handles optionality, default values, enum choices, and boolean flags.\n * Supports short flags via description prefix (e.g., 's;...').\n * @param key - The option name (can include underscores for grouping)\n * @param zod - The Zod schema for the option\n * @returns A Commander Option instance\n */\nexport const zodOption = (key: string, zod: z.ZodType): Option => {\n\tconst abbr = zod.description?.match(/^(\\w);/)?.[1]\n\tconst description = abbr ? zod.description?.slice(2) : zod.description\n\tconst arg = key.includes('_') ? key.split('_').slice(1).join('-') : key\n\tif (key.includes('_')) [key] = key.split('_')\n\tconst isBoolean = utils.zodIsBoolean(zod)\n\tconst flag = `--${key}${isBoolean ? '' : ` <${arg}>`}`\n\tconst flags = abbr ? `-${abbr}, ${flag}` : flag\n\tconst opt = new Option(flags, description)\n\n\t// required for boolean flags\n\tif (isBoolean) opt.optional = true\n\telse if (!utils.zodIsOptional(zod)) opt.makeOptionMandatory()\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) opt.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)?.map(String)\n\tif (choices) opt.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn opt.argParser(zodParser(zod, 'opt'))\n}\n\n/**\n * Defines a Commander.js Command using Zod schemas for arguments and options.\n * Automatically wires up parsing, validation, and help configuration.\n * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema\n * @template O - Record of key-value pairs where key is the option name and value is the Zod schema\n * @param props - Command properties (name, description, args, opts, action)\n * @returns A Commander Command instance\n */\nexport const zodCommand = <\n\tA extends Record<string, z.ZodType>,\n\tO extends Record<string, z.ZodType>,\n>({\n\tname,\n\tdescription,\n\targs,\n\topts,\n\taction,\n}: ZodCommandProps<A, O>): Command => {\n\tconst command = new Command(name)\n\tif (description) command.description(description)\n\tfor (const key in args) command.addArgument(zodArgument(key, args[key]))\n\tfor (const key in opts) command.addOption(zodOption(key, opts[key]))\n\tif (action)\n\t\tcommand.action(async (...all) => {\n\t\t\tconst resultArgs = Object.fromEntries(\n\t\t\t\tObject.keys(args ?? {}).map((key, i) => [key, all[i]]),\n\t\t\t) as z.infer<z.ZodObject<A>>\n\t\t\tconst resultOpts = all[Object.keys(args ?? {}).length] as z.infer<\n\t\t\t\tz.ZodObject<ReplaceKeyTypes<O>>\n\t\t\t>\n\t\t\tawait action(resultArgs, resultOpts)\n\t\t})\n\treturn command\n}\n"],"mappings":";;;;;;AAEA,MAAM,WACL,KACA,OACO;CACP,MAAM,QAAQ;EAACA,SAAE;EAAYA,SAAE;EAAaA,SAAE;EAAaA,SAAE;EAAY;AACzE,MAAK,MAAM,QAAQ,MAClB,KAAI,eAAe,KAAM,QAAO,QAAQ,IAAI,IAAI,WAAW,GAAG;AAC/D,KAAI,eAAeA,SAAE,QAAS,QAAO,QAAQ,IAAI,IAAI,IAAI,GAAG;AAC5D,QAAO,GAAG,IAAI;;AAGf,MAAM,eAAe,QACpB,QAAQ,MAAM,QAAS,eAAeA,SAAE,UAAU,IAAI,UAAU,KAAM;AAEvE,MAAM,gBAAgB,QACrB,QAAQ,MAAM,QAAQ,eAAeA,SAAE,WAAW;AAEnD,MAAM,iBAAiB,QACtB,IAAI,UAAU,OAAU,CAAC;AAE1B,MAAM,cACL,QAEA,eAAeA,SAAE,UACd,WAAW,IAAI,IAAI,GAAgC,GACnD,eAAeA,SAAE,cACf,IAAI,IAAI,eACT;AAEL,MAAM,QAAQ;CACb;CACA;CACA;CACA;CACA;CACA;;;;;;;;ACYD,MAAM,aAAa,KAAgB,SAAiB,UAAkB;CACrE,MAAM,SAAS,IAAI,UAAU,MAAM;AACnC,KAAI,OAAO,QAAS,QAAO,OAAO;CAClC,MAAM,MAAM,OAAO,MAAM,OAAO,GAAG;AACnC,KAAI,IAAK,OAAM,IAAIC,qCAA2B,IAAI;AAClD,OAAM,IAAIC,+BAAqB,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MACA,aACA,MACA,MACA,aACqC;CACrC,MAAM,UAAU,IAAIC,kBAAQ,KAAK;AACjC,KAAI,YAAa,SAAQ,YAAY,YAAY;AACjD,MAAK,MAAM,OAAO,KAAM,SAAQ,YAAY,YAAY,KAAK,KAAK,KAAK,CAAC;AACxE,MAAK,MAAM,OAAO,KAAM,SAAQ,UAAU,UAAU,KAAK,KAAK,KAAK,CAAC;AACpE,KAAI,OACH,SAAQ,OAAO,OAAO,GAAG,QAAQ;EAChC,MAAM,aAAa,OAAO,YACzB,OAAO,KAAK,QAAQ,EAAE,CAAC,CAAC,KAAK,KAAK,MAAM,CAAC,KAAK,IAAI,GAAG,CAAC,CACtD;EACD,MAAM,aAAa,IAAI,OAAO,KAAK,QAAQ,EAAE,CAAC,CAAC;AAG/C,QAAM,OAAO,YAAY,WAAW;GACnC;AACH,QAAO"}
@@ -0,0 +1,57 @@
1
+ import { n as Prettify, r as __name, t as BeforeFirstUnderscore } from "../common-x9DEqOZ0.cjs";
2
+ import { Argument, Argument as Argument$1, Command, Command as Command$1, Option, Option as Option$1 } from "commander";
3
+ import { z } from "zod/v4";
4
+
5
+ //#region src/zod4/index.d.ts
6
+ type ReplaceKeyTypes<Type extends z.ZodRawShape> = { [Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key] };
7
+ /**
8
+ * The action function signature for a Zod-powered command.
9
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
10
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
11
+ * @param args - Parsed and validated arguments
12
+ * @param opts - Parsed and validated options (with key normalization)
13
+ * @returns A Promise or void
14
+ */
15
+ type ZodCommandAction<A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>> = ZodCommandProps<A, O>['action'];
16
+ type ZodCommandProps<A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>> = {
17
+ name?: string;
18
+ description?: string;
19
+ args?: A;
20
+ opts?: O;
21
+ action?: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
22
+ };
23
+ /**
24
+ * Creates a Commander.js Argument from a Zod schema.
25
+ * Handles optionality, default values, and enum choices.
26
+ * @param key - The argument name
27
+ * @param zod - The Zod schema for the argument
28
+ * @returns A Commander Argument instance
29
+ */
30
+ declare const zodArgument: (key: string, zod: z.ZodType) => Argument$1;
31
+ /**
32
+ * Creates a Commander.js Option from a Zod schema.
33
+ * Handles optionality, default values, enum choices, and boolean flags.
34
+ * Supports short flags via description prefix (e.g., 's;...').
35
+ * @param key - The option name (can include underscores for grouping)
36
+ * @param zod - The Zod schema for the option
37
+ * @returns A Commander Option instance
38
+ */
39
+ declare const zodOption: (key: string, zod: z.ZodType) => Option$1;
40
+ /**
41
+ * Defines a Commander.js Command using Zod schemas for arguments and options.
42
+ * Automatically wires up parsing, validation, and help configuration.
43
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
44
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
45
+ * @param props - Command properties (name, description, args, opts, action)
46
+ * @returns A Commander Command instance
47
+ */
48
+ declare const zodCommand: <A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>>({
49
+ name,
50
+ description,
51
+ args,
52
+ opts,
53
+ action
54
+ }: ZodCommandProps<A, O>) => Command$1;
55
+ //#endregion
56
+ export { type Argument, type Command, type Option, ZodCommandAction, zodArgument, zodCommand, zodOption };
57
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1,58 @@
1
+ import { t as __name } from "../chunk-Dco9itRc.mjs";
2
+ import { n as Prettify, t as BeforeFirstUnderscore } from "../common--ZfG2Kuo.mjs";
3
+ import { Argument, Argument as Argument$1, Command, Command as Command$1, Option, Option as Option$1 } from "commander";
4
+ import { z } from "zod/v4";
5
+
6
+ //#region src/zod4/index.d.ts
7
+ type ReplaceKeyTypes<Type extends z.ZodRawShape> = { [Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key] };
8
+ /**
9
+ * The action function signature for a Zod-powered command.
10
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
11
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
12
+ * @param args - Parsed and validated arguments
13
+ * @param opts - Parsed and validated options (with key normalization)
14
+ * @returns A Promise or void
15
+ */
16
+ type ZodCommandAction<A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>> = ZodCommandProps<A, O>['action'];
17
+ type ZodCommandProps<A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>> = {
18
+ name?: string;
19
+ description?: string;
20
+ args?: A;
21
+ opts?: O;
22
+ action?: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
23
+ };
24
+ /**
25
+ * Creates a Commander.js Argument from a Zod schema.
26
+ * Handles optionality, default values, and enum choices.
27
+ * @param key - The argument name
28
+ * @param zod - The Zod schema for the argument
29
+ * @returns A Commander Argument instance
30
+ */
31
+ declare const zodArgument: (key: string, zod: z.ZodType) => Argument$1;
32
+ /**
33
+ * Creates a Commander.js Option from a Zod schema.
34
+ * Handles optionality, default values, enum choices, and boolean flags.
35
+ * Supports short flags via description prefix (e.g., 's;...').
36
+ * @param key - The option name (can include underscores for grouping)
37
+ * @param zod - The Zod schema for the option
38
+ * @returns A Commander Option instance
39
+ */
40
+ declare const zodOption: (key: string, zod: z.ZodType) => Option$1;
41
+ /**
42
+ * Defines a Commander.js Command using Zod schemas for arguments and options.
43
+ * Automatically wires up parsing, validation, and help configuration.
44
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
45
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
46
+ * @param props - Command properties (name, description, args, opts, action)
47
+ * @returns A Commander Command instance
48
+ */
49
+ declare const zodCommand: <A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>>({
50
+ name,
51
+ description,
52
+ args,
53
+ opts,
54
+ action
55
+ }: ZodCommandProps<A, O>) => Command$1;
56
+ //#endregion
57
+ export { type Argument, type Command, type Option, ZodCommandAction, zodArgument, zodCommand, zodOption };
58
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1,104 @@
1
+ import { t as __name } from "../chunk-Dco9itRc.mjs";
2
+ import { Argument, Command, InvalidArgumentError, InvalidOptionArgumentError, Option } from "commander";
3
+ import { z } from "zod/v4";
4
+
5
+ //#region src/zod4/utils.ts
6
+ const zodCore = (zod, fn) => {
7
+ const types = [
8
+ z.ZodDefault,
9
+ z.ZodPrefault,
10
+ z.ZodNullable,
11
+ z.ZodOptional
12
+ ];
13
+ for (const type of types) if (zod instanceof type) return zodCore(zod.def.innerType, fn);
14
+ if (zod instanceof z.ZodPipe) return zodCore(zod.def.in, fn);
15
+ return fn(zod);
16
+ };
17
+ const zodEnumVals = (zod) => zodCore(zod, (zod) => zod instanceof z.ZodEnum ? zod.options : null);
18
+ const zodIsBoolean = (zod) => zodCore(zod, (zod) => zod instanceof z.ZodBoolean);
19
+ const zodIsOptional = (zod) => zod.safeParse(void 0).success;
20
+ const zodDefault = (zod) => zod instanceof z.ZodPipe ? zodDefault(zod.def.in) : zod instanceof z.ZodPrefault ? zod.def.defaultValue : void 0;
21
+ const utils = {
22
+ zodCore,
23
+ zodEnumVals,
24
+ zodIsBoolean,
25
+ zodIsOptional,
26
+ zodDefault
27
+ };
28
+
29
+ //#endregion
30
+ //#region src/zod4/index.ts
31
+ /**
32
+ * Utilities for building type-safe Commander commands using Zod schemas.
33
+ * @module
34
+ */
35
+ const zodParser = (zod, opt) => (value) => {
36
+ const result = zod.safeParse(value);
37
+ if (result.success) return result.data;
38
+ const msg = result.error.issues[0].message;
39
+ if (opt) throw new InvalidOptionArgumentError(msg);
40
+ throw new InvalidArgumentError(msg);
41
+ };
42
+ /**
43
+ * Creates a Commander.js Argument from a Zod schema.
44
+ * Handles optionality, default values, and enum choices.
45
+ * @param key - The argument name
46
+ * @param zod - The Zod schema for the argument
47
+ * @returns A Commander Argument instance
48
+ */
49
+ const zodArgument = (key, zod) => {
50
+ const arg = new Argument(utils.zodIsOptional(zod) ? `[${key}]` : `<${key}>`, zod.description);
51
+ const def = utils.zodDefault(zod);
52
+ if (def !== void 0) arg.default(zod.parse(def));
53
+ const choices = utils.zodEnumVals(zod)?.map(String);
54
+ if (choices) arg.choices(choices);
55
+ return arg.argParser(zodParser(zod));
56
+ };
57
+ /**
58
+ * Creates a Commander.js Option from a Zod schema.
59
+ * Handles optionality, default values, enum choices, and boolean flags.
60
+ * Supports short flags via description prefix (e.g., 's;...').
61
+ * @param key - The option name (can include underscores for grouping)
62
+ * @param zod - The Zod schema for the option
63
+ * @returns A Commander Option instance
64
+ */
65
+ const zodOption = (key, zod) => {
66
+ const abbr = zod.description?.match(/^(\w);/)?.[1];
67
+ const description = abbr ? zod.description?.slice(2) : zod.description;
68
+ const arg = key.includes("_") ? key.split("_").slice(1).join("-") : key;
69
+ if (key.includes("_")) [key] = key.split("_");
70
+ const isBoolean = utils.zodIsBoolean(zod);
71
+ const flag = `--${key}${isBoolean ? "" : ` <${arg}>`}`;
72
+ const opt = new Option(abbr ? `-${abbr}, ${flag}` : flag, description);
73
+ if (isBoolean) opt.optional = true;
74
+ else if (!utils.zodIsOptional(zod)) opt.makeOptionMandatory();
75
+ const def = utils.zodDefault(zod);
76
+ if (def !== void 0) opt.default(zod.parse(def));
77
+ const choices = utils.zodEnumVals(zod)?.map(String);
78
+ if (choices) opt.choices(choices);
79
+ return opt.argParser(zodParser(zod, "opt"));
80
+ };
81
+ /**
82
+ * Defines a Commander.js Command using Zod schemas for arguments and options.
83
+ * Automatically wires up parsing, validation, and help configuration.
84
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
85
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
86
+ * @param props - Command properties (name, description, args, opts, action)
87
+ * @returns A Commander Command instance
88
+ */
89
+ const zodCommand = ({ name, description, args, opts, action }) => {
90
+ const command = new Command(name);
91
+ if (description) command.description(description);
92
+ for (const key in args) command.addArgument(zodArgument(key, args[key]));
93
+ for (const key in opts) command.addOption(zodOption(key, opts[key]));
94
+ if (action) command.action(async (...all) => {
95
+ const resultArgs = Object.fromEntries(Object.keys(args ?? {}).map((key, i) => [key, all[i]]));
96
+ const resultOpts = all[Object.keys(args ?? {}).length];
97
+ await action(resultArgs, resultOpts);
98
+ });
99
+ return command;
100
+ };
101
+
102
+ //#endregion
103
+ export { zodArgument, zodCommand, zodOption };
104
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../../src/zod4/utils.ts","../../src/zod4/index.ts"],"sourcesContent":["import { z } from 'zod/v4'\n\nconst zodCore = <T>(\n\tzod: z.core.$ZodType,\n\tfn: (zod: z.core.$ZodType) => T,\n): T => {\n\tconst types = [z.ZodDefault, z.ZodPrefault, z.ZodNullable, z.ZodOptional]\n\tfor (const type of types)\n\t\tif (zod instanceof type) return zodCore(zod.def.innerType, fn)\n\tif (zod instanceof z.ZodPipe) return zodCore(zod.def.in, fn)\n\treturn fn(zod)\n}\n\nconst zodEnumVals = (zod: z.ZodTypeAny): z.core.util.EnumValue[] | null =>\n\tzodCore(zod, (zod) => (zod instanceof z.ZodEnum ? zod.options : null))\n\nconst zodIsBoolean = (zod: z.ZodTypeAny): boolean =>\n\tzodCore(zod, (zod) => zod instanceof z.ZodBoolean)\n\nconst zodIsOptional = (zod: z.ZodType): boolean =>\n\tzod.safeParse(undefined).success\n\nconst zodDefault = <Output, Input>(\n\tzod: z.ZodType<Output, Input>,\n): Input | undefined =>\n\tzod instanceof z.ZodPipe\n\t\t? zodDefault(zod.def.in as z.ZodType<unknown, Input>)\n\t\t: zod instanceof z.ZodPrefault\n\t\t\t? (zod.def.defaultValue as Input)\n\t\t\t: undefined\n\nconst utils = {\n\tzodCore,\n\tzodEnumVals,\n\tzodIsBoolean,\n\tzodIsOptional,\n\tzodDefault,\n}\n\nexport default utils\n","/**\n * Utilities for building type-safe Commander commands using Zod schemas.\n * @module\n */\n\nimport {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod/v4'\nimport type { BeforeFirstUnderscore, Prettify } from '#/common.js'\nimport utils from './utils.js'\n\nexport type { Argument, Command, Option } from 'commander'\n\ntype ReplaceKeyTypes<Type extends z.ZodRawShape> = {\n\t[Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key]\n}\n\n/**\n * The action function signature for a Zod-powered command.\n * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema\n * @template O - Record of key-value pairs where key is the option name and value is the Zod schema\n * @param args - Parsed and validated arguments\n * @param opts - Parsed and validated options (with key normalization)\n * @returns A Promise or void\n */\nexport type ZodCommandAction<\n\tA extends Record<string, z.ZodType>,\n\tO extends Record<string, z.ZodType>,\n> = ZodCommandProps<A, O>['action']\n\ntype ZodCommandProps<\n\tA extends Record<string, z.ZodType>,\n\tO extends Record<string, z.ZodType>,\n> = {\n\tname?: string\n\tdescription?: string\n\targs?: A\n\topts?: O\n\taction?: (\n\t\targs: Prettify<z.infer<z.ZodObject<A>>>,\n\t\topts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>,\n\t) => Promise<void> | void\n}\n\nconst zodParser = (zod: z.ZodType, opt?: 'opt') => (value: string) => {\n\tconst result = zod.safeParse(value)\n\tif (result.success) return result.data\n\tconst msg = result.error.issues[0].message\n\tif (opt) throw new InvalidOptionArgumentError(msg)\n\tthrow new InvalidArgumentError(msg)\n}\n\n/**\n * Creates a Commander.js Argument from a Zod schema.\n * Handles optionality, default values, and enum choices.\n * @param key - The argument name\n * @param zod - The Zod schema for the argument\n * @returns A Commander Argument instance\n */\nexport const zodArgument = (key: string, zod: z.ZodType): Argument => {\n\tconst flag = utils.zodIsOptional(zod) ? `[${key}]` : `<${key}>`\n\tconst arg = new Argument(flag, zod.description)\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) arg.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)?.map(String)\n\tif (choices) arg.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn arg.argParser(zodParser(zod))\n}\n\n/**\n * Creates a Commander.js Option from a Zod schema.\n * Handles optionality, default values, enum choices, and boolean flags.\n * Supports short flags via description prefix (e.g., 's;...').\n * @param key - The option name (can include underscores for grouping)\n * @param zod - The Zod schema for the option\n * @returns A Commander Option instance\n */\nexport const zodOption = (key: string, zod: z.ZodType): Option => {\n\tconst abbr = zod.description?.match(/^(\\w);/)?.[1]\n\tconst description = abbr ? zod.description?.slice(2) : zod.description\n\tconst arg = key.includes('_') ? key.split('_').slice(1).join('-') : key\n\tif (key.includes('_')) [key] = key.split('_')\n\tconst isBoolean = utils.zodIsBoolean(zod)\n\tconst flag = `--${key}${isBoolean ? '' : ` <${arg}>`}`\n\tconst flags = abbr ? `-${abbr}, ${flag}` : flag\n\tconst opt = new Option(flags, description)\n\n\t// required for boolean flags\n\tif (isBoolean) opt.optional = true\n\telse if (!utils.zodIsOptional(zod)) opt.makeOptionMandatory()\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) opt.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)?.map(String)\n\tif (choices) opt.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn opt.argParser(zodParser(zod, 'opt'))\n}\n\n/**\n * Defines a Commander.js Command using Zod schemas for arguments and options.\n * Automatically wires up parsing, validation, and help configuration.\n * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema\n * @template O - Record of key-value pairs where key is the option name and value is the Zod schema\n * @param props - Command properties (name, description, args, opts, action)\n * @returns A Commander Command instance\n */\nexport const zodCommand = <\n\tA extends Record<string, z.ZodType>,\n\tO extends Record<string, z.ZodType>,\n>({\n\tname,\n\tdescription,\n\targs,\n\topts,\n\taction,\n}: ZodCommandProps<A, O>): Command => {\n\tconst command = new Command(name)\n\tif (description) command.description(description)\n\tfor (const key in args) command.addArgument(zodArgument(key, args[key]))\n\tfor (const key in opts) command.addOption(zodOption(key, opts[key]))\n\tif (action)\n\t\tcommand.action(async (...all) => {\n\t\t\tconst resultArgs = Object.fromEntries(\n\t\t\t\tObject.keys(args ?? {}).map((key, i) => [key, all[i]]),\n\t\t\t) as z.infer<z.ZodObject<A>>\n\t\t\tconst resultOpts = all[Object.keys(args ?? {}).length] as z.infer<\n\t\t\t\tz.ZodObject<ReplaceKeyTypes<O>>\n\t\t\t>\n\t\t\tawait action(resultArgs, resultOpts)\n\t\t})\n\treturn command\n}\n"],"mappings":";;;;;AAEA,MAAM,WACL,KACA,OACO;CACP,MAAM,QAAQ;EAAC,EAAE;EAAY,EAAE;EAAa,EAAE;EAAa,EAAE;EAAY;AACzE,MAAK,MAAM,QAAQ,MAClB,KAAI,eAAe,KAAM,QAAO,QAAQ,IAAI,IAAI,WAAW,GAAG;AAC/D,KAAI,eAAe,EAAE,QAAS,QAAO,QAAQ,IAAI,IAAI,IAAI,GAAG;AAC5D,QAAO,GAAG,IAAI;;AAGf,MAAM,eAAe,QACpB,QAAQ,MAAM,QAAS,eAAe,EAAE,UAAU,IAAI,UAAU,KAAM;AAEvE,MAAM,gBAAgB,QACrB,QAAQ,MAAM,QAAQ,eAAe,EAAE,WAAW;AAEnD,MAAM,iBAAiB,QACtB,IAAI,UAAU,OAAU,CAAC;AAE1B,MAAM,cACL,QAEA,eAAe,EAAE,UACd,WAAW,IAAI,IAAI,GAAgC,GACnD,eAAe,EAAE,cACf,IAAI,IAAI,eACT;AAEL,MAAM,QAAQ;CACb;CACA;CACA;CACA;CACA;CACA;;;;;;;;ACYD,MAAM,aAAa,KAAgB,SAAiB,UAAkB;CACrE,MAAM,SAAS,IAAI,UAAU,MAAM;AACnC,KAAI,OAAO,QAAS,QAAO,OAAO;CAClC,MAAM,MAAM,OAAO,MAAM,OAAO,GAAG;AACnC,KAAI,IAAK,OAAM,IAAI,2BAA2B,IAAI;AAClD,OAAM,IAAI,qBA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MACA,aACA,MACA,MACA,aACqC;CACrC,MAAM,UAAU,IAAI,QAAQ,KAAK;AACjC,KAAI,YAAa,SAAQ,YAAY,YAAY;AACjD,MAAK,MAAM,OAAO,KAAM,SAAQ,YAAY,YAAY,KAAK,KAAK,KAAK,CAAC;AACxE,MAAK,MAAM,OAAO,KAAM,SAAQ,UAAU,UAAU,KAAK,KAAK,KAAK,CAAC;AACpE,KAAI,OACH,SAAQ,OAAO,OAAO,GAAG,QAAQ;EAChC,MAAM,aAAa,OAAO,YACzB,OAAO,KAAK,QAAQ,EAAE,CAAC,CAAC,KAAK,KAAK,MAAM,CAAC,KAAK,IAAI,GAAG,CAAC,CACtD;EACD,MAAM,aAAa,IAAI,OAAO,KAAK,QAAQ,EAAE,CAAC,CAAC;AAG/C,QAAM,OAAO,YAAY,WAAW;GACnC;AACH,QAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-commander",
3
- "version": "0.0.5",
3
+ "version": "0.1.1",
4
4
  "description": "A TypeScript utility for building type-safe CLI commands using commander and zod.",
5
5
  "author": "Román Via-Dufresne Saus <roman910dev@gmail.com>(https://github.com/roman910dev)",
6
6
  "license": "MIT",
@@ -15,49 +15,62 @@
15
15
  "typescript"
16
16
  ],
17
17
  "devDependencies": {
18
- "@biomejs/biome": "^2.0.6",
18
+ "@biomejs/biome": "^2.4.12",
19
19
  "@types/node": "^22.16.3",
20
- "commander": "^14.0.0",
20
+ "commander": "^14.0.3",
21
21
  "husky": "^9.1.7",
22
- "tsup": "^8.5.0",
23
- "typescript": "^5.8.3",
24
- "vitest": "^3.2.4",
25
- "zod": "^3.25.0"
22
+ "tsdown": "^0.21.9",
23
+ "typescript": "^6.0.2",
24
+ "vitest": "^4.1.4",
25
+ "zod": "^4.3.6"
26
26
  },
27
27
  "main": "./dist/index.cjs",
28
28
  "module": "./dist/index.js",
29
29
  "types": "./dist/index.d.ts",
30
30
  "exports": {
31
31
  ".": {
32
- "types": "./dist/index.d.ts",
33
- "import": "./dist/index.js",
34
- "require": "./dist/index.cjs",
35
- "default": "./dist/index.js"
32
+ "types": "./dist/zod3/index.d.ts",
33
+ "import": "./dist/zod3/index.js",
34
+ "require": "./dist/zod3/index.cjs",
35
+ "default": "./dist/zod3/index.js"
36
+ },
37
+ "./zod3": {
38
+ "types": "./dist/zod3/index.d.ts",
39
+ "import": "./dist/zod3/index.js",
40
+ "require": "./dist/zod3/index.cjs",
41
+ "default": "./dist/zod3/index.js"
42
+ },
43
+ "./zod4": {
44
+ "types": "./dist/zod4/index.d.ts",
45
+ "import": "./dist/zod4/index.js",
46
+ "require": "./dist/zod4/index.cjs",
47
+ "default": "./dist/zod4/index.js"
36
48
  }
37
49
  },
38
50
  "files": [
39
51
  "dist/"
40
52
  ],
41
53
  "imports": {
42
- "#/*": "./src/*"
54
+ "#/*": "./src/*",
55
+ "#tests/*": "./tests/*"
43
56
  },
44
57
  "publishConfig": {
45
58
  "access": "public"
46
59
  },
47
60
  "peerDependencies": {
48
61
  "commander": ">=8 <15",
49
- "zod": ">=3.20.0 <4"
62
+ "zod": ">=3.20.0 <5"
50
63
  },
51
64
  "type": "module",
52
65
  "scripts": {
53
- "build": "tsup",
66
+ "build": "tsdown",
54
67
  "check": "biome check .",
55
68
  "fix": "biome check . --write",
56
69
  "format": "biome format . --write",
57
70
  "full-check": "pnpm ts && pnpm check",
58
71
  "lint": "biome lint .",
59
72
  "ci": "biome ci --threads=4",
60
- "test": "vitest",
73
+ "test": "vitest run",
61
74
  "ts": "tsc --noEmit"
62
75
  }
63
76
  }
package/dist/index.cjs DELETED
@@ -1,86 +0,0 @@
1
- 'use strict';
2
-
3
- var commander = require('commander');
4
- var zod = require('zod');
5
-
6
- var __defProp = Object.defineProperty;
7
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
- var zodCore = /* @__PURE__ */ __name((zod$1, fn) => {
9
- const types = [zod.z.ZodDefault, zod.z.ZodNullable, zod.z.ZodOptional];
10
- for (const type of types)
11
- if (zod$1 instanceof type) return zodCore(zod$1._def.innerType, fn);
12
- if (zod$1 instanceof zod.z.ZodEffects) return zodCore(zod$1._def.schema, fn);
13
- return fn(zod$1);
14
- }, "zodCore");
15
- var zodEnumVals = /* @__PURE__ */ __name((zod$1) => zodCore(zod$1, (zod2) => zod2 instanceof zod.z.ZodEnum ? zod2._def.values : null), "zodEnumVals");
16
- var zodIsBoolean = /* @__PURE__ */ __name((zod$1) => zodCore(zod$1, (zod2) => zod2 instanceof zod.z.ZodBoolean), "zodIsBoolean");
17
- var zodDefault = /* @__PURE__ */ __name((zod$1) => zod$1 instanceof zod.z.ZodEffects ? zodDefault(zod$1._def.schema) : zod$1 instanceof zod.z.ZodDefault ? zod$1._def.defaultValue() : void 0, "zodDefault");
18
- var utils = {
19
- zodCore,
20
- zodEnumVals,
21
- zodIsBoolean,
22
- zodDefault
23
- };
24
- var utils_default = utils;
25
-
26
- // src/index.ts
27
- var zodParser = /* @__PURE__ */ __name((zod, opt) => (value) => {
28
- const result = zod.safeParse(value);
29
- if (result.success) return result.data;
30
- const msg = result.error.issues[0].message;
31
- if (opt) throw new commander.InvalidOptionArgumentError(msg);
32
- throw new commander.InvalidArgumentError(msg);
33
- }, "zodParser");
34
- var zodArgument = /* @__PURE__ */ __name((key, zod) => {
35
- const flag = zod.isOptional() ? `[${key}]` : `<${key}>`;
36
- const arg = new commander.Argument(flag, zod.description);
37
- const def = utils_default.zodDefault(zod);
38
- if (def !== void 0) arg.default(zod.parse(def));
39
- const choices = utils_default.zodEnumVals(zod);
40
- if (choices) arg.choices(choices);
41
- return arg.argParser(zodParser(zod));
42
- }, "zodArgument");
43
- var zodOption = /* @__PURE__ */ __name((key, zod) => {
44
- const abbr = zod.description?.match(/^(\w);/)?.[1];
45
- const description = abbr ? zod.description.slice(2) : zod.description;
46
- const arg = key.includes("_") ? key.split("_").slice(1).join("-") : key;
47
- if (key.includes("_")) [key] = key.split("_");
48
- const isBoolean = utils_default.zodIsBoolean(zod);
49
- const flag = `--${key}${isBoolean ? "" : ` <${arg}>`}`;
50
- const flags = abbr ? `-${abbr}, ${flag}` : flag;
51
- const opt = new commander.Option(flags, description);
52
- if (isBoolean) opt.optional = true;
53
- else if (!zod.isOptional()) opt.makeOptionMandatory();
54
- const def = utils_default.zodDefault(zod);
55
- if (def !== void 0) opt.default(zod.parse(def));
56
- const choices = utils_default.zodEnumVals(zod);
57
- if (choices) opt.choices(choices);
58
- return opt.argParser(zodParser(zod, "opt"));
59
- }, "zodOption");
60
- var zodCommand = /* @__PURE__ */ __name(({
61
- name,
62
- description,
63
- args,
64
- opts,
65
- action
66
- }) => {
67
- const command = new commander.Command(name);
68
- if (description) command.description(description);
69
- for (const key in args) command.addArgument(zodArgument(key, args[key]));
70
- for (const key in opts) command.addOption(zodOption(key, opts[key]));
71
- if (action)
72
- command.action(async (...all) => {
73
- const resultArgs = Object.fromEntries(
74
- Object.keys(args ?? {}).map((key, i) => [key, all[i]])
75
- );
76
- const resultOpts = all[Object.keys(args ?? {}).length];
77
- await action(resultArgs, resultOpts);
78
- });
79
- return command;
80
- }, "zodCommand");
81
-
82
- exports.zodArgument = zodArgument;
83
- exports.zodCommand = zodCommand;
84
- exports.zodOption = zodOption;
85
- //# sourceMappingURL=index.cjs.map
86
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils.ts","../src/index.ts"],"names":["zod","z","InvalidOptionArgumentError","InvalidArgumentError","Argument","Option","Command"],"mappings":";;;;;;;AAEA,IAAM,OAAA,mBAAU,MAAA,CAAA,CAAIA,KAAA,EAAmB,EAAA,KAAoC;AAC1E,EAAA,MAAM,QAAQ,CAACC,KAAA,CAAE,YAAYA,KAAA,CAAE,WAAA,EAAaA,MAAE,WAAW,CAAA;AACzD,EAAA,KAAA,MAAW,IAAA,IAAQ,KAAA;AAClB,IAAA,IAAID,iBAAe,IAAA,EAAM,OAAO,QAAQA,KAAA,CAAI,IAAA,CAAK,WAAW,EAAE,CAAA;AAC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A,CAAQ,WAAA,CAAY,WAAW,CAAA;AAChD,EAAA,KAAA,MAAW,GAAA,IAAO,MAAM,OAAA,CAAQ,WAAA,CAAY,YAAY,GAAA,EAAK,IAAA,CAAK,GAAG,CAAC,CAAC,CAAA;AACvE,EAAA,KAAA,MAAW,GAAA,IAAO,MAAM,OAAA,CAAQ,SAAA,CAAU,UAAU,GAAA,EAAK,IAAA,CAAK,GAAG,CAAC,CAAC,CAAA;AACnE,EAAA,IAAI,MAAA;AACH,IAAA,OAAA,CAAQ,MAAA,CAAO,UAAU,GAAA,KAAQ;AAChC,MAAA,MAAM,aAAa,MAAA,CAAO,WAAA;AAAA,QACzB,MAAA,CAAO,IAAA,CAAK,IAAA,IAAQ,EAAE,CAAA,CAAE,GAAA,CAAI,CAAC,GAAA,EAAK,MAAM,CAAC,GAAA,EAAK,GAAA,CAAI,CAAC,CAAC,CAAC;AAAA,OACtD;AACA,MAAA,MAAM,UAAA,GAAa,IAAI,MAAA,CAAO,IAAA,CAAK,QAAQ,EAAE,EAAE,MAAM,CAAA;AAGrD,MAAA,MAAM,MAAA,CAAO,YAAY,UAAU,CAAA;AAAA,IACpC,CAAC,CAAA;AACF,EAAA,OAAO,OAAA;AACR,CAAA,EAtB0B,YAAA","file":"index.cjs","sourcesContent":["import { z } from 'zod'\n\nconst zodCore = <T>(zod: z.ZodTypeAny, fn: (zod: z.ZodTypeAny) => T): T => {\n\tconst types = [z.ZodDefault, z.ZodNullable, z.ZodOptional]\n\tfor (const type of types)\n\t\tif (zod instanceof type) return zodCore(zod._def.innerType, fn)\n\tif (zod instanceof z.ZodEffects) return zodCore(zod._def.schema, fn)\n\treturn fn(zod)\n}\n\nconst zodEnumVals = (zod: z.ZodTypeAny): string[] | null =>\n\tzodCore(zod, (zod) => (zod instanceof z.ZodEnum ? zod._def.values : null))\n\nconst zodIsBoolean = (zod: z.ZodTypeAny): boolean =>\n\tzodCore(zod, (zod) => zod instanceof z.ZodBoolean)\n\nconst zodDefault = <Output, Def extends z.ZodTypeDef, Input>(\n\tzod: z.ZodType<Output, Def, Input>,\n): Input | undefined =>\n\tzod instanceof z.ZodEffects\n\t\t? zodDefault(zod._def.schema)\n\t\t: zod instanceof z.ZodDefault\n\t\t\t? zod._def.defaultValue()\n\t\t\t: undefined\n\nconst utils = {\n\tzodCore,\n\tzodEnumVals,\n\tzodIsBoolean,\n\tzodDefault,\n}\n\nexport default utils\n","import {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod'\nimport utils from './utils.js'\n\ntype BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S\n\ntype ReplaceKeyTypes<Type extends z.ZodRawShape> = {\n\t[Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key]\n}\n\ntype Prettify<T> = {\n\t[K in keyof T]: T[K]\n} & {}\n\n/**\n * The action function signature for a Zod-powered command.\n * @template A - ZodRawShape for arguments\n * @template O - ZodRawShape for options\n * @param args - Parsed and validated arguments\n * @param opts - Parsed and validated options (with key normalization)\n * @returns A Promise or void\n */\nexport type ZodCommandAction<\n\tA extends z.ZodRawShape,\n\tO extends z.ZodRawShape,\n> = ZodCommandProps<A, O>['action']\n\ntype ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {\n\tname?: string\n\tdescription?: string\n\targs?: A\n\topts?: O\n\taction?: (\n\t\targs: Prettify<z.infer<z.ZodObject<A>>>,\n\t\topts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>,\n\t) => Promise<void> | void\n}\n\nconst zodParser = (zod: z.ZodTypeAny, opt?: 'opt') => (value: string) => {\n\tconst result = zod.safeParse(value)\n\tif (result.success) return result.data\n\tconst msg = result.error.issues[0].message\n\tif (opt) throw new InvalidOptionArgumentError(msg)\n\tthrow new InvalidArgumentError(msg)\n}\n\n/**\n * Creates a Commander.js Argument from a Zod schema.\n * Handles optionality, default values, and enum choices.\n * @param key - The argument name\n * @param zod - The Zod schema for the argument\n * @returns A Commander Argument instance\n */\nexport const zodArgument = (key: string, zod: z.ZodTypeAny): Argument => {\n\tconst flag = zod.isOptional() ? `[${key}]` : `<${key}>`\n\tconst arg = new Argument(flag, zod.description)\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) arg.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)\n\tif (choices) arg.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn arg.argParser(zodParser(zod))\n}\n\n/**\n * Creates a Commander.js Option from a Zod schema.\n * Handles optionality, default values, enum choices, and boolean flags.\n * Supports short flags via description prefix (e.g., 's;...').\n * @param key - The option name (can include underscores for grouping)\n * @param zod - The Zod schema for the option\n * @returns A Commander Option instance\n */\nexport const zodOption = (key: string, zod: z.ZodTypeAny): Option => {\n\tconst abbr = zod.description?.match(/^(\\w);/)?.[1]\n\tconst description = abbr ? zod.description.slice(2) : zod.description\n\tconst arg = key.includes('_') ? key.split('_').slice(1).join('-') : key\n\tif (key.includes('_')) [key] = key.split('_')\n\tconst isBoolean = utils.zodIsBoolean(zod)\n\tconst flag = `--${key}${isBoolean ? '' : ` <${arg}>`}`\n\tconst flags = abbr ? `-${abbr}, ${flag}` : flag\n\tconst opt = new Option(flags, description)\n\n\t// required for boolean flags\n\tif (isBoolean) opt.optional = true\n\telse if (!zod.isOptional()) opt.makeOptionMandatory()\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) opt.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)\n\tif (choices) opt.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn opt.argParser(zodParser(zod, 'opt'))\n}\n\n/**\n * Defines a Commander.js Command using Zod schemas for arguments and options.\n * Automatically wires up parsing, validation, and help configuration.\n * @template A - ZodRawShape for arguments\n * @template O - ZodRawShape for options\n * @param props - Command properties (name, description, args, opts, action)\n * @returns A Commander Command instance\n */\nexport const zodCommand = <A extends z.ZodRawShape, O extends z.ZodRawShape>({\n\tname,\n\tdescription,\n\targs,\n\topts,\n\taction,\n}: ZodCommandProps<A, O>): Command => {\n\tconst command = new Command(name)\n\tif (description) command.description(description)\n\tfor (const key in args) command.addArgument(zodArgument(key, args[key]))\n\tfor (const key in opts) command.addOption(zodOption(key, opts[key]))\n\tif (action)\n\t\tcommand.action(async (...all) => {\n\t\t\tconst resultArgs = Object.fromEntries(\n\t\t\t\tObject.keys(args ?? {}).map((key, i) => [key, all[i]]),\n\t\t\t) as z.infer<z.ZodObject<A>>\n\t\t\tconst resultOpts = all[Object.keys(args ?? {}).length] as z.infer<\n\t\t\t\tz.ZodObject<ReplaceKeyTypes<O>>\n\t\t\t>\n\t\t\tawait action(resultArgs, resultOpts)\n\t\t})\n\treturn command\n}\n"]}
package/dist/index.js DELETED
@@ -1,82 +0,0 @@
1
- import { Argument, Option, Command, InvalidOptionArgumentError, InvalidArgumentError } from 'commander';
2
- import { z } from 'zod';
3
-
4
- var __defProp = Object.defineProperty;
5
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
- var zodCore = /* @__PURE__ */ __name((zod, fn) => {
7
- const types = [z.ZodDefault, z.ZodNullable, z.ZodOptional];
8
- for (const type of types)
9
- if (zod instanceof type) return zodCore(zod._def.innerType, fn);
10
- if (zod instanceof z.ZodEffects) return zodCore(zod._def.schema, fn);
11
- return fn(zod);
12
- }, "zodCore");
13
- var zodEnumVals = /* @__PURE__ */ __name((zod) => zodCore(zod, (zod2) => zod2 instanceof z.ZodEnum ? zod2._def.values : null), "zodEnumVals");
14
- var zodIsBoolean = /* @__PURE__ */ __name((zod) => zodCore(zod, (zod2) => zod2 instanceof z.ZodBoolean), "zodIsBoolean");
15
- var zodDefault = /* @__PURE__ */ __name((zod) => zod instanceof z.ZodEffects ? zodDefault(zod._def.schema) : zod instanceof z.ZodDefault ? zod._def.defaultValue() : void 0, "zodDefault");
16
- var utils = {
17
- zodCore,
18
- zodEnumVals,
19
- zodIsBoolean,
20
- zodDefault
21
- };
22
- var utils_default = utils;
23
-
24
- // src/index.ts
25
- var zodParser = /* @__PURE__ */ __name((zod, opt) => (value) => {
26
- const result = zod.safeParse(value);
27
- if (result.success) return result.data;
28
- const msg = result.error.issues[0].message;
29
- if (opt) throw new InvalidOptionArgumentError(msg);
30
- throw new InvalidArgumentError(msg);
31
- }, "zodParser");
32
- var zodArgument = /* @__PURE__ */ __name((key, zod) => {
33
- const flag = zod.isOptional() ? `[${key}]` : `<${key}>`;
34
- const arg = new Argument(flag, zod.description);
35
- const def = utils_default.zodDefault(zod);
36
- if (def !== void 0) arg.default(zod.parse(def));
37
- const choices = utils_default.zodEnumVals(zod);
38
- if (choices) arg.choices(choices);
39
- return arg.argParser(zodParser(zod));
40
- }, "zodArgument");
41
- var zodOption = /* @__PURE__ */ __name((key, zod) => {
42
- const abbr = zod.description?.match(/^(\w);/)?.[1];
43
- const description = abbr ? zod.description.slice(2) : zod.description;
44
- const arg = key.includes("_") ? key.split("_").slice(1).join("-") : key;
45
- if (key.includes("_")) [key] = key.split("_");
46
- const isBoolean = utils_default.zodIsBoolean(zod);
47
- const flag = `--${key}${isBoolean ? "" : ` <${arg}>`}`;
48
- const flags = abbr ? `-${abbr}, ${flag}` : flag;
49
- const opt = new Option(flags, description);
50
- if (isBoolean) opt.optional = true;
51
- else if (!zod.isOptional()) opt.makeOptionMandatory();
52
- const def = utils_default.zodDefault(zod);
53
- if (def !== void 0) opt.default(zod.parse(def));
54
- const choices = utils_default.zodEnumVals(zod);
55
- if (choices) opt.choices(choices);
56
- return opt.argParser(zodParser(zod, "opt"));
57
- }, "zodOption");
58
- var zodCommand = /* @__PURE__ */ __name(({
59
- name,
60
- description,
61
- args,
62
- opts,
63
- action
64
- }) => {
65
- const command = new Command(name);
66
- if (description) command.description(description);
67
- for (const key in args) command.addArgument(zodArgument(key, args[key]));
68
- for (const key in opts) command.addOption(zodOption(key, opts[key]));
69
- if (action)
70
- command.action(async (...all) => {
71
- const resultArgs = Object.fromEntries(
72
- Object.keys(args ?? {}).map((key, i) => [key, all[i]])
73
- );
74
- const resultOpts = all[Object.keys(args ?? {}).length];
75
- await action(resultArgs, resultOpts);
76
- });
77
- return command;
78
- }, "zodCommand");
79
-
80
- export { zodArgument, zodCommand, zodOption };
81
- //# sourceMappingURL=index.js.map
82
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/utils.ts","../src/index.ts"],"names":["zod"],"mappings":";;;;;AAEA,IAAM,OAAA,mBAAU,MAAA,CAAA,CAAI,GAAA,EAAmB,EAAA,KAAoC;AAC1E,EAAA,MAAM,QAAQ,CAAC,CAAA,CAAE,YAAY,CAAA,CAAE,WAAA,EAAa,EAAE,WAAW,CAAA;AACzD,EAAA,KAAA,MAAW,IAAA,IAAQ,KAAA;AAClB,IAAA,IAAI,eAAe,IAAA,EAAM,OAAO,QAAQ,GAAA,CAAI,IAAA,CAAK,WAAW,EAAE,CAAA;AAC/D,EAAA,IAAI,GAAA,YAAe,EAAE,UAAA,EAAY,OAAO,QAAQ,GAAA,CAAI,IAAA,CAAK,QAAQ,EAAE,CAAA;AACnE,EAAA,OAAO,GAAG,GAAG,CAAA;AACd,CAAA,EANgB,SAAA,CAAA;AAQhB,IAAM,WAAA,mBAAc,MAAA,CAAA,CAAC,GAAA,KACpB,OAAA,CAAQ,KAAK,CAACA,IAAAA,KAASA,IAAAA,YAAe,CAAA,CAAE,OAAA,GAAUA,IAAAA,CAAI,IAAA,CAAK,MAAA,GAAS,IAAK,CAAA,EADtD,aAAA,CAAA;AAGpB,IAAM,YAAA,mBAAe,MAAA,CAAA,CAAC,GAAA,KACrB,OAAA,CAAQ,GAAA,EAAK,CAACA,IAAAA,KAAQA,IAAAA,YAAe,CAAA,CAAE,UAAU,CAAA,EAD7B,cAAA,CAAA;AAGrB,IAAM,6BAAa,MAAA,CAAA,CAClB,GAAA,KAEA,eAAe,CAAA,CAAE,UAAA,GACd,WAAW,GAAA,CAAI,IAA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GrD,MAAA,MAAM,MAAA,CAAO,YAAY,UAAU,CAAA;AAAA,IACpC,CAAC,CAAA;AACF,EAAA,OAAO,OAAA;AACR,CAAA,EAtB0B,YAAA","file":"index.js","sourcesContent":["import { z } from 'zod'\n\nconst zodCore = <T>(zod: z.ZodTypeAny, fn: (zod: z.ZodTypeAny) => T): T => {\n\tconst types = [z.ZodDefault, z.ZodNullable, z.ZodOptional]\n\tfor (const type of types)\n\t\tif (zod instanceof type) return zodCore(zod._def.innerType, fn)\n\tif (zod instanceof z.ZodEffects) return zodCore(zod._def.schema, fn)\n\treturn fn(zod)\n}\n\nconst zodEnumVals = (zod: z.ZodTypeAny): string[] | null =>\n\tzodCore(zod, (zod) => (zod instanceof z.ZodEnum ? zod._def.values : null))\n\nconst zodIsBoolean = (zod: z.ZodTypeAny): boolean =>\n\tzodCore(zod, (zod) => zod instanceof z.ZodBoolean)\n\nconst zodDefault = <Output, Def extends z.ZodTypeDef, Input>(\n\tzod: z.ZodType<Output, Def, Input>,\n): Input | undefined =>\n\tzod instanceof z.ZodEffects\n\t\t? zodDefault(zod._def.schema)\n\t\t: zod instanceof z.ZodDefault\n\t\t\t? zod._def.defaultValue()\n\t\t\t: undefined\n\nconst utils = {\n\tzodCore,\n\tzodEnumVals,\n\tzodIsBoolean,\n\tzodDefault,\n}\n\nexport default utils\n","import {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod'\nimport utils from './utils.js'\n\ntype BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S\n\ntype ReplaceKeyTypes<Type extends z.ZodRawShape> = {\n\t[Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key]\n}\n\ntype Prettify<T> = {\n\t[K in keyof T]: T[K]\n} & {}\n\n/**\n * The action function signature for a Zod-powered command.\n * @template A - ZodRawShape for arguments\n * @template O - ZodRawShape for options\n * @param args - Parsed and validated arguments\n * @param opts - Parsed and validated options (with key normalization)\n * @returns A Promise or void\n */\nexport type ZodCommandAction<\n\tA extends z.ZodRawShape,\n\tO extends z.ZodRawShape,\n> = ZodCommandProps<A, O>['action']\n\ntype ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {\n\tname?: string\n\tdescription?: string\n\targs?: A\n\topts?: O\n\taction?: (\n\t\targs: Prettify<z.infer<z.ZodObject<A>>>,\n\t\topts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>,\n\t) => Promise<void> | void\n}\n\nconst zodParser = (zod: z.ZodTypeAny, opt?: 'opt') => (value: string) => {\n\tconst result = zod.safeParse(value)\n\tif (result.success) return result.data\n\tconst msg = result.error.issues[0].message\n\tif (opt) throw new InvalidOptionArgumentError(msg)\n\tthrow new InvalidArgumentError(msg)\n}\n\n/**\n * Creates a Commander.js Argument from a Zod schema.\n * Handles optionality, default values, and enum choices.\n * @param key - The argument name\n * @param zod - The Zod schema for the argument\n * @returns A Commander Argument instance\n */\nexport const zodArgument = (key: string, zod: z.ZodTypeAny): Argument => {\n\tconst flag = zod.isOptional() ? `[${key}]` : `<${key}>`\n\tconst arg = new Argument(flag, zod.description)\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) arg.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)\n\tif (choices) arg.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn arg.argParser(zodParser(zod))\n}\n\n/**\n * Creates a Commander.js Option from a Zod schema.\n * Handles optionality, default values, enum choices, and boolean flags.\n * Supports short flags via description prefix (e.g., 's;...').\n * @param key - The option name (can include underscores for grouping)\n * @param zod - The Zod schema for the option\n * @returns A Commander Option instance\n */\nexport const zodOption = (key: string, zod: z.ZodTypeAny): Option => {\n\tconst abbr = zod.description?.match(/^(\\w);/)?.[1]\n\tconst description = abbr ? zod.description.slice(2) : zod.description\n\tconst arg = key.includes('_') ? key.split('_').slice(1).join('-') : key\n\tif (key.includes('_')) [key] = key.split('_')\n\tconst isBoolean = utils.zodIsBoolean(zod)\n\tconst flag = `--${key}${isBoolean ? '' : ` <${arg}>`}`\n\tconst flags = abbr ? `-${abbr}, ${flag}` : flag\n\tconst opt = new Option(flags, description)\n\n\t// required for boolean flags\n\tif (isBoolean) opt.optional = true\n\telse if (!zod.isOptional()) opt.makeOptionMandatory()\n\n\tconst def = utils.zodDefault(zod)\n\tif (def !== undefined) opt.default(zod.parse(def))\n\n\tconst choices = utils.zodEnumVals(zod)\n\tif (choices) opt.choices(choices)\n\n\t// parsing must be done at the end to override default parsers\n\treturn opt.argParser(zodParser(zod, 'opt'))\n}\n\n/**\n * Defines a Commander.js Command using Zod schemas for arguments and options.\n * Automatically wires up parsing, validation, and help configuration.\n * @template A - ZodRawShape for arguments\n * @template O - ZodRawShape for options\n * @param props - Command properties (name, description, args, opts, action)\n * @returns A Commander Command instance\n */\nexport const zodCommand = <A extends z.ZodRawShape, O extends z.ZodRawShape>({\n\tname,\n\tdescription,\n\targs,\n\topts,\n\taction,\n}: ZodCommandProps<A, O>): Command => {\n\tconst command = new Command(name)\n\tif (description) command.description(description)\n\tfor (const key in args) command.addArgument(zodArgument(key, args[key]))\n\tfor (const key in opts) command.addOption(zodOption(key, opts[key]))\n\tif (action)\n\t\tcommand.action(async (...all) => {\n\t\t\tconst resultArgs = Object.fromEntries(\n\t\t\t\tObject.keys(args ?? {}).map((key, i) => [key, all[i]]),\n\t\t\t) as z.infer<z.ZodObject<A>>\n\t\t\tconst resultOpts = all[Object.keys(args ?? {}).length] as z.infer<\n\t\t\t\tz.ZodObject<ReplaceKeyTypes<O>>\n\t\t\t>\n\t\t\tawait action(resultArgs, resultOpts)\n\t\t})\n\treturn command\n}\n"]}