zod-commander 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.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,6 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ export { __name };
5
+ //# sourceMappingURL=chunk-7QVYU63E.js.map
6
+ //# sourceMappingURL=chunk-7QVYU63E.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"chunk-7QVYU63E.js"}
@@ -1,20 +1,20 @@
1
1
  'use strict';
2
2
 
3
3
  var commander = require('commander');
4
- var zod = require('zod');
4
+ var v3 = require('zod/v3');
5
5
 
6
6
  var __defProp = Object.defineProperty;
7
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];
8
+ var zodCore = /* @__PURE__ */ __name((zod, fn) => {
9
+ const types = [v3.z.ZodDefault, v3.z.ZodNullable, v3.z.ZodOptional];
10
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);
11
+ if (zod instanceof type) return zodCore(zod._def.innerType, fn);
12
+ if (zod instanceof v3.z.ZodEffects) return zodCore(zod._def.schema, fn);
13
+ return fn(zod);
14
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");
15
+ var zodEnumVals = /* @__PURE__ */ __name((zod) => zodCore(zod, (zod2) => zod2 instanceof v3.z.ZodEnum ? zod2._def.values : null), "zodEnumVals");
16
+ var zodIsBoolean = /* @__PURE__ */ __name((zod) => zodCore(zod, (zod2) => zod2 instanceof v3.z.ZodBoolean), "zodIsBoolean");
17
+ var zodDefault = /* @__PURE__ */ __name((zod) => zod instanceof v3.z.ZodEffects ? zodDefault(zod._def.schema) : zod instanceof v3.z.ZodDefault ? zod._def.defaultValue() : void 0, "zodDefault");
18
18
  var utils = {
19
19
  zodCore,
20
20
  zodEnumVals,
@@ -23,7 +23,7 @@ var utils = {
23
23
  };
24
24
  var utils_default = utils;
25
25
 
26
- // src/index.ts
26
+ // src/zod3/index.ts
27
27
  var zodParser = /* @__PURE__ */ __name((zod, opt) => (value) => {
28
28
  const result = zod.safeParse(value);
29
29
  if (result.success) return result.data;
@@ -68,13 +68,14 @@ var zodCommand = /* @__PURE__ */ __name(({
68
68
  if (description) command.description(description);
69
69
  for (const key in args) command.addArgument(zodArgument(key, args[key]));
70
70
  for (const key in opts) command.addOption(zodOption(key, opts[key]));
71
- command.action(async (...all) => {
72
- const resultArgs = Object.fromEntries(
73
- Object.keys(args ?? {}).map((key, i) => [key, all[i]])
74
- );
75
- const resultOpts = all[Object.keys(args ?? {}).length];
76
- await action(resultArgs, resultOpts);
77
- });
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
+ });
78
79
  return command;
79
80
  }, "zodCommand");
80
81
 
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/zod3/utils.ts","../../src/zod3/index.ts"],"names":["z","zod","InvalidOptionArgumentError","InvalidArgumentError","Argument","Option","Command"],"mappings":";;;;;;;AAEA,IAAM,OAAA,mBAAU,MAAA,CAAA,CAAI,GAAA,EAAmB,EAAA,KAAoC;AAC1E,EAAA,MAAM,QAAQ,CAACA,IAAA,CAAE,YAAYA,IAAA,CAAE,WAAA,EAAaA,KAAE,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,YAAeA,KAAE,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,OAA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,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/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","import {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod/v3'\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"]}
@@ -1,5 +1,5 @@
1
1
  import { Argument, Option, Command } from 'commander';
2
- import { z } from 'zod';
2
+ import { z } from 'zod/v3';
3
3
 
4
4
  type BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S;
5
5
  type ReplaceKeyTypes<Type extends z.ZodRawShape> = {
@@ -18,11 +18,11 @@ type Prettify<T> = {
18
18
  */
19
19
  type ZodCommandAction<A extends z.ZodRawShape, O extends z.ZodRawShape> = ZodCommandProps<A, O>['action'];
20
20
  type ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {
21
- name: string;
21
+ name?: string;
22
22
  description?: string;
23
23
  args?: A;
24
24
  opts?: O;
25
- action: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
25
+ action?: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
26
26
  };
27
27
  /**
28
28
  * Creates a Commander.js Argument from a Zod schema.
@@ -1,5 +1,5 @@
1
1
  import { Argument, Option, Command } from 'commander';
2
- import { z } from 'zod';
2
+ import { z } from 'zod/v3';
3
3
 
4
4
  type BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S;
5
5
  type ReplaceKeyTypes<Type extends z.ZodRawShape> = {
@@ -18,11 +18,11 @@ type Prettify<T> = {
18
18
  */
19
19
  type ZodCommandAction<A extends z.ZodRawShape, O extends z.ZodRawShape> = ZodCommandProps<A, O>['action'];
20
20
  type ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {
21
- name: string;
21
+ name?: string;
22
22
  description?: string;
23
23
  args?: A;
24
24
  opts?: O;
25
- action: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
25
+ action?: (args: Prettify<z.infer<z.ZodObject<A>>>, opts: Prettify<z.infer<z.ZodObject<ReplaceKeyTypes<O>>>>) => Promise<void> | void;
26
26
  };
27
27
  /**
28
28
  * Creates a Commander.js Argument from a Zod schema.
@@ -1,8 +1,7 @@
1
+ import { __name } from '../chunk-7QVYU63E.js';
1
2
  import { Argument, Option, Command, InvalidOptionArgumentError, InvalidArgumentError } from 'commander';
2
- import { z } from 'zod';
3
+ import { z } from 'zod/v3';
3
4
 
4
- var __defProp = Object.defineProperty;
5
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
5
  var zodCore = /* @__PURE__ */ __name((zod, fn) => {
7
6
  const types = [z.ZodDefault, z.ZodNullable, z.ZodOptional];
8
7
  for (const type of types)
@@ -21,7 +20,7 @@ var utils = {
21
20
  };
22
21
  var utils_default = utils;
23
22
 
24
- // src/index.ts
23
+ // src/zod3/index.ts
25
24
  var zodParser = /* @__PURE__ */ __name((zod, opt) => (value) => {
26
25
  const result = zod.safeParse(value);
27
26
  if (result.success) return result.data;
@@ -66,13 +65,14 @@ var zodCommand = /* @__PURE__ */ __name(({
66
65
  if (description) command.description(description);
67
66
  for (const key in args) command.addArgument(zodArgument(key, args[key]));
68
67
  for (const key in opts) command.addOption(zodOption(key, opts[key]));
69
- command.action(async (...all) => {
70
- const resultArgs = Object.fromEntries(
71
- Object.keys(args ?? {}).map((key, i) => [key, all[i]])
72
- );
73
- const resultOpts = all[Object.keys(args ?? {}).length];
74
- await action(resultArgs, resultOpts);
75
- });
68
+ if (action)
69
+ command.action(async (...all) => {
70
+ const resultArgs = Object.fromEntries(
71
+ Object.keys(args ?? {}).map((key, i) => [key, all[i]])
72
+ );
73
+ const resultOpts = all[Object.keys(args ?? {}).length];
74
+ await action(resultArgs, resultOpts);
75
+ });
76
76
  return command;
77
77
  }, "zodCommand");
78
78
 
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/zod3/utils.ts","../../src/zod3/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,IAAA,CAAK,MAAM,CAAA,GAC1B,eAAe,CAAA,CAAE,UAAA,GAChB,IAAI,IAAA,CAAK,YAAA,KACT,MAAA,EA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ACR,CAAA,EAtB0B,YAAA","file":"index.js","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","import {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod/v3'\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"]}
@@ -0,0 +1,88 @@
1
+ 'use strict';
2
+
3
+ var commander = require('commander');
4
+ var v4 = require('zod/v4');
5
+
6
+ var __defProp = Object.defineProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var zodCore = /* @__PURE__ */ __name((zod, fn) => {
9
+ const types = [v4.z.ZodDefault, v4.z.ZodPrefault, v4.z.ZodNullable, v4.z.ZodOptional];
10
+ for (const type of types)
11
+ if (zod instanceof type) return zodCore(zod.def.innerType, fn);
12
+ if (zod instanceof v4.z.ZodPipe) return zodCore(zod.def.in, fn);
13
+ return fn(zod);
14
+ }, "zodCore");
15
+ var zodEnumVals = /* @__PURE__ */ __name((zod) => zodCore(zod, (zod2) => zod2 instanceof v4.z.ZodEnum ? zod2.options : null), "zodEnumVals");
16
+ var zodIsBoolean = /* @__PURE__ */ __name((zod) => zodCore(zod, (zod2) => zod2 instanceof v4.z.ZodBoolean), "zodIsBoolean");
17
+ var zodIsOptional = /* @__PURE__ */ __name((zod) => zod.safeParse(void 0).success, "zodIsOptional");
18
+ var zodDefault = /* @__PURE__ */ __name((zod) => zod instanceof v4.z.ZodPipe ? zodDefault(zod.def.in) : zod instanceof v4.z.ZodPrefault ? zod.def.defaultValue : void 0, "zodDefault");
19
+ var utils = {
20
+ zodCore,
21
+ zodEnumVals,
22
+ zodIsBoolean,
23
+ zodIsOptional,
24
+ zodDefault
25
+ };
26
+ var utils_default = utils;
27
+
28
+ // src/zod4/index.ts
29
+ var zodParser = /* @__PURE__ */ __name((zod, opt) => (value) => {
30
+ const result = zod.safeParse(value);
31
+ if (result.success) return result.data;
32
+ const msg = result.error.issues[0].message;
33
+ if (opt) throw new commander.InvalidOptionArgumentError(msg);
34
+ throw new commander.InvalidArgumentError(msg);
35
+ }, "zodParser");
36
+ var zodArgument = /* @__PURE__ */ __name((key, zod) => {
37
+ const flag = utils_default.zodIsOptional(zod) ? `[${key}]` : `<${key}>`;
38
+ const arg = new commander.Argument(flag, zod.description);
39
+ const def = utils_default.zodDefault(zod);
40
+ if (def !== void 0) arg.default(zod.parse(def));
41
+ const choices = utils_default.zodEnumVals(zod)?.map(String);
42
+ if (choices) arg.choices(choices);
43
+ return arg.argParser(zodParser(zod));
44
+ }, "zodArgument");
45
+ var zodOption = /* @__PURE__ */ __name((key, zod) => {
46
+ const abbr = zod.description?.match(/^(\w);/)?.[1];
47
+ const description = abbr ? zod.description?.slice(2) : zod.description;
48
+ const arg = key.includes("_") ? key.split("_").slice(1).join("-") : key;
49
+ if (key.includes("_")) [key] = key.split("_");
50
+ const isBoolean = utils_default.zodIsBoolean(zod);
51
+ const flag = `--${key}${isBoolean ? "" : ` <${arg}>`}`;
52
+ const flags = abbr ? `-${abbr}, ${flag}` : flag;
53
+ const opt = new commander.Option(flags, description);
54
+ if (isBoolean) opt.optional = true;
55
+ else if (!utils_default.zodIsOptional(zod)) opt.makeOptionMandatory();
56
+ const def = utils_default.zodDefault(zod);
57
+ if (def !== void 0) opt.default(zod.parse(def));
58
+ const choices = utils_default.zodEnumVals(zod)?.map(String);
59
+ if (choices) opt.choices(choices);
60
+ return opt.argParser(zodParser(zod, "opt"));
61
+ }, "zodOption");
62
+ var zodCommand = /* @__PURE__ */ __name(({
63
+ name,
64
+ description,
65
+ args,
66
+ opts,
67
+ action
68
+ }) => {
69
+ const command = new commander.Command(name);
70
+ if (description) command.description(description);
71
+ for (const key in args) command.addArgument(zodArgument(key, args[key]));
72
+ for (const key in opts) command.addOption(zodOption(key, opts[key]));
73
+ if (action)
74
+ command.action(async (...all) => {
75
+ const resultArgs = Object.fromEntries(
76
+ Object.keys(args ?? {}).map((key, i) => [key, all[i]])
77
+ );
78
+ const resultOpts = all[Object.keys(args ?? {}).length];
79
+ await action(resultArgs, resultOpts);
80
+ });
81
+ return command;
82
+ }, "zodCommand");
83
+
84
+ exports.zodArgument = zodArgument;
85
+ exports.zodCommand = zodCommand;
86
+ exports.zodOption = zodOption;
87
+ //# sourceMappingURL=index.cjs.map
88
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/zod4/utils.ts","../../src/zod4/index.ts"],"names":["z","zod","InvalidOptionArgumentError","InvalidArgumentError","Argument","Option","Command"],"mappings":";;;;;;;AAEA,IAAM,OAAA,mBAAU,MAAA,CAAA,CACf,GAAA,EACA,EAAA,KACO;AACP,EAAA,MAAM,KAAA,GAAQ,CAACA,IAAA,CAAE,UA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zod: 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","import {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod/v4'\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 - 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"]}
@@ -0,0 +1,54 @@
1
+ import { Argument, Option, Command } from 'commander';
2
+ import { z } from 'zod/v4';
3
+
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
+ } & {};
11
+ /**
12
+ * The action function signature for a Zod-powered command.
13
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
14
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
15
+ * @param args - Parsed and validated arguments
16
+ * @param opts - Parsed and validated options (with key normalization)
17
+ * @returns A Promise or void
18
+ */
19
+ type ZodCommandAction<A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>> = ZodCommandProps<A, O>['action'];
20
+ type ZodCommandProps<A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>> = {
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;
26
+ };
27
+ /**
28
+ * Creates a Commander.js Argument from a Zod schema.
29
+ * Handles optionality, default values, and enum choices.
30
+ * @param key - The argument name
31
+ * @param zod - The Zod schema for the argument
32
+ * @returns A Commander Argument instance
33
+ */
34
+ declare const zodArgument: (key: string, zod: z.ZodType) => Argument;
35
+ /**
36
+ * Creates a Commander.js Option from a Zod schema.
37
+ * Handles optionality, default values, enum choices, and boolean flags.
38
+ * Supports short flags via description prefix (e.g., 's;...').
39
+ * @param key - The option name (can include underscores for grouping)
40
+ * @param zod - The Zod schema for the option
41
+ * @returns A Commander Option instance
42
+ */
43
+ declare const zodOption: (key: string, zod: z.ZodType) => Option;
44
+ /**
45
+ * Defines a Commander.js Command using Zod schemas for arguments and options.
46
+ * Automatically wires up parsing, validation, and help configuration.
47
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
48
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
49
+ * @param props - Command properties (name, description, args, opts, action)
50
+ * @returns A Commander Command instance
51
+ */
52
+ declare const zodCommand: <A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>>({ name, description, args, opts, action, }: ZodCommandProps<A, O>) => Command;
53
+
54
+ export { type ZodCommandAction, zodArgument, zodCommand, zodOption };
@@ -0,0 +1,54 @@
1
+ import { Argument, Option, Command } from 'commander';
2
+ import { z } from 'zod/v4';
3
+
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
+ } & {};
11
+ /**
12
+ * The action function signature for a Zod-powered command.
13
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
14
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
15
+ * @param args - Parsed and validated arguments
16
+ * @param opts - Parsed and validated options (with key normalization)
17
+ * @returns A Promise or void
18
+ */
19
+ type ZodCommandAction<A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>> = ZodCommandProps<A, O>['action'];
20
+ type ZodCommandProps<A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>> = {
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;
26
+ };
27
+ /**
28
+ * Creates a Commander.js Argument from a Zod schema.
29
+ * Handles optionality, default values, and enum choices.
30
+ * @param key - The argument name
31
+ * @param zod - The Zod schema for the argument
32
+ * @returns A Commander Argument instance
33
+ */
34
+ declare const zodArgument: (key: string, zod: z.ZodType) => Argument;
35
+ /**
36
+ * Creates a Commander.js Option from a Zod schema.
37
+ * Handles optionality, default values, enum choices, and boolean flags.
38
+ * Supports short flags via description prefix (e.g., 's;...').
39
+ * @param key - The option name (can include underscores for grouping)
40
+ * @param zod - The Zod schema for the option
41
+ * @returns A Commander Option instance
42
+ */
43
+ declare const zodOption: (key: string, zod: z.ZodType) => Option;
44
+ /**
45
+ * Defines a Commander.js Command using Zod schemas for arguments and options.
46
+ * Automatically wires up parsing, validation, and help configuration.
47
+ * @template A - Record of key-value pairs where key is the argument name and value is the Zod schema
48
+ * @template O - Record of key-value pairs where key is the option name and value is the Zod schema
49
+ * @param props - Command properties (name, description, args, opts, action)
50
+ * @returns A Commander Command instance
51
+ */
52
+ declare const zodCommand: <A extends Record<string, z.ZodType>, O extends Record<string, z.ZodType>>({ name, description, args, opts, action, }: ZodCommandProps<A, O>) => Command;
53
+
54
+ export { type ZodCommandAction, zodArgument, zodCommand, zodOption };
@@ -0,0 +1,83 @@
1
+ import { __name } from '../chunk-7QVYU63E.js';
2
+ import { Argument, Option, Command, InvalidOptionArgumentError, InvalidArgumentError } from 'commander';
3
+ import { z } from 'zod/v4';
4
+
5
+ var zodCore = /* @__PURE__ */ __name((zod, fn) => {
6
+ const types = [z.ZodDefault, z.ZodPrefault, z.ZodNullable, z.ZodOptional];
7
+ for (const type of types)
8
+ if (zod instanceof type) return zodCore(zod.def.innerType, fn);
9
+ if (zod instanceof z.ZodPipe) return zodCore(zod.def.in, fn);
10
+ return fn(zod);
11
+ }, "zodCore");
12
+ var zodEnumVals = /* @__PURE__ */ __name((zod) => zodCore(zod, (zod2) => zod2 instanceof z.ZodEnum ? zod2.options : null), "zodEnumVals");
13
+ var zodIsBoolean = /* @__PURE__ */ __name((zod) => zodCore(zod, (zod2) => zod2 instanceof z.ZodBoolean), "zodIsBoolean");
14
+ var zodIsOptional = /* @__PURE__ */ __name((zod) => zod.safeParse(void 0).success, "zodIsOptional");
15
+ var zodDefault = /* @__PURE__ */ __name((zod) => zod instanceof z.ZodPipe ? zodDefault(zod.def.in) : zod instanceof z.ZodPrefault ? zod.def.defaultValue : void 0, "zodDefault");
16
+ var utils = {
17
+ zodCore,
18
+ zodEnumVals,
19
+ zodIsBoolean,
20
+ zodIsOptional,
21
+ zodDefault
22
+ };
23
+ var utils_default = utils;
24
+
25
+ // src/zod4/index.ts
26
+ var zodParser = /* @__PURE__ */ __name((zod, opt) => (value) => {
27
+ const result = zod.safeParse(value);
28
+ if (result.success) return result.data;
29
+ const msg = result.error.issues[0].message;
30
+ if (opt) throw new InvalidOptionArgumentError(msg);
31
+ throw new InvalidArgumentError(msg);
32
+ }, "zodParser");
33
+ var zodArgument = /* @__PURE__ */ __name((key, zod) => {
34
+ const flag = utils_default.zodIsOptional(zod) ? `[${key}]` : `<${key}>`;
35
+ const arg = new Argument(flag, zod.description);
36
+ const def = utils_default.zodDefault(zod);
37
+ if (def !== void 0) arg.default(zod.parse(def));
38
+ const choices = utils_default.zodEnumVals(zod)?.map(String);
39
+ if (choices) arg.choices(choices);
40
+ return arg.argParser(zodParser(zod));
41
+ }, "zodArgument");
42
+ var zodOption = /* @__PURE__ */ __name((key, zod) => {
43
+ const abbr = zod.description?.match(/^(\w);/)?.[1];
44
+ const description = abbr ? zod.description?.slice(2) : zod.description;
45
+ const arg = key.includes("_") ? key.split("_").slice(1).join("-") : key;
46
+ if (key.includes("_")) [key] = key.split("_");
47
+ const isBoolean = utils_default.zodIsBoolean(zod);
48
+ const flag = `--${key}${isBoolean ? "" : ` <${arg}>`}`;
49
+ const flags = abbr ? `-${abbr}, ${flag}` : flag;
50
+ const opt = new Option(flags, description);
51
+ if (isBoolean) opt.optional = true;
52
+ else if (!utils_default.zodIsOptional(zod)) opt.makeOptionMandatory();
53
+ const def = utils_default.zodDefault(zod);
54
+ if (def !== void 0) opt.default(zod.parse(def));
55
+ const choices = utils_default.zodEnumVals(zod)?.map(String);
56
+ if (choices) opt.choices(choices);
57
+ return opt.argParser(zodParser(zod, "opt"));
58
+ }, "zodOption");
59
+ var zodCommand = /* @__PURE__ */ __name(({
60
+ name,
61
+ description,
62
+ args,
63
+ opts,
64
+ action
65
+ }) => {
66
+ const command = new Command(name);
67
+ if (description) command.description(description);
68
+ for (const key in args) command.addArgument(zodArgument(key, args[key]));
69
+ for (const key in opts) command.addOption(zodOption(key, opts[key]));
70
+ if (action)
71
+ command.action(async (...all) => {
72
+ const resultArgs = Object.fromEntries(
73
+ Object.keys(args ?? {}).map((key, i) => [key, all[i]])
74
+ );
75
+ const resultOpts = all[Object.keys(args ?? {}).length];
76
+ await action(resultArgs, resultOpts);
77
+ });
78
+ return command;
79
+ }, "zodCommand");
80
+
81
+ export { zodArgument, zodCommand, zodOption };
82
+ //# sourceMappingURL=index.js.map
83
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/zod4/utils.ts","../../src/zod4/index.ts"],"names":["zod"],"mappings":";;;;AAEA,IAAM,OAAA,mBAAU,MAAA,CAAA,CACf,GAAA,EACA,EAAA,KACO;AACP,EAAA,MAAM,KAAA,GAAQ,CAAC,CAAA,CAAE,UAAA,EAAY,EAAE,WAAA,EAAa,CAAA,CAAE,WAAA,EAAa,CAAA,CAAE,WAAW,CAAA;AACxE,EAAA,KAAA,MAAW,IAAA,IAAQ,KAAA;AAClB,IAAA,IAAI,eAAe,IAAA,EAAM,OAAO,QAAQ,GAAA,CAAI,GAAA,CAAI,WAAW,EAAE,CAAA;AAC9D,EAAA,IAAI,GAAA,YAAe,EAAE,OAAA,EAAS,OAAO,QAAQ,GAAA,CAAI,GAAA,CAAI,IAAI,EAAE,CAAA;AAC3D,EAAA,OAAO,GAAG,GAAG,CAAA;AACd,CAAA,EATgB,SAAA,CAAA;AAWhB,IAAM,WAAA,mBAAc,MAAA,CAAA,CAAC,GAAA,KACpB,OAAA,CAAQ,GAAA,EAAK,CAACA,IAAAA,KAASA,IAAAA,YAAe,CAAA,CAAE,OAAA,GAAUA,IAAAA,CAAI,OAAA,GAAU,IAAK,CAAA,EADlD,aAAA,CAAA;AAGpB,IAAM,YAAA,mBAAe,MAAA,CAAA,CAAC,GAAA,KACrB,OAAA,CAA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C,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,EAzB0B,YAAA","file":"index.js","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","import {\n\tArgument,\n\tCommand,\n\tInvalidArgumentError,\n\tInvalidOptionArgumentError,\n\tOption,\n} from 'commander'\nimport type { z } from 'zod/v4'\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 - 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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zod-commander",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
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,38 +15,51 @@
15
15
  "typescript"
16
16
  ],
17
17
  "devDependencies": {
18
- "@biomejs/biome": "^2.0.6",
18
+ "@biomejs/biome": "^2.3.11",
19
19
  "@types/node": "^22.16.3",
20
- "commander": "^14.0.0",
20
+ "commander": "^14.0.2",
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
+ "tsup": "^8.5.1",
23
+ "typescript": "^5.9.3",
24
+ "vitest": "^4.0.16",
25
+ "zod": "^4.3.5"
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": {
@@ -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/D,EAAA,IAAIA,KAAA,YAAeC,MAAE,UAAA,EAAY,OAAO,QAAQD,KAAA,CAAI,IAAA,CAAK,QAAQ,EAAE,CAAA;AACnE,EAAA,OAAO,GAAGA,KAAG,CAAA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C,CAAC,CAAA;AACvE,EAAA,KAAA,MAAW,GAAA,IAAO,MAAM,OAAA,CAAQ,SAAA,CAAU,UAAU,GAAA,EAAK,IAAA,CAAK,GAAG,CAAC,CAAC,CAAA;AACnE,EAAA,OAAA,CAAQ,MAAA,CAAO,UAAU,GAAA,KAAQ;AAChC,IAAA,MAAM,aAAa,MAAA,CAAO,WAAA;AAAA,MACzB,MAAA,CAAO,IAAA,CAAK,IAAA,IAAQ,EAAE,CAAA,CAAE,GAAA,CAAI,CAAC,GAAA,EAAK,MAAM,CAAC,GAAA,EAAK,GAAA,CAAI,CAAC,CAAC,CAAC;AAAA,KACtD;AACA,IAAA,MAAM,UAAA,GAAa,IAAI,MAAA,CAAO,IAAA,CAAK,QAAQ,EAAE,EAAE,MAAM,CAAA;AAGrD,IAAA,MAAM,MAAA,CAAO,YAAY,UAAU,CAAA;AAAA,EACpC,CAAC,CAAA;AACD,EAAA,OAAO,OAAA;AACR,CAAA,EArB0B,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\tcommand.action(async (...all) => {\n\t\tconst resultArgs = Object.fromEntries(\n\t\t\tObject.keys(args ?? {}).map((key, i) => [key, all[i]]),\n\t\t) as z.infer<z.ZodObject<A>>\n\t\tconst resultOpts = all[Object.keys(args ?? {}).length] as z.infer<\n\t\t\tz.ZodObject<ReplaceKeyTypes<O>>\n\t\t>\n\t\tawait action(resultArgs, resultOpts)\n\t})\n\treturn command\n}\n"]}
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,MAA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A,CAAO,IAAA,CAAK,QAAQ,EAAE,EAAE,MAAM,CAAA;AAGrD,IAAA,MAAM,MAAA,CAAO,YAAY,UAAU,CAAA;AAAA,EACpC,CAAC,CAAA;AACD,EAAA,OAAO,OAAA;AACR,CAAA,EArB0B,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\tcommand.action(async (...all) => {\n\t\tconst resultArgs = Object.fromEntries(\n\t\t\tObject.keys(args ?? {}).map((key, i) => [key, all[i]]),\n\t\t) as z.infer<z.ZodObject<A>>\n\t\tconst resultOpts = all[Object.keys(args ?? {}).length] as z.infer<\n\t\t\tz.ZodObject<ReplaceKeyTypes<O>>\n\t\t>\n\t\tawait action(resultArgs, resultOpts)\n\t})\n\treturn command\n}\n"]}