padrone 1.0.0-beta.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/index.d.mts ADDED
@@ -0,0 +1,384 @@
1
+ import { StandardJSONSchemaV1, StandardSchemaV1 } from "@standard-schema/spec";
2
+ import { Tool } from "ai";
3
+
4
+ //#region src/formatter.d.ts
5
+ type HelpFormat = 'text' | 'ansi' | 'console' | 'markdown' | 'html' | 'json';
6
+ type HelpDetail = 'minimal' | 'standard' | 'full';
7
+ /**
8
+ * Information about a single positional argument.
9
+ */
10
+ type HelpArgumentInfo = {
11
+ name: string;
12
+ description?: string;
13
+ optional: boolean;
14
+ default?: unknown;
15
+ type?: string;
16
+ };
17
+ /**
18
+ * Information about a single option/flag.
19
+ */
20
+ type HelpOptionInfo = {
21
+ name: string;
22
+ description?: string;
23
+ optional: boolean;
24
+ default?: unknown;
25
+ type?: string;
26
+ enum?: string[];
27
+ aliases?: string[];
28
+ deprecated?: boolean | string;
29
+ hidden?: boolean;
30
+ examples?: unknown[];
31
+ /** Environment variable(s) this option can be set from */
32
+ env?: string | string[];
33
+ /** Whether this option is an array type (shown as <type...>) */
34
+ variadic?: boolean;
35
+ /** Whether this option is a boolean (shown as --[no-]option) */
36
+ negatable?: boolean;
37
+ /** Config file key that maps to this option */
38
+ configKey?: string;
39
+ };
40
+ /**
41
+ * Information about a subcommand (minimal info for listing).
42
+ */
43
+ type HelpSubcommandInfo = {
44
+ name: string;
45
+ title?: string;
46
+ description?: string;
47
+ deprecated?: boolean | string;
48
+ hidden?: boolean;
49
+ };
50
+ /**
51
+ * Comprehensive JSON structure for help information.
52
+ * This is the single source of truth that all formatters use.
53
+ */
54
+ type HelpInfo = {
55
+ /** The full command name (e.g., "cli serve" or "<root>") */
56
+ name: string;
57
+ /** Short title for the command */
58
+ title?: string;
59
+ /** Command description */
60
+ description?: string;
61
+ /** Whether the command is deprecated */
62
+ deprecated?: boolean | string;
63
+ /** Whether the command is hidden */
64
+ hidden?: boolean;
65
+ /** Usage string parts for flexible formatting */
66
+ usage: {
67
+ command: string;
68
+ hasSubcommands: boolean;
69
+ hasArguments: boolean;
70
+ hasOptions: boolean;
71
+ };
72
+ /** List of subcommands */
73
+ subcommands?: HelpSubcommandInfo[];
74
+ /** Positional arguments */
75
+ arguments?: HelpArgumentInfo[];
76
+ /** Options/flags (only visible ones, hidden filtered out) */
77
+ options?: HelpOptionInfo[];
78
+ /** Full help info for nested commands (used in 'full' detail mode) */
79
+ nestedCommands?: HelpInfo[];
80
+ };
81
+ //#endregion
82
+ //#region src/help.d.ts
83
+ type HelpOptions = {
84
+ format?: HelpFormat | 'auto';
85
+ detail?: HelpDetail;
86
+ };
87
+ //#endregion
88
+ //#region src/options.d.ts
89
+ interface PadroneOptionsMeta {
90
+ description?: string;
91
+ alias?: string[] | string;
92
+ deprecated?: boolean | string;
93
+ hidden?: boolean;
94
+ examples?: unknown[];
95
+ /**
96
+ * Environment variable name(s) to bind this option to.
97
+ * Can be a single string or array of env var names (checked in order).
98
+ */
99
+ env?: string | string[];
100
+ /**
101
+ * Key path in config file that maps to this option.
102
+ * Supports dot notation for nested keys (e.g., 'server.port').
103
+ */
104
+ configKey?: string;
105
+ }
106
+ type PositionalArgs<TObj> = TObj extends Record<string, any> ? { [K in keyof TObj]: TObj[K] extends Array<any> ? `...${K & string}` : K & string }[keyof TObj] : string;
107
+ /**
108
+ * Meta configuration for options including positional arguments.
109
+ * The `positional` array defines which options are positional arguments and their order.
110
+ * Use '...name' prefix to indicate variadic (rest) arguments, matching JS/TS rest syntax.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * .options(schema, {
115
+ * positional: ['source', '...files', 'dest'], // '...files' is variadic
116
+ * })
117
+ * ```
118
+ */
119
+ interface PadroneMeta<TObj = Record<string, any>> {
120
+ /**
121
+ * Array of option names that should be treated as positional arguments.
122
+ * Order in array determines position. Use '...name' prefix for variadic args.
123
+ * @example ['source', '...files', 'dest'] - 'files' captures multiple values
124
+ */
125
+ positional?: PositionalArgs<TObj>[];
126
+ /**
127
+ * Per-option metadata.
128
+ */
129
+ options?: { [K in keyof TObj]?: PadroneOptionsMeta };
130
+ }
131
+ //#endregion
132
+ //#region src/type-utils.d.ts
133
+ type SafeString = string & {};
134
+ type IsUnknown<T> = unknown extends T ? true : false;
135
+ type IsAny<T> = any extends T ? true : false;
136
+ type IsNever<T> = [T] extends [never] ? true : false;
137
+ type SplitString<TName extends string, TSplitBy extends string = ' '> = TName extends `${infer FirstPart}${TSplitBy}${infer RestParts}` ? [FirstPart, ...SplitString<RestParts, TSplitBy>] : [TName];
138
+ type JoinString<TParts extends string[], TJoinBy extends string = ' '> = TParts extends [infer FirstPart extends string, ...infer RestParts extends string[]] ? RestParts extends [] ? FirstPart : `${FirstPart}${TJoinBy}${JoinString<RestParts, TJoinBy>}` : TParts extends [] ? '' : TParts[number];
139
+ type SplitLastSpace<S extends string> = SplitString<S> extends [...infer Init extends string[], infer Last extends string] ? Init extends [] ? [S, never] : [JoinString<Init>, Last] : [S, never];
140
+ type AnyPartExtends<U, T> = [U] extends [never] ? false : U extends any ? (U extends T ? true : never) : never extends true ? true : false;
141
+ type FullCommandName<TName extends string, TParentName extends string = ''> = TParentName extends '' ? TName : `${TParentName} ${TName}`;
142
+ type PickCommandByName<TCommands extends AnyPadroneCommand[], TName extends string | AnyPadroneCommand> = TName extends AnyPadroneCommand ? TName : Extract<FlattenCommands<TCommands>, {
143
+ path: TName;
144
+ }>;
145
+ type FlattenCommands<TCommands extends AnyPadroneCommand[]> = TCommands extends [infer FirstCommand, ...infer RestCommands] ? FirstCommand extends AnyPadroneCommand ? (RestCommands extends AnyPadroneCommand[] ? FlattenCommands<RestCommands> : never) | FlattenCommands<FirstCommand['~types']['commands']> | FirstCommand : RestCommands extends AnyPadroneCommand[] ? FlattenCommands<RestCommands> : never : TCommands[number];
146
+ type GetCommandNames<TCommands extends AnyPadroneCommand[]> = FlattenCommands<TCommands>['path'];
147
+ /**
148
+ * Find all the commands that are prefixed with a command name.
149
+ * This is needed to avoid matching other commands when followed by a space and another word.
150
+ * For example, let's say `level1` and `level1 level2` are commands.
151
+ * Then `level1 ${string}` would also match `level1 level2`,
152
+ * and it would cause `level1 level2` to not show up in the autocomplete.
153
+ * By excluding those cases, we can ensure autocomplete works correctly.
154
+ */
155
+ type PrefixedCommands<TCommands extends AnyPadroneCommand[]> = GetCommandNames<TCommands> extends infer CommandNames ? CommandNames extends string ? AnyPartExtends<GetCommandNames<TCommands>, `${CommandNames} ${string}`> extends true ? never : `${CommandNames} ${string}` : never : never;
156
+ /**
157
+ * The possible commands are the commands that can be parsed by the program.
158
+ * This includes the string that are exact matches to a command name, and strings that are prefixed with a command name.
159
+ */
160
+ type PossibleCommands<TCommands extends AnyPadroneCommand[]> = GetCommandNames<TCommands> | PrefixedCommands<TCommands> | SafeString;
161
+ /**
162
+ * Match a string to a command by the possible commands.
163
+ * This is done by recursively splitting the string by the last space, and then checking if the prefix is a valid command name.
164
+ * This is needed to avoid matching the top-level command when there are nested commands.
165
+ */
166
+ type PickCommandByPossibleCommands<TCommands extends AnyPadroneCommand[], TCommand extends PossibleCommands<TCommands>> = IsAny<TCommand> extends true ? TCommands[number] : TCommand extends GetCommandNames<TCommands> ? PickCommandByName<TCommands, TCommand> : SplitLastSpace<TCommand> extends [infer Prefix extends string, infer Rest] ? IsNever<Rest> extends true ? PickCommandByName<TCommands, Prefix> : PickCommandByPossibleCommands<TCommands, Prefix> : never;
167
+ //#endregion
168
+ //#region src/types.d.ts
169
+ type UnknownRecord = Record<string, unknown>;
170
+ type EmptyRecord = Record<string, never>;
171
+ type DefaultOpts = UnknownRecord | void;
172
+ /**
173
+ * A schema that supports both validation (StandardSchemaV1) and JSON schema generation (StandardJSONSchemaV1).
174
+ * This is the type required for command arguments and options in Padrone.
175
+ */
176
+ type PadroneSchema<Input = unknown, Output = Input> = StandardSchemaV1<Input, Output> & StandardJSONSchemaV1<Input, Output>;
177
+ type PadroneCommand<TName extends string = string, TParentName extends string = '', TOpts extends PadroneSchema = PadroneSchema<DefaultOpts>, TRes = void, TCommands extends [...AnyPadroneCommand[]] = []> = {
178
+ name: TName;
179
+ path: FullCommandName<TName, TParentName>;
180
+ title?: string;
181
+ description?: string;
182
+ version?: string;
183
+ deprecated?: boolean | string;
184
+ hidden?: boolean;
185
+ needsApproval?: boolean | ((options: TOpts) => Promise<boolean> | boolean);
186
+ options?: PadroneSchema;
187
+ meta?: GetMeta<TOpts>;
188
+ handler?: (options: StandardSchemaV1.InferOutput<TOpts>) => TRes;
189
+ /** List of possible config file names to search for. */
190
+ configFiles?: string[];
191
+ parent?: AnyPadroneCommand;
192
+ commands?: TCommands;
193
+ /** @deprecated Internal use only */
194
+ '~types': {
195
+ name: TName;
196
+ parentName: TParentName;
197
+ path: FullCommandName<TName, TParentName>;
198
+ optionsInput: StandardSchemaV1.InferInput<TOpts>;
199
+ optionsOutput: StandardSchemaV1.InferOutput<TOpts>;
200
+ result: TRes;
201
+ commands: TCommands;
202
+ };
203
+ };
204
+ type AnyPadroneCommand = PadroneCommand<string, any, any, any, [...AnyPadroneCommand[]]>;
205
+ /**
206
+ * Configuration options for a command.
207
+ */
208
+ type PadroneCommandConfig = {
209
+ /** A short title for the command, displayed in help. */
210
+ title?: string;
211
+ /** A longer description of what the command does. */
212
+ description?: string;
213
+ /** The version of the command. */
214
+ version?: string;
215
+ /** Whether the command is deprecated, or a message explaining the deprecation. */
216
+ deprecated?: boolean | string;
217
+ /** Whether the command should be hidden from help output. */
218
+ hidden?: boolean;
219
+ /**
220
+ * List of possible config file names to search for.
221
+ * When the CLI runs, it will search for these files in the current directory
222
+ * and apply the first one found.
223
+ *
224
+ * - `undefined`: Inherit from parent command (default)
225
+ * - `['file1', 'file2']`: Use these config files
226
+ * - `[]`: Explicitly disable config file loading (no inheritance)
227
+ *
228
+ * @example ['myapp.config.json', 'myapp.config.yaml', '.myapprc']
229
+ */
230
+ configFiles?: string[];
231
+ };
232
+ type PadroneCommandBuilder<TName extends string = string, TParentName extends string = '', TOpts extends PadroneSchema = PadroneSchema<DefaultOpts>, TRes = void, TCommands extends [...AnyPadroneCommand[]] = []> = {
233
+ /**
234
+ * Configures command properties like title, description, version, deprecated, and hidden.
235
+ * @example
236
+ * ```ts
237
+ * .configure({
238
+ * title: 'Build Project',
239
+ * description: 'Compiles the project',
240
+ * deprecated: 'Use "compile" instead',
241
+ * })
242
+ * ```
243
+ */
244
+ configure: (config: PadroneCommandConfig) => PadroneCommandBuilder<TName, TParentName, TOpts, TRes, TCommands>;
245
+ /**
246
+ * Defines the options schema for the command, including positional arguments.
247
+ * Use the `positional` array in meta to specify which options are positional args.
248
+ * Use '...name' prefix for variadic (rest) arguments, matching JS/TS rest syntax.
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * .options(z.object({
253
+ * source: z.string(),
254
+ * files: z.string().array(),
255
+ * dest: z.string(),
256
+ * recursive: z.boolean().default(false),
257
+ * }), {
258
+ * positional: ['source', '...files', 'dest'],
259
+ * })
260
+ * ```
261
+ */
262
+ options: <TOpts extends PadroneSchema = PadroneSchema<void>>(options?: TOpts, meta?: GetMeta<TOpts>) => PadroneCommandBuilder<TName, TParentName, TOpts, TRes, TCommands>;
263
+ /**
264
+ * Defines the handler function to be executed when the command is run.
265
+ */
266
+ action: <TRes>(handler?: (options: StandardSchemaV1.InferOutput<TOpts>) => TRes) => PadroneCommandBuilder<TName, TParentName, TOpts, TRes, TCommands>;
267
+ /**
268
+ * Creates a nested command within the current command with the given name and builder function.
269
+ */
270
+ command: <TNameNested extends string, TBuilder extends PadroneCommandBuilder<TNameNested, FullCommandName<TName, TParentName>, any, any, any>>(name: TNameNested, builderFn?: (builder: PadroneCommandBuilder<TNameNested, FullCommandName<TName, TParentName>>) => TBuilder) => PadroneCommandBuilder<TName, TParentName, TOpts, TRes, [...TCommands, TBuilder['~types']['command']]>;
271
+ /** @deprecated Internal use only */
272
+ '~types': {
273
+ name: TName;
274
+ parentName: TParentName;
275
+ path: FullCommandName<TName, TParentName>;
276
+ options: TOpts;
277
+ result: TRes;
278
+ commands: TCommands;
279
+ command: PadroneCommand<TName, TParentName, TOpts, TRes, TCommands>;
280
+ };
281
+ };
282
+ type PadroneProgram<TName extends string = string, TOpts extends PadroneSchema = PadroneSchema<DefaultOpts>, TRes = void, TCommands extends [...AnyPadroneCommand[]] = [], TCmd extends PadroneCommand<'', '', TOpts, TRes, TCommands> = PadroneCommand<'', '', TOpts, TRes, TCommands>> = Omit<PadroneCommandBuilder<TName, '', TOpts, TRes, TCommands>, 'command' | 'configure'> & {
283
+ /**
284
+ * Configures program properties like title, description, version, deprecated, hidden, and configFiles.
285
+ * @example
286
+ * ```ts
287
+ * .configure({
288
+ * description: 'My CLI application',
289
+ * version: '1.0.0',
290
+ * configFiles: ['myapp.config.json', '.myapprc'],
291
+ * })
292
+ * ```
293
+ */
294
+ configure: (config: PadroneCommandConfig) => PadroneProgram<TName, TOpts, TRes, TCommands>;
295
+ /**
296
+ * Creates a command within the program with the given name and builder function.
297
+ */
298
+ command: <TNameNested extends string, TBuilder extends PadroneCommandBuilder<TNameNested, '', any, any, any>>(name: TNameNested, builderFn?: (builder: PadroneCommandBuilder<TNameNested, ''>) => TBuilder) => PadroneProgram<TName, TOpts, TRes, [...TCommands, TBuilder['~types']['command']]>;
299
+ /**
300
+ * Runs a command programmatically by name with provided options (including positional args).
301
+ */
302
+ run: <const TCommand extends GetCommandNames<[TCmd]> | FlattenCommands<[TCmd]>>(name: TCommand, options: NoInfer<GetOptions<'in', PickCommandByName<[TCmd], TCommand>>>) => PadroneCommandResult<PickCommandByName<[TCmd], TCommand>>;
303
+ /**
304
+ * Runs the program as a CLI application, parsing `process.argv` or provided input.
305
+ */
306
+ cli: <const TCommand extends PossibleCommands<[TCmd]>>(input?: TCommand, options?: PadroneParseOptions) => PadroneCommandResult<PickCommandByPossibleCommands<[TCmd], TCommand>>;
307
+ /**
308
+ * Parses CLI input (or the provided input string) into command, args, and options without executing anything.
309
+ */
310
+ parse: <const TCommand extends PossibleCommands<[TCmd]>>(input?: TCommand, options?: PadroneParseOptions) => PadroneParseResult<PickCommandByPossibleCommands<[TCmd], TCommand>>;
311
+ /**
312
+ * Converts command and options back into a CLI string.
313
+ */
314
+ stringify: <const TCommand extends GetCommandNames<TCommands> = ''>(command?: TCommand, options?: GetOptions<'out', PickCommandByPossibleCommands<[TCmd], TCommand>>) => string;
315
+ /**
316
+ * Finds a command by name, returning `undefined` if not found.
317
+ */
318
+ find: <const TName extends GetCommandNames<[TCmd]>>(command: TName | (string & {})) => IsUnknown<TName> extends false ? TName extends string ? PickCommandByName<[TCmd], TName> : FlattenCommands<[TCmd]> | undefined : FlattenCommands<[TCmd]> | undefined;
319
+ /**
320
+ * Generates a type-safe API for invoking commands programmatically.
321
+ */
322
+ api: () => PadroneAPI<TCmd>;
323
+ /**
324
+ * Starts an interactive prompt to run commands.
325
+ */
326
+ /**
327
+ * Starts a REPL (Read-Eval-Print Loop) for running commands interactively.
328
+ */
329
+ /**
330
+ * Returns a tool definition that can be passed to AI SDK.
331
+ */
332
+ tool: () => Tool<{
333
+ command: string;
334
+ }>;
335
+ /**
336
+ * Returns the help information for the program or a specific command.
337
+ */
338
+ help: <const TCommand extends GetCommandNames<[TCmd]> | FlattenCommands<[TCmd]>>(command?: TCommand, options?: HelpOptions) => string;
339
+ /**
340
+ * Reflection information about the program.
341
+ * Avoid using this in application code, unless you know what you're doing.
342
+ * @deprecated Internal use only
343
+ */
344
+ '~types': {
345
+ command: TCmd;
346
+ commands: TCommands;
347
+ };
348
+ };
349
+ type AnyPadroneProgram = PadroneProgram<string, any, any, [...AnyPadroneCommand[]]>;
350
+ type PadroneCommandResult<TCommand extends AnyPadroneCommand = AnyPadroneCommand> = PadroneParseResult<TCommand> & {
351
+ result: GetResults<TCommand>;
352
+ };
353
+ /**
354
+ * Options for parsing CLI input.
355
+ */
356
+ type PadroneParseOptions = {
357
+ /**
358
+ * Custom environment variables to use for env binding.
359
+ * If not provided, process.env will be used.
360
+ */
361
+ env?: Record<string, string | undefined>;
362
+ /**
363
+ * Config file data to use for config binding.
364
+ * This should be the parsed content of a config file (JSON, YAML, etc.).
365
+ */
366
+ configData?: Record<string, unknown>;
367
+ };
368
+ type PadroneParseResult<TCommand extends AnyPadroneCommand = AnyPadroneCommand> = {
369
+ command: TCommand;
370
+ options?: GetOptions<'out', TCommand>;
371
+ optionsResult?: StandardSchemaV1.Result<GetOptions<'out', TCommand>>;
372
+ };
373
+ type PadroneAPI<TCommand extends AnyPadroneCommand> = PadroneAPICommand<TCommand> & { [K in TCommand['~types']['commands'][number] as K['name']]: PadroneAPI<K> };
374
+ type PadroneAPICommand<TCommand extends AnyPadroneCommand> = (options: GetOptions<'in', TCommand>) => GetResults<TCommand>;
375
+ type NormalizeOptions<TOptions> = IsUnknown<TOptions> extends true ? void | EmptyRecord : TOptions;
376
+ type GetOptions<TDir extends 'in' | 'out', TCommand extends AnyPadroneCommand> = TDir extends 'in' ? NormalizeOptions<TCommand['~types']['optionsInput']> : NormalizeOptions<TCommand['~types']['optionsOutput']>;
377
+ type GetResults<TCommand extends AnyPadroneCommand> = ReturnType<NonNullable<TCommand['handler']>>;
378
+ type GetMeta<TOpts extends PadroneSchema> = PadroneMeta<NonNullable<StandardSchemaV1.InferInput<TOpts>>>;
379
+ //#endregion
380
+ //#region src/index.d.ts
381
+ declare function createPadrone(name: string): PadroneProgram;
382
+ //#endregion
383
+ export { type AnyPadroneCommand, type AnyPadroneProgram, type HelpArgumentInfo, type HelpFormat, type HelpInfo, type HelpOptionInfo, type HelpOptions, type HelpSubcommandInfo, type PadroneCommand, type PadroneCommandBuilder, type PadroneCommandConfig, type PadroneCommandResult, type PadroneOptionsMeta, type PadroneParseOptions, type PadroneParseResult, type PadroneProgram, createPadrone };
384
+ //# sourceMappingURL=index.d.mts.map