zod-commander 0.0.2
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/LICENSE +21 -0
- package/README.md +48 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +67 -0
- package/dist/utis.d.ts +8 -0
- package/dist/utis.js +26 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Román Via-Dufresne Saus
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# zod-commander
|
|
2
|
+
|
|
3
|
+
A TypeScript utility for building type-safe CLI commands using [commander](https://www.npmjs.com/package/commander) and [zod](https://www.npmjs.com/package/zod).
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
`zod-commander` lets you define type-safe CLI commands using [zod](https://github.com/colinhacks/zod) schemas for arguments and options, with a simple API.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Command } from 'commander'
|
|
11
|
+
import { z } from 'zod'
|
|
12
|
+
import { zodCommand } from 'zod-commander'
|
|
13
|
+
|
|
14
|
+
// Define a command using zodCommand
|
|
15
|
+
const greet = zodCommand({
|
|
16
|
+
name: 'greet',
|
|
17
|
+
description: 'Say hello to someone',
|
|
18
|
+
args: {
|
|
19
|
+
name: z.string().describe('Name of the person to greet'),
|
|
20
|
+
},
|
|
21
|
+
opts: {
|
|
22
|
+
excited: z.boolean().default(false).describe('e;Add an exclamation mark'), // 'e;' makes -e an alias
|
|
23
|
+
},
|
|
24
|
+
async action({ name }, { excited }) {
|
|
25
|
+
console.log(`Hello, ${name}${excited ? '!' : '.'}`)
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// Create a CLI program and add the command
|
|
30
|
+
const program = new Command()
|
|
31
|
+
program
|
|
32
|
+
.name('my-cli')
|
|
33
|
+
.description('A demo CLI using zod-commander')
|
|
34
|
+
.version('1.0.0')
|
|
35
|
+
.addCommand(greet)
|
|
36
|
+
.parseAsync(process.argv)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Features
|
|
40
|
+
|
|
41
|
+
- **Type-safe arguments and options**: Use zod schemas to define and validate CLI inputs.
|
|
42
|
+
- **Booleans are treated as flags**: Any boolean option automatically becomes a CLI flag (e.g. `--excited`).
|
|
43
|
+
- **Descriptive help**: `.describe()` on schemas provides help text for each argument/option.
|
|
44
|
+
- **Default values**: Use `.default()` on zod schemas for default option values.
|
|
45
|
+
- **Async actions**: The `action` function can be async and receives parsed args and opts.
|
|
46
|
+
- **No boilerplate**: Just export your command; integrate with your CLI runner as needed.
|
|
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!
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Argument, Command, Option } from 'commander';
|
|
2
|
+
import type { z } from 'zod';
|
|
3
|
+
type BeforeFirstUnderscore<S> = S extends `${infer T}_${infer _}` ? T : S;
|
|
4
|
+
type ReplaceKeyTypes<Type extends z.ZodRawShape> = {
|
|
5
|
+
[Key in keyof Type as BeforeFirstUnderscore<Key>]: Type[Key];
|
|
6
|
+
};
|
|
7
|
+
export type ZodCommandAction<A extends z.ZodRawShape, O extends z.ZodRawShape> = (args: z.infer<z.ZodObject<A>>, opts: z.infer<z.ZodObject<ReplaceKeyTypes<O>>>) => Promise<void> | void;
|
|
8
|
+
type ZodCommandProps<A extends z.ZodRawShape, O extends z.ZodRawShape> = {
|
|
9
|
+
name: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
args?: A;
|
|
12
|
+
opts?: O;
|
|
13
|
+
action: ZodCommandAction<A, O>;
|
|
14
|
+
};
|
|
15
|
+
export declare const zodArgument: (key: string, zod: z.ZodTypeAny) => Argument;
|
|
16
|
+
export declare const zodOption: (key: string, zod: z.ZodTypeAny) => Option;
|
|
17
|
+
export declare const zodCommand: <A extends z.ZodRawShape, O extends z.ZodRawShape>({ name, description, args, opts, action, }: ZodCommandProps<A, O>) => Command;
|
|
18
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.zodCommand = exports.zodOption = exports.zodArgument = void 0;
|
|
7
|
+
const commander_1 = require("commander");
|
|
8
|
+
const kebabCase_1 = __importDefault(require("lodash/kebabCase"));
|
|
9
|
+
const utis_1 = __importDefault(require("./utis"));
|
|
10
|
+
const zodParser = (zod, opt) => (value) => {
|
|
11
|
+
const result = zod.safeParse(value);
|
|
12
|
+
if (result.success)
|
|
13
|
+
return result.data;
|
|
14
|
+
const msg = result.error.issues[0].message;
|
|
15
|
+
if (opt)
|
|
16
|
+
throw new commander_1.InvalidOptionArgumentError(msg);
|
|
17
|
+
throw new commander_1.InvalidArgumentError(msg);
|
|
18
|
+
};
|
|
19
|
+
const zodArgument = (key, zod) => {
|
|
20
|
+
const flag = zod.isOptional() ? `[${key}]` : `<${key}>`;
|
|
21
|
+
const arg = new commander_1.Argument(flag, zod.description).argParser(zodParser(zod));
|
|
22
|
+
if (utis_1.default.zodDefault(zod))
|
|
23
|
+
arg.default(zod.parse(utis_1.default.zodDefault(zod)));
|
|
24
|
+
const choices = utis_1.default.zodEnumVals(zod);
|
|
25
|
+
if (choices)
|
|
26
|
+
arg.choices(choices);
|
|
27
|
+
return arg;
|
|
28
|
+
};
|
|
29
|
+
exports.zodArgument = zodArgument;
|
|
30
|
+
const zodOption = (key, zod) => {
|
|
31
|
+
const abbr = zod.description?.match(/^(\w);/)?.[1];
|
|
32
|
+
const description = abbr ? zod.description.slice(2) : zod.description;
|
|
33
|
+
const arg = key.includes('_') ? key.split('_').slice(1).join('-') : key;
|
|
34
|
+
if (key.includes('_'))
|
|
35
|
+
[key] = key.split('_');
|
|
36
|
+
key = (0, kebabCase_1.default)(key);
|
|
37
|
+
const isBoolean = utis_1.default.zodIsBoolean(zod);
|
|
38
|
+
const flag = `--${key}${isBoolean ? '' : zod.isOptional() ? ` [${arg}]` : ` <${arg}>`}`;
|
|
39
|
+
const flags = abbr ? `-${abbr}, ${flag}` : flag;
|
|
40
|
+
const opt = new commander_1.Option(flags, description).argParser(zodParser(zod, 'opt'));
|
|
41
|
+
if (utis_1.default.zodDefault(zod))
|
|
42
|
+
opt.default(zod.parse(utis_1.default.zodDefault(zod)));
|
|
43
|
+
if (isBoolean)
|
|
44
|
+
opt.optional = true;
|
|
45
|
+
const choices = utis_1.default.zodEnumVals(zod);
|
|
46
|
+
if (choices)
|
|
47
|
+
opt.choices(choices);
|
|
48
|
+
return opt;
|
|
49
|
+
};
|
|
50
|
+
exports.zodOption = zodOption;
|
|
51
|
+
const zodCommand = ({ name, description, args, opts, action, }) => {
|
|
52
|
+
const command = new commander_1.Command(name);
|
|
53
|
+
if (description)
|
|
54
|
+
command.description(description);
|
|
55
|
+
for (const key in args)
|
|
56
|
+
command.addArgument((0, exports.zodArgument)(key, args[key]));
|
|
57
|
+
for (const key in opts)
|
|
58
|
+
command.addOption((0, exports.zodOption)(key, opts[key]));
|
|
59
|
+
command.action(async (...all) => {
|
|
60
|
+
const resultArgs = Object.fromEntries(Object.keys(args ?? {}).map((key, i) => [key, all[i]]));
|
|
61
|
+
const resultOpts = all[Object.keys(args ?? {}).length];
|
|
62
|
+
await action(resultArgs, resultOpts);
|
|
63
|
+
});
|
|
64
|
+
command.configureHelp({ showGlobalOptions: true });
|
|
65
|
+
return command;
|
|
66
|
+
};
|
|
67
|
+
exports.zodCommand = zodCommand;
|
package/dist/utis.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const utils: {
|
|
3
|
+
zodCore: <T>(zod: z.ZodTypeAny, fn: (zod: z.ZodTypeAny) => T) => T;
|
|
4
|
+
zodEnumVals: (zod: z.ZodTypeAny) => any;
|
|
5
|
+
zodIsBoolean: (zod: z.ZodTypeAny) => boolean;
|
|
6
|
+
zodDefault: <Output, Def extends z.ZodTypeDef, Input>(zod: z.ZodType<Output, Def, Input>) => Input | undefined;
|
|
7
|
+
};
|
|
8
|
+
export default utils;
|
package/dist/utis.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const zod_1 = require("zod");
|
|
4
|
+
const zodCore = (zod, fn) => {
|
|
5
|
+
const types = [zod_1.z.ZodDefault, zod_1.z.ZodNullable, zod_1.z.ZodOptional];
|
|
6
|
+
for (const type of types)
|
|
7
|
+
if (zod instanceof type)
|
|
8
|
+
return zodCore(zod._def.innerType, fn);
|
|
9
|
+
if (zod instanceof zod_1.z.ZodEffects)
|
|
10
|
+
return zodCore(zod._def.schema, fn);
|
|
11
|
+
return fn(zod);
|
|
12
|
+
};
|
|
13
|
+
const zodEnumVals = (zod) => zodCore(zod, (zod) => (zod instanceof zod_1.z.ZodEnum ? zod._def.values : null));
|
|
14
|
+
const zodIsBoolean = (zod) => zodCore(zod, (zod) => zod instanceof zod_1.z.ZodBoolean);
|
|
15
|
+
const zodDefault = (zod) => zod instanceof zod_1.z.ZodEffects
|
|
16
|
+
? zodDefault(zod._def.schema)
|
|
17
|
+
: zod instanceof zod_1.z.ZodDefault
|
|
18
|
+
? zod._def.defaultValue()
|
|
19
|
+
: undefined;
|
|
20
|
+
const utils = {
|
|
21
|
+
zodCore,
|
|
22
|
+
zodEnumVals,
|
|
23
|
+
zodIsBoolean,
|
|
24
|
+
zodDefault,
|
|
25
|
+
};
|
|
26
|
+
exports.default = utils;
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zod-commander",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "A TypeScript utility for building type-safe CLI commands using commander and zod.",
|
|
5
|
+
"author": "Román Via-Dufresne Saus <roman910dev@gmail.com>(https://github.com/roman910dev)",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/roman910dev/zod-commander.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"commander",
|
|
13
|
+
"zod",
|
|
14
|
+
"cli",
|
|
15
|
+
"typescript"
|
|
16
|
+
],
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@biomejs/biome": "^2.0.6",
|
|
19
|
+
"@types/lodash": "^4.17.20",
|
|
20
|
+
"commander": "^14.0.0",
|
|
21
|
+
"husky": "^9.1.7",
|
|
22
|
+
"lint-staged": "^16.1.2",
|
|
23
|
+
"typescript": "^5.8.3",
|
|
24
|
+
"zod": "^3.25.0"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"lodash": "^4.17.21"
|
|
28
|
+
},
|
|
29
|
+
"main": "dist/index.js",
|
|
30
|
+
"types": "dist/index.d.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"import": "./dist/index.js",
|
|
34
|
+
"require": "./dist/index.js",
|
|
35
|
+
"types": "./dist/index.d.ts"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist/"
|
|
40
|
+
],
|
|
41
|
+
"lint-staged": {
|
|
42
|
+
"*.ts": [
|
|
43
|
+
"tsc --noEmit && biome ci --threads=4"
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"commander": ">=8 <15",
|
|
51
|
+
"zod": ">=3.20.0 <4"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "tsc",
|
|
55
|
+
"lint": "biome check .",
|
|
56
|
+
"lint:fix": "biome check --fix --formatter-enabled=false .",
|
|
57
|
+
"format": "biome format --write .",
|
|
58
|
+
"ci": "biome ci --threads=4"
|
|
59
|
+
}
|
|
60
|
+
}
|