-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargs.ts
155 lines (136 loc) · 3.97 KB
/
args.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// deno-lint-ignore-file no-explicit-any
import { BaseContext } from "./command.ts";
import { dedent } from "./lib/dedent.ts";
import { Prettify } from "./lib/types.ts";
import { z } from "./z.ts";
import { zodProxy } from "./zod-proxy.ts";
/**
* Add arguments to a command.
*
* @param config - Configuration for the arguments.
*/
export function args(
config: ArgsConfig = {},
) {
const argsProps = {
short(context: any) {
let description: string | undefined;
if (typeof config.short === "function") {
description = config.short(context);
} else {
description = config.short;
}
return description && [...dedent(description)].join(" ");
},
long(context: any) {
let description: string | undefined;
if (typeof config.long === "function") {
description = config.long(context);
} else {
description = config.long;
}
return description && [...dedent(description)].join("\n");
},
usage(context: any) {
let usage: string | undefined;
if (typeof config.use === "function") {
usage = config.use(context);
} else {
usage = config.use;
}
return usage;
},
__args: true,
};
return zodProxy(z, argsProps) as Prettify<
& ArgTypes
& typeof argsProps
>;
}
export function isArgs(schema: unknown): schema is Args {
return (
schema instanceof z.ZodType && "__args" in schema
);
}
export function walkArgs(
args: unknown,
callback: (
arg: z.ZodTypeAny,
meta: { position: number; variadic: boolean },
) => void,
) {
const hasArgs = isArgs(args);
if (!hasArgs) {
return;
}
const isOptional = args instanceof z.ZodDefault ||
args instanceof z.ZodOptional;
const argsItems = isOptional && args._def.innerType instanceof z.ZodTuple
? [...args._def.innerType.items, args._def.innerType._def.rest].filter(
Boolean,
)
: args instanceof z.ZodTuple
? [...args.items, args._def.rest].filter(Boolean)
: isOptional && args._def.innerType instanceof z.ZodArray
? [args._def.innerType._def.type]
: args instanceof z.ZodArray
? [args._def.type]
: [];
if (argsItems.length) {
for (let i = 0; i < argsItems.length; i++) {
const item = argsItems[i];
const variadic =
!!(isOptional && args._def.innerType instanceof z.ZodTuple
? args._def.innerType._def.rest === item
: args instanceof z.ZodTuple
? args._def.rest === item
: isOptional && args._def.innerType instanceof z.ZodArray
? args._def.innerType
: args instanceof z.ZodArray
? args
: false);
callback(item, { position: i, variadic });
}
}
}
export type Args =
& ArgsZodTypes
& {
/**
* A short description of the args.
*/
short<Context extends BaseContext>(context: Context): string | undefined;
/**
* A long description of the args.
*/
long<Context extends BaseContext>(context: Context): string | undefined;
/**
* A usage string for the arguments.
*/
usage<Context extends BaseContext>(context: Context): string | undefined;
__args: true;
};
export type ArgsZodTypes =
| z.ZodTuple<any, any>
| z.ZodArray<any>
| z.ZodDefault<z.ZodTuple<any, any> | z.ZodArray<any>>
| z.ZodOptional<z.ZodTuple<any, any> | z.ZodArray<any>>;
export type ArgsConfig = {
/**
* A short description of the args.
*/
short?: string | (<Context extends BaseContext>(context: Context) => string);
/**
* A long description of the args.
*/
long?: string | (<Context extends BaseContext>(context: Context) => string);
/**
* A usage string for the arguments.
*/
use?: string | (<Context extends BaseContext>(context: Context) => string);
};
export type ArgTypes = Pick<typeof z, "tuple" | "array">;
export type inferArgs<T extends Args | ArgsZodTypes> = T extends
z.ZodOptional<z.ZodTuple<any, any> | z.ZodArray<any>>
? [] | NonNullable<T["_output"]>
: T["_output"];