zod-inquirer 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 +68 -0
- package/dist/esm/index.js +49 -0
- package/dist/iife/index.js +2278 -0
- package/dist/index.cjs +74 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +74 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# zod-inquirer (WIP)
|
|
2
|
+
|
|
3
|
+
A TypeScript library that automatically generates validated CLI prompts from Zod schemas.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install zod-inquirer
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Define your schema once, get validated prompts automatically:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import { zodPrompter } from "zod-inquirer";
|
|
18
|
+
|
|
19
|
+
const PizzaSchema = z.object({
|
|
20
|
+
name: z.string(),
|
|
21
|
+
size: z.enum(["small", "medium", "large"]),
|
|
22
|
+
crust: z.enum(["thin", "thick", "stuffed"]),
|
|
23
|
+
toppings: z
|
|
24
|
+
.array(
|
|
25
|
+
z.enum([
|
|
26
|
+
"pepperoni",
|
|
27
|
+
"mushrooms",
|
|
28
|
+
"onions",
|
|
29
|
+
"sausage",
|
|
30
|
+
"bacon",
|
|
31
|
+
"extra cheese",
|
|
32
|
+
]),
|
|
33
|
+
)
|
|
34
|
+
.min(1, { message: "You must select at least one topping." })
|
|
35
|
+
.max(3, { message: "You can select up to 3 toppings." }),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const pizza = await zodPrompter(PizzaSchema);
|
|
39
|
+
console.log(pizza);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
This will automatically prompt the user with:
|
|
43
|
+
|
|
44
|
+
- A text input for `name`
|
|
45
|
+
- A select dropdown for `size`
|
|
46
|
+
- A select dropdown for `crust`
|
|
47
|
+
- A checkbox list for `toppings`
|
|
48
|
+
|
|
49
|
+
All inputs are validated against your Zod schema with automatic retry on validation failure.
|
|
50
|
+
|
|
51
|
+
## Options
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
await zodPrompter(schema, {
|
|
55
|
+
maxRetries: 3, // Maximum retry attempts for invalid input (default: 3)
|
|
56
|
+
hint: "press Ctrl+C to abort", // Hint message shown on validation error
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Supported Types
|
|
61
|
+
|
|
62
|
+
- `z.string()` - Text input
|
|
63
|
+
- `z.enum()` - Select dropdown
|
|
64
|
+
- `z.array(z.enum())` - Checkbox multi-select
|
|
65
|
+
|
|
66
|
+
## License
|
|
67
|
+
|
|
68
|
+
MIT
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import { input, checkbox, select } from "@inquirer/prompts";
|
|
3
|
+
var zodPrompter = async (ZodSchema, options = {}) => {
|
|
4
|
+
const schema = ZodSchema;
|
|
5
|
+
const schemaShape = schema.shape;
|
|
6
|
+
const fields = Object.keys(schemaShape || {});
|
|
7
|
+
const answers = {};
|
|
8
|
+
const maxRetries = typeof options.maxRetries === "number" ? options.maxRetries : 3;
|
|
9
|
+
const hint = options.hint ?? "press Ctrl+C to abort";
|
|
10
|
+
for (const key of fields) {
|
|
11
|
+
const message = `Enter your ${key.replace(/([A-Z])/g, " $1").toLowerCase()}:`;
|
|
12
|
+
const fieldSchema = schemaShape[key];
|
|
13
|
+
let attempts = 0;
|
|
14
|
+
while (true) {
|
|
15
|
+
let value;
|
|
16
|
+
const def = fieldSchema._def;
|
|
17
|
+
if (def.typeName === "ZodEnum") {
|
|
18
|
+
const enumValues = def.values || [];
|
|
19
|
+
value = await select({
|
|
20
|
+
message,
|
|
21
|
+
choices: enumValues.map((v) => ({ name: v, value: v }))
|
|
22
|
+
});
|
|
23
|
+
} else if (def.typeName === "ZodArray" && def.type?._def?.typeName === "ZodEnum") {
|
|
24
|
+
const elementDef = def.type._def;
|
|
25
|
+
const enumValues = elementDef.values || [];
|
|
26
|
+
value = await checkbox({
|
|
27
|
+
message,
|
|
28
|
+
choices: enumValues.map((v) => ({ name: v, value: v }))
|
|
29
|
+
});
|
|
30
|
+
} else {
|
|
31
|
+
value = await input({ message });
|
|
32
|
+
}
|
|
33
|
+
const result = fieldSchema.safeParse(value);
|
|
34
|
+
if (result.success) {
|
|
35
|
+
answers[key] = result.data;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
attempts++;
|
|
39
|
+
console.log(`Invalid value for ${key}: ${result.error.issues[0].message} (${attempts}/${maxRetries}). ${hint}`);
|
|
40
|
+
if (attempts >= maxRetries) {
|
|
41
|
+
throw new Error(`Too many invalid attempts for ${key}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return answers;
|
|
46
|
+
};
|
|
47
|
+
export {
|
|
48
|
+
zodPrompter
|
|
49
|
+
};
|