padrone 1.1.0 → 1.3.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.
Files changed (80) hide show
  1. package/CHANGELOG.md +97 -1
  2. package/LICENSE +1 -1
  3. package/README.md +60 -30
  4. package/dist/args-DFEI7_G_.mjs +197 -0
  5. package/dist/args-DFEI7_G_.mjs.map +1 -0
  6. package/dist/chunk-y_GBKt04.mjs +5 -0
  7. package/dist/codegen/index.d.mts +305 -0
  8. package/dist/codegen/index.d.mts.map +1 -0
  9. package/dist/codegen/index.mjs +1358 -0
  10. package/dist/codegen/index.mjs.map +1 -0
  11. package/dist/completion.d.mts +64 -0
  12. package/dist/completion.d.mts.map +1 -0
  13. package/dist/completion.mjs +417 -0
  14. package/dist/completion.mjs.map +1 -0
  15. package/dist/docs/index.d.mts +34 -0
  16. package/dist/docs/index.d.mts.map +1 -0
  17. package/dist/docs/index.mjs +405 -0
  18. package/dist/docs/index.mjs.map +1 -0
  19. package/dist/formatter-XroimS3Q.d.mts +83 -0
  20. package/dist/formatter-XroimS3Q.d.mts.map +1 -0
  21. package/dist/help-CgGP7hQU.mjs +1229 -0
  22. package/dist/help-CgGP7hQU.mjs.map +1 -0
  23. package/dist/index.d.mts +120 -546
  24. package/dist/index.d.mts.map +1 -1
  25. package/dist/index.mjs +1220 -1204
  26. package/dist/index.mjs.map +1 -1
  27. package/dist/test.d.mts +112 -0
  28. package/dist/test.d.mts.map +1 -0
  29. package/dist/test.mjs +138 -0
  30. package/dist/test.mjs.map +1 -0
  31. package/dist/types-BS7RP5Ls.d.mts +1059 -0
  32. package/dist/types-BS7RP5Ls.d.mts.map +1 -0
  33. package/dist/update-check-EbNDkzyV.mjs +146 -0
  34. package/dist/update-check-EbNDkzyV.mjs.map +1 -0
  35. package/package.json +61 -21
  36. package/src/args.ts +457 -0
  37. package/src/cli/completions.ts +29 -0
  38. package/src/cli/docs.ts +86 -0
  39. package/src/cli/doctor.ts +330 -0
  40. package/src/cli/index.ts +159 -0
  41. package/src/cli/init.ts +135 -0
  42. package/src/cli/link.ts +320 -0
  43. package/src/cli/wrap.ts +152 -0
  44. package/src/codegen/README.md +118 -0
  45. package/src/codegen/code-builder.ts +226 -0
  46. package/src/codegen/discovery.ts +232 -0
  47. package/src/codegen/file-emitter.ts +73 -0
  48. package/src/codegen/generators/barrel-file.ts +16 -0
  49. package/src/codegen/generators/command-file.ts +197 -0
  50. package/src/codegen/generators/command-tree.ts +124 -0
  51. package/src/codegen/index.ts +33 -0
  52. package/src/codegen/parsers/fish.ts +163 -0
  53. package/src/codegen/parsers/help.ts +378 -0
  54. package/src/codegen/parsers/merge.ts +158 -0
  55. package/src/codegen/parsers/zsh.ts +221 -0
  56. package/src/codegen/schema-to-code.ts +199 -0
  57. package/src/codegen/template.ts +69 -0
  58. package/src/codegen/types.ts +143 -0
  59. package/src/colorizer.ts +2 -2
  60. package/src/command-utils.ts +504 -0
  61. package/src/completion.ts +110 -97
  62. package/src/create.ts +1048 -308
  63. package/src/docs/index.ts +607 -0
  64. package/src/errors.ts +131 -0
  65. package/src/formatter.ts +195 -73
  66. package/src/help.ts +159 -58
  67. package/src/index.ts +12 -15
  68. package/src/interactive.ts +169 -0
  69. package/src/parse.ts +52 -21
  70. package/src/repl-loop.ts +317 -0
  71. package/src/runtime.ts +304 -0
  72. package/src/shell-utils.ts +83 -0
  73. package/src/test.ts +285 -0
  74. package/src/type-helpers.ts +10 -10
  75. package/src/type-utils.ts +124 -14
  76. package/src/types.ts +752 -154
  77. package/src/update-check.ts +244 -0
  78. package/src/wrap.ts +44 -40
  79. package/src/zod.d.ts +2 -2
  80. package/src/options.ts +0 -180
package/src/args.ts ADDED
@@ -0,0 +1,457 @@
1
+ import type { StandardJSONSchemaV1 } from '@standard-schema/spec';
2
+
3
+ type Letter =
4
+ | 'a'
5
+ | 'b'
6
+ | 'c'
7
+ | 'd'
8
+ | 'e'
9
+ | 'f'
10
+ | 'g'
11
+ | 'h'
12
+ | 'i'
13
+ | 'j'
14
+ | 'k'
15
+ | 'l'
16
+ | 'm'
17
+ | 'n'
18
+ | 'o'
19
+ | 'p'
20
+ | 'q'
21
+ | 'r'
22
+ | 's'
23
+ | 't'
24
+ | 'u'
25
+ | 'v'
26
+ | 'w'
27
+ | 'x'
28
+ | 'y'
29
+ | 'z';
30
+
31
+ /** A single letter character, valid as a short CLI flag (e.g. `'v'`, `'n'`, `'V'`). */
32
+ export type SingleChar = Letter | Uppercase<Letter>;
33
+
34
+ export interface PadroneFieldMeta {
35
+ description?: string;
36
+ /** Single-character short flags (stackable: `-abc` = `-a -b -c`). Used with single dash. */
37
+ flags?: SingleChar[] | SingleChar;
38
+ /** Multi-character alternative long names. Used with double dash (e.g. `--dry-run` for `--dryRun`). */
39
+ alias?: string[] | string;
40
+ deprecated?: boolean | string;
41
+ hidden?: boolean;
42
+ examples?: unknown[];
43
+ }
44
+
45
+ type PositionalArgs<TObj> =
46
+ TObj extends Record<string, any>
47
+ ? {
48
+ [K in keyof TObj]: TObj[K] extends Array<any> ? `...${K & string}` : K & string;
49
+ }[keyof TObj]
50
+ : string;
51
+
52
+ /**
53
+ * Meta configuration for arguments, including positional arguments.
54
+ * The `positional` array defines which arguments are positional and their order.
55
+ * Use '...name' prefix to indicate variadic (rest) arguments, matching JS/TS rest syntax.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * .arguments(schema, {
60
+ * positional: ['source', '...files', 'dest'], // '...files' is variadic
61
+ * })
62
+ * ```
63
+ */
64
+ /**
65
+ * Configuration for reading from stdin and mapping it to an argument field.
66
+ */
67
+ export type StdinConfig<TObj = Record<string, any>> =
68
+ | (keyof TObj & string)
69
+ | {
70
+ /** The argument field to populate with stdin data. */
71
+ field: keyof TObj & string;
72
+ /**
73
+ * How to consume stdin:
74
+ * - `'text'` (default): read all stdin as a single string.
75
+ * - `'lines'`: read stdin as an array of lines (string[]).
76
+ */
77
+ as?: 'text' | 'lines';
78
+ };
79
+
80
+ export interface PadroneArgsSchemaMeta<TObj = Record<string, any>> {
81
+ /**
82
+ * Array of argument names that should be treated as positional arguments.
83
+ * Order in array determines position. Use '...name' prefix for variadic args.
84
+ * @example ['source', '...files', 'dest'] - 'files' captures multiple values
85
+ */
86
+ positional?: PositionalArgs<TObj>[];
87
+ /**
88
+ * Per-argument metadata.
89
+ */
90
+ fields?: { [K in keyof TObj]?: PadroneFieldMeta };
91
+ /**
92
+ * Automatically generate kebab-case aliases for camelCase option names.
93
+ * For example, `dryRun` automatically gets `--dry-run` as an alias.
94
+ * Defaults to `true`. Set to `false` to disable.
95
+ *
96
+ * @default true
97
+ * @example
98
+ * ```ts
99
+ * // Auto-aliases enabled (default): --dry-run → dryRun
100
+ * .arguments(z.object({ dryRun: z.boolean() }))
101
+ *
102
+ * // Disable auto-aliases
103
+ * .arguments(z.object({ dryRun: z.boolean() }), { autoAlias: false })
104
+ * ```
105
+ */
106
+ autoAlias?: boolean;
107
+ /**
108
+ * Read from stdin and inject the data into the specified argument field.
109
+ * Only reads when stdin is piped (not a TTY) and the field wasn't already provided via CLI flags.
110
+ *
111
+ * - `string`: shorthand for `{ field: name, as: 'text' }` — read all stdin as a string.
112
+ * - `{ field, as }`: explicit form. `as: 'text'` reads all stdin as a string,
113
+ * `as: 'lines'` reads stdin as an array of line strings.
114
+ *
115
+ * Precedence: CLI flags > stdin > env vars > config file > schema defaults.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * // Shorthand: read all stdin as text into 'data' field
120
+ * .arguments(z.object({ data: z.string() }), { stdin: 'data' })
121
+ *
122
+ * // Explicit: read stdin lines into 'lines' field
123
+ * .arguments(z.object({ lines: z.string().array() }), {
124
+ * stdin: { field: 'lines', as: 'lines' },
125
+ * })
126
+ * ```
127
+ */
128
+ stdin?: StdinConfig<TObj>;
129
+ /**
130
+ * Fields to interactively prompt for when their values are missing after CLI/env/config resolution.
131
+ * - `true`: prompt for all required fields that are missing.
132
+ * - `string[]`: prompt for these specific fields if missing.
133
+ *
134
+ * Interactive prompting only occurs in `cli()` when the runtime has `interactive: true`.
135
+ * Setting this makes `parse()` and `cli()` return Promises.
136
+ *
137
+ * @example
138
+ * ```ts
139
+ * .arguments(schema, {
140
+ * interactive: true, // prompt all missing required fields
141
+ * interactive: ['name', 'template'], // prompt only these fields
142
+ * })
143
+ * ```
144
+ */
145
+ interactive?: true | (keyof TObj & string)[];
146
+ /**
147
+ * Optional fields offered after required interactive prompts.
148
+ * Users are shown a multi-select to choose which of these fields to configure.
149
+ * - `true`: offer all optional fields that are missing.
150
+ * - `string[]`: offer these specific fields.
151
+ *
152
+ * @example
153
+ * ```ts
154
+ * .arguments(schema, {
155
+ * interactive: ['name'],
156
+ * optionalInteractive: ['typescript', 'eslint', 'prettier'],
157
+ * })
158
+ * ```
159
+ */
160
+ optionalInteractive?: true | (keyof TObj & string)[];
161
+ }
162
+
163
+ /**
164
+ * Convert a camelCase string to kebab-case.
165
+ * Returns null if the string has no uppercase letters (no conversion needed).
166
+ */
167
+ export function camelToKebab(str: string): string | null {
168
+ if (!/[A-Z]/.test(str)) return null;
169
+ return str.replace(/[A-Z]/g, (ch) => `-${ch.toLowerCase()}`);
170
+ }
171
+
172
+ /**
173
+ * Normalizes stdin config into its explicit form.
174
+ */
175
+ export function parseStdinConfig(stdin: StdinConfig): { field: string; as: 'text' | 'lines' } {
176
+ if (typeof stdin === 'string') return { field: stdin, as: 'text' };
177
+ return { field: stdin.field as string, as: stdin.as ?? 'text' };
178
+ }
179
+
180
+ /**
181
+ * Parse positional configuration to extract names and variadic info.
182
+ */
183
+ export function parsePositionalConfig(positional: string[]): { name: string; variadic: boolean }[] {
184
+ return positional.map((p) => {
185
+ const isVariadic = p.startsWith('...');
186
+ const name = isVariadic ? p.slice(3) : p;
187
+ return { name, variadic: isVariadic };
188
+ });
189
+ }
190
+
191
+ /**
192
+ * Result type for extractSchemaMetadata function.
193
+ */
194
+ interface SchemaMetadataResult {
195
+ /** Single-char flags: maps flag char → full arg name (e.g. `{ v: 'verbose' }`) */
196
+ flags: Record<string, string>;
197
+ /** Multi-char aliases: maps alias → full arg name (e.g. `{ 'dry-run': 'dryRun' }`) */
198
+ aliases: Record<string, string>;
199
+ }
200
+
201
+ function addEntries(target: Record<string, string>, key: string, items: string | string[], filter?: (item: string) => boolean) {
202
+ const list = typeof items === 'string' ? [items] : items;
203
+ for (const item of list) {
204
+ if (typeof item === 'string' && item && item !== key && !(item in target) && (!filter || filter(item))) {
205
+ target[item] = key;
206
+ }
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Extract all arg metadata from schema and meta in a single pass.
212
+ * Returns flags (single-char, stackable) and aliases (multi-char, long names) separately.
213
+ * When `autoAlias` is true (default), camelCase property names automatically get kebab-case aliases.
214
+ */
215
+ export function extractSchemaMetadata(
216
+ schema: StandardJSONSchemaV1,
217
+ meta?: Record<string, PadroneFieldMeta | undefined>,
218
+ autoAlias?: boolean,
219
+ ): SchemaMetadataResult {
220
+ const flags: Record<string, string> = {};
221
+ const aliases: Record<string, string> = {};
222
+
223
+ // Extract from meta object
224
+ if (meta) {
225
+ for (const [key, value] of Object.entries(meta)) {
226
+ if (!value) continue;
227
+
228
+ if (value.flags) {
229
+ addEntries(flags, key, value.flags, (item) => item.length === 1);
230
+ }
231
+ if (value.alias) {
232
+ addEntries(aliases, key, value.alias, (item) => item.length > 1);
233
+ }
234
+ }
235
+ }
236
+
237
+ // Extract from JSON schema properties
238
+ try {
239
+ const jsonSchema = schema['~standard'].jsonSchema.input({ target: 'draft-2020-12' }) as Record<string, any>;
240
+ if (jsonSchema.type === 'object' && jsonSchema.properties) {
241
+ for (const [propertyName, propertySchema] of Object.entries(jsonSchema.properties as Record<string, any>)) {
242
+ if (!propertySchema) continue;
243
+
244
+ // Extract flags from schema `.meta({ flags: ... })`
245
+ const propFlags = propertySchema.flags;
246
+ if (propFlags) {
247
+ addEntries(flags, propertyName, propFlags, (item) => item.length === 1);
248
+ }
249
+
250
+ // Extract aliases from schema `.meta({ alias: ... })`
251
+ const propAlias = propertySchema.alias;
252
+ if (propAlias) {
253
+ const list = typeof propAlias === 'string' ? [propAlias] : propAlias;
254
+ if (Array.isArray(list)) {
255
+ addEntries(aliases, propertyName, list, (item) => item.length > 1);
256
+ }
257
+ }
258
+
259
+ // Auto-generate kebab-case alias for camelCase property names
260
+ if (autoAlias !== false) {
261
+ const kebab = camelToKebab(propertyName);
262
+ if (kebab && !(kebab in aliases)) {
263
+ aliases[kebab] = propertyName;
264
+ }
265
+ }
266
+ }
267
+ }
268
+ } catch {
269
+ // Ignore errors from JSON schema generation
270
+ }
271
+
272
+ return { flags, aliases };
273
+ }
274
+
275
+ function preprocessMappings(data: Record<string, unknown>, mappings: Record<string, string>): Record<string, unknown> {
276
+ const result = { ...data };
277
+
278
+ for (const [mappedKey, fullArgName] of Object.entries(mappings)) {
279
+ if (mappedKey in data && mappedKey !== fullArgName) {
280
+ const mappedValue = data[mappedKey];
281
+ // Prefer full arg name if it exists
282
+ if (!(fullArgName in result)) result[fullArgName] = mappedValue;
283
+ delete result[mappedKey];
284
+ }
285
+ }
286
+
287
+ return result;
288
+ }
289
+
290
+ interface ParseArgsContext {
291
+ flags?: Record<string, string>;
292
+ aliases?: Record<string, string>;
293
+ stdinData?: Record<string, unknown>;
294
+ envData?: Record<string, unknown>;
295
+ configData?: Record<string, unknown>;
296
+ }
297
+
298
+ /**
299
+ * Apply values directly to arguments.
300
+ * CLI values take precedence over the provided values.
301
+ */
302
+ function applyValues(data: Record<string, unknown>, values: Record<string, unknown>): Record<string, unknown> {
303
+ const result = { ...data };
304
+
305
+ for (const [key, value] of Object.entries(values)) {
306
+ // Only apply value if arg wasn't already set
307
+ if (key in result && result[key] !== undefined) continue;
308
+ if (value !== undefined) {
309
+ result[key] = value;
310
+ }
311
+ }
312
+
313
+ return result;
314
+ }
315
+
316
+ /**
317
+ * Combined preprocessing of arguments with all features.
318
+ * Precedence order (highest to lowest): CLI args > stdin > env vars > config file
319
+ */
320
+ export function preprocessArgs(data: Record<string, unknown>, ctx: ParseArgsContext): Record<string, unknown> {
321
+ let result = { ...data };
322
+
323
+ // 1. Apply flags and aliases first
324
+ if (ctx.flags && Object.keys(ctx.flags).length > 0) {
325
+ result = preprocessMappings(result, ctx.flags);
326
+ }
327
+ if (ctx.aliases && Object.keys(ctx.aliases).length > 0) {
328
+ result = preprocessMappings(result, ctx.aliases);
329
+ }
330
+
331
+ // 2. Apply stdin data (higher precedence than env)
332
+ // Only applies if CLI didn't set the arg
333
+ if (ctx.stdinData) {
334
+ result = applyValues(result, ctx.stdinData);
335
+ }
336
+
337
+ // 3. Apply environment variables (higher precedence than config)
338
+ // These only apply if CLI/stdin didn't set the arg
339
+ if (ctx.envData) {
340
+ result = applyValues(result, ctx.envData);
341
+ }
342
+
343
+ // 4. Apply config file values (lowest precedence)
344
+ // These only apply if neither CLI, stdin, nor env set the arg
345
+ if (ctx.configData) {
346
+ result = applyValues(result, ctx.configData);
347
+ }
348
+
349
+ return result;
350
+ }
351
+
352
+ /**
353
+ * Auto-coerce CLI string values to match the expected schema types.
354
+ * Handles: string → number, string → boolean for primitive schema fields.
355
+ * Arrays of primitives are also coerced element-wise.
356
+ */
357
+ export function coerceArgs(data: Record<string, unknown>, schema: StandardJSONSchemaV1): Record<string, unknown> {
358
+ let properties: Record<string, any>;
359
+ try {
360
+ const jsonSchema = schema['~standard'].jsonSchema.input({ target: 'draft-2020-12' }) as Record<string, any>;
361
+ if (jsonSchema.type !== 'object' || !jsonSchema.properties) return data;
362
+ properties = jsonSchema.properties;
363
+ } catch {
364
+ return data;
365
+ }
366
+
367
+ const result = { ...data };
368
+
369
+ for (const [key, value] of Object.entries(result)) {
370
+ const prop = properties[key];
371
+ if (!prop) continue;
372
+
373
+ const targetType = prop.type as string | undefined;
374
+
375
+ if (targetType === 'number' || targetType === 'integer') {
376
+ if (typeof value === 'string') {
377
+ const num = Number(value);
378
+ if (!Number.isNaN(num)) result[key] = num;
379
+ }
380
+ } else if (targetType === 'boolean') {
381
+ if (typeof value === 'string') {
382
+ if (value === 'true' || value === '1') result[key] = true;
383
+ else if (value === 'false' || value === '0') result[key] = false;
384
+ }
385
+ } else if (targetType === 'array' && Array.isArray(value)) {
386
+ const itemType = prop.items?.type as string | undefined;
387
+ if (itemType === 'number' || itemType === 'integer') {
388
+ result[key] = value.map((v) => {
389
+ if (typeof v === 'string') {
390
+ const num = Number(v);
391
+ return Number.isNaN(num) ? v : num;
392
+ }
393
+ return v;
394
+ });
395
+ } else if (itemType === 'boolean') {
396
+ result[key] = value.map((v) => {
397
+ if (typeof v === 'string') {
398
+ if (v === 'true' || v === '1') return true;
399
+ if (v === 'false' || v === '0') return false;
400
+ }
401
+ return v;
402
+ });
403
+ }
404
+ }
405
+ }
406
+
407
+ return result;
408
+ }
409
+
410
+ /** Keys consumed by the CLI framework that are not user-defined args. */
411
+ const frameworkReservedKeys = new Set(['config', 'c']);
412
+
413
+ /**
414
+ * Detect unknown keys in the args that don't match any schema property.
415
+ * Returns an array of { key, suggestion? } for each unknown key.
416
+ * Framework-reserved keys (--config, -c) are always allowed.
417
+ */
418
+ export function detectUnknownArgs(
419
+ data: Record<string, unknown>,
420
+ schema: StandardJSONSchemaV1,
421
+ flags: Record<string, string>,
422
+ aliases: Record<string, string>,
423
+ suggestFn: (input: string, candidates: string[]) => string,
424
+ ): { key: string; suggestion: string }[] {
425
+ let properties: Record<string, any>;
426
+ let isLoose = false;
427
+ try {
428
+ const jsonSchema = schema['~standard'].jsonSchema.input({ target: 'draft-2020-12' }) as Record<string, any>;
429
+ if (jsonSchema.type !== 'object' || !jsonSchema.properties) return [];
430
+ properties = jsonSchema.properties;
431
+ // If additionalProperties is set (true, {}, or a schema), the schema allows extra keys
432
+ if (jsonSchema.additionalProperties !== undefined && jsonSchema.additionalProperties !== false) isLoose = true;
433
+ } catch {
434
+ return [];
435
+ }
436
+
437
+ if (isLoose) return [];
438
+
439
+ const knownKeys = new Set<string>([
440
+ ...Object.keys(properties),
441
+ ...Object.keys(flags),
442
+ ...Object.values(flags),
443
+ ...Object.keys(aliases),
444
+ ...Object.values(aliases),
445
+ ]);
446
+ const propertyNames = Object.keys(properties);
447
+ const unknowns: { key: string; suggestion: string }[] = [];
448
+
449
+ for (const key of Object.keys(data)) {
450
+ if (!knownKeys.has(key) && !frameworkReservedKeys.has(key)) {
451
+ const suggestion = suggestFn(key, propertyNames);
452
+ unknowns.push({ key, suggestion });
453
+ }
454
+ }
455
+
456
+ return unknowns;
457
+ }
@@ -0,0 +1,29 @@
1
+ import { basename } from 'node:path';
2
+ import { detectShell, getCompletionInstallInstructions, type ShellType, setupCompletions } from '../completion.ts';
3
+ import type { PadroneActionContext } from '../types.ts';
4
+
5
+ interface CompletionsArgs {
6
+ appPath?: string;
7
+ for?: ShellType;
8
+ setup?: boolean;
9
+ }
10
+
11
+ export function runCompletions(args: CompletionsArgs, _ctx: PadroneActionContext) {
12
+ const programName = args.appPath ? basename(args.appPath).replace(/\.[cm]?[jt]sx?$/, '') : 'padrone';
13
+ const shell = args.for ?? detectShell();
14
+
15
+ if (!shell) {
16
+ console.error('Could not detect shell. Use --for to specify one: bash, zsh, fish, powershell');
17
+ process.exit(1);
18
+ }
19
+
20
+ if (args.setup) {
21
+ const result = setupCompletions(programName, shell);
22
+ const verb = result.updated ? 'Updated' : 'Added';
23
+ console.log(`${verb} ${programName} completions in ${result.file}`);
24
+ return;
25
+ }
26
+
27
+ const instructions = getCompletionInstallInstructions(programName, shell);
28
+ console.log(instructions);
29
+ }
@@ -0,0 +1,86 @@
1
+ import { resolve } from 'node:path';
2
+ import { commandSymbol } from '../command-utils.ts';
3
+ import { type DocsFormat, generateDocs } from '../docs/index.ts';
4
+ import type { PadroneActionContext } from '../types.ts';
5
+
6
+ interface DocsArgs {
7
+ entry: string;
8
+ output?: string;
9
+ format?: DocsFormat;
10
+ includeHidden?: boolean;
11
+ dryRun?: boolean;
12
+ }
13
+
14
+ export async function runDocs(args: DocsArgs, _ctx: PadroneActionContext) {
15
+ const entryPath = resolve(args.entry);
16
+
17
+ let mod: Record<string, unknown>;
18
+ try {
19
+ mod = (await import(entryPath)) as Record<string, unknown>;
20
+ } catch (err) {
21
+ console.error(`Failed to import entry file: ${entryPath}`);
22
+ console.error(err instanceof Error ? err.message : String(err));
23
+ process.exit(1);
24
+ }
25
+
26
+ // Look for a padrone program export (default export or named export with .cli method)
27
+ const program = findProgram(mod);
28
+ if (!program) {
29
+ console.error('No Padrone program found in the entry file.');
30
+ console.error('The entry file must export a Padrone program (default or named export).');
31
+ process.exit(1);
32
+ }
33
+
34
+ const result = generateDocs(program, {
35
+ format: args.format,
36
+ output: args.output,
37
+ includeHidden: args.includeHidden,
38
+ dryRun: args.dryRun,
39
+ });
40
+
41
+ if (args.dryRun) {
42
+ console.log('Dry run — files that would be generated:');
43
+ for (const page of result.pages) {
44
+ console.log(` ${page.path}`);
45
+ }
46
+ return;
47
+ }
48
+
49
+ if (result.written.length > 0) {
50
+ console.log(`Generated ${result.written.length} documentation file(s):`);
51
+ for (const file of result.written) {
52
+ console.log(` ${file}`);
53
+ }
54
+ }
55
+
56
+ if (result.skipped.length > 0) {
57
+ console.log(`Skipped ${result.skipped.length} existing file(s).`);
58
+ }
59
+
60
+ if (result.errors.length > 0) {
61
+ console.error(`Failed to write ${result.errors.length} file(s):`);
62
+ for (const { file, error } of result.errors) {
63
+ console.error(` ${file}: ${error.message}`);
64
+ }
65
+ process.exit(1);
66
+ }
67
+ }
68
+
69
+ function findProgram(mod: Record<string, unknown>): any {
70
+ // Check default export first
71
+ const defaultExport = mod.default;
72
+ if (isPadroneProgram(defaultExport)) return defaultExport;
73
+
74
+ // Then check named exports
75
+ for (const value of Object.values(mod)) {
76
+ if (isPadroneProgram(value)) return value;
77
+ }
78
+
79
+ return null;
80
+ }
81
+
82
+ function isPadroneProgram(value: unknown): boolean {
83
+ if (!value || typeof value !== 'object') return false;
84
+ // A PadroneProgram has the internal command symbol (set by createPadrone)
85
+ return commandSymbol in value;
86
+ }