@guanghechen/commander 4.5.1 → 4.7.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.
@@ -93,6 +93,8 @@ interface ICommandArgumentConfig<T = unknown> {
93
93
  coerce?: (rawValue: string) => T;
94
94
  }
95
95
  interface ICommandBuiltinOptionConfig {
96
+ /** Enable built-in --version option (root only, requires configured version) */
97
+ version?: boolean;
96
98
  /** Enable built-in --color/--no-color option for help rendering (defaults respect NO_COLOR) */
97
99
  color?: boolean;
98
100
  /** Enable built-in --log-level option */
@@ -104,15 +106,9 @@ interface ICommandBuiltinOptionConfig {
104
106
  /** Enable built-in --log-colorful/--no-log-colorful option */
105
107
  logColorful?: boolean;
106
108
  }
107
- interface ICommandBuiltinCommandConfig {
108
- /** Enable built-in help subcommand */
109
- help?: boolean;
110
- }
111
109
  interface ICommandBuiltinConfig {
112
110
  /** Built-in options configuration */
113
111
  option?: boolean | ICommandBuiltinOptionConfig;
114
- /** Built-in command configuration */
115
- command?: boolean | ICommandBuiltinCommandConfig;
116
112
  }
117
113
  /** Command example configuration */
118
114
  interface ICommandExample {
@@ -123,6 +119,32 @@ interface ICommandExample {
123
119
  /** Example description */
124
120
  desc: string;
125
121
  }
122
+ /** Command preset defaults */
123
+ interface ICommandPresetConfig {
124
+ /** Preset root directory (absolute path) */
125
+ root?: string;
126
+ /** Default preset options file */
127
+ opt?: string;
128
+ /** Default preset envs file */
129
+ env?: string;
130
+ }
131
+ /** Runtime file stats abstraction */
132
+ interface ICommandRuntimeStats {
133
+ isDirectory(): boolean;
134
+ }
135
+ /** Runtime abstraction for environment-dependent operations */
136
+ interface ICommandRuntime {
137
+ /** Current working directory */
138
+ cwd(): string;
139
+ /** Path absolute check */
140
+ isAbsolute(filepath: string): boolean;
141
+ /** Resolve paths into an absolute path */
142
+ resolve(...paths: string[]): string;
143
+ /** Read UTF-8 text file */
144
+ readFile(filepath: string): Promise<string>;
145
+ /** Stat file system entry */
146
+ stat(filepath: string): Promise<ICommandRuntimeStats>;
147
+ }
126
148
  /** Command configuration */
127
149
  interface ICommandConfig {
128
150
  /** Command name (only for root command) */
@@ -133,14 +155,20 @@ interface ICommandConfig {
133
155
  version?: string;
134
156
  /** Built-in features configuration */
135
157
  builtin?: boolean | ICommandBuiltinConfig;
158
+ /** Command-level preset defaults */
159
+ preset?: ICommandPresetConfig;
136
160
  /** Default reporter for this command */
137
161
  reporter?: IReporter;
162
+ /** Runtime adapter for environment-dependent operations */
163
+ runtime?: ICommandRuntime;
138
164
  }
139
165
  /** Command interface */
140
166
  interface ICommand {
141
167
  readonly name: string | undefined;
142
168
  readonly description: string;
143
169
  readonly version: string | undefined;
170
+ readonly builtin: ICommandConfig['builtin'] | undefined;
171
+ readonly preset: ICommandPresetConfig | undefined;
144
172
  readonly parent: ICommand | undefined;
145
173
  readonly options: ICommandOptionConfig[];
146
174
  readonly arguments: ICommandArgumentConfig[];
@@ -151,12 +179,16 @@ interface ICommand {
151
179
  interface ICommandContext {
152
180
  /** Current command node */
153
181
  cmd: ICommand;
154
- /** Environment variables */
182
+ /** Command chain from root to leaf */
183
+ chain: ICommand[];
184
+ /** Effective environment variables */
155
185
  envs: Record<string, string | undefined>;
186
+ /** Built-in control hit status */
187
+ controls: ICommandControls;
188
+ /** Input source snapshots */
189
+ sources: ICommandInputSources;
156
190
  /** Reporter instance */
157
191
  reporter: IReporter;
158
- /** Original argv */
159
- argv: string[];
160
192
  }
161
193
  /** Action callback parameters */
162
194
  interface ICommandActionParams {
@@ -185,11 +217,29 @@ type ICommandParsedOpts = Record<string, unknown>;
185
217
  /** Parsed arguments record */
186
218
  type ICommandParsedArgs = Record<string, unknown>;
187
219
  /** Route stage result */
188
- interface ICommandRouteResult {
220
+ interface ICommandRouteResult<TCommand = ICommand> {
189
221
  /** Command chain from root to leaf */
190
- chain: ICommand[];
222
+ chain: TCommand[];
191
223
  /** Remaining argv after routing */
192
224
  remaining: string[];
225
+ /** Routed command tokens from user argv (name/alias) */
226
+ cmds: string[];
227
+ }
228
+ /** Control-scan stage result */
229
+ interface ICommandControlScanResult {
230
+ /** Built-in control hit status */
231
+ controls: ICommandControls;
232
+ /** Remaining argv after stripping control tokens */
233
+ remaining: string[];
234
+ /** Optional target token from `help <child>` syntax */
235
+ helpTarget?: string;
236
+ }
237
+ /** Preset stage result */
238
+ interface ICommandPresetResult {
239
+ /** Effective tail argv after preset merge */
240
+ tailArgv: string[];
241
+ /** Effective envs after preset merge */
242
+ envs: Record<string, string | undefined>;
193
243
  }
194
244
  /** Tokenize stage result */
195
245
  interface ICommandTokenizeResult {
@@ -223,8 +273,29 @@ interface ICommandParseResult {
223
273
  /** Raw argument strings */
224
274
  rawArgs: string[];
225
275
  }
276
+ /** Input source snapshots for debugging/tracing */
277
+ interface ICommandInputSources {
278
+ preset: {
279
+ argv: string[];
280
+ envs: Record<string, string>;
281
+ };
282
+ user: {
283
+ /** Routed command tokens (name/alias as entered by user) */
284
+ cmds: string[];
285
+ /** Clean user tail argv after removing command chain/control/preset directives */
286
+ argv: string[];
287
+ /** Raw env snapshot from run/parse params */
288
+ envs: Record<string, string | undefined>;
289
+ };
290
+ }
291
+ /** Built-in run controls */
292
+ interface ICommandControls {
293
+ help: boolean;
294
+ version: boolean;
295
+ }
226
296
  /** Built-in option resolution result (internal) */
227
297
  interface ICommandBuiltinOptionResolved {
298
+ version: boolean;
228
299
  color: boolean;
229
300
  logLevel: boolean;
230
301
  silent: boolean;
@@ -234,9 +305,6 @@ interface ICommandBuiltinOptionResolved {
234
305
  /** Built-in config resolution result (internal) */
235
306
  interface ICommandBuiltinResolved {
236
307
  option: ICommandBuiltinOptionResolved;
237
- command: {
238
- help: boolean;
239
- };
240
308
  }
241
309
  /** Subcommand registry entry (internal) */
242
310
  interface ISubcommandEntry<TCommand = ICommand> {
@@ -244,11 +312,6 @@ interface ISubcommandEntry<TCommand = ICommand> {
244
312
  aliases: string[];
245
313
  command: TCommand;
246
314
  }
247
- /** Internal route result */
248
- interface IInternalRouteResult<TCommand = ICommand> {
249
- chain: TCommand[];
250
- remaining: string[];
251
- }
252
315
  /** Help option line (internal) */
253
316
  interface IHelpOptionLine {
254
317
  sig: string;
@@ -325,13 +388,21 @@ interface ICompletionCommandConfig {
325
388
  /** Program name for completion scripts (defaults to root.name) */
326
389
  programName?: string;
327
390
  /** Default completion file paths for each shell */
328
- paths: ICompletionPaths;
391
+ paths?: Partial<ICompletionPaths>;
329
392
  }
330
393
 
394
+ /**
395
+ * Node runtime adapter
396
+ *
397
+ * @module @guanghechen/commander
398
+ */
399
+
400
+ declare function createNodeCommandRuntime(): ICommandRuntime;
401
+
331
402
  /**
332
403
  * Command class - CLI command builder with fluent API
333
404
  *
334
- * Execution flow: route → tokenize → resolve → parse → run
405
+ * Execution flow: route → control-scan → run-control(run) → preset → tokenize → resolve → parse → run
335
406
  *
336
407
  * @module @guanghechen/commander
337
408
  */
@@ -342,6 +413,8 @@ declare class Command implements ICommand {
342
413
  get name(): string | undefined;
343
414
  get description(): string;
344
415
  get version(): string | undefined;
416
+ get builtin(): ICommandConfig['builtin'] | undefined;
417
+ get preset(): ICommandPresetConfig | undefined;
345
418
  get parent(): Command | undefined;
346
419
  get options(): ICommandOptionConfig[];
347
420
  get arguments(): ICommandArgumentConfig[];
@@ -353,7 +426,7 @@ declare class Command implements ICommand {
353
426
  example(title: string, usage: string, desc: string): this;
354
427
  subcommand(name: string, cmd: Command): this;
355
428
  run(params: ICommandRunParams): Promise<void>;
356
- parse(params: ICommandRunParams): ICommandParseResult;
429
+ parse(params: ICommandRunParams): Promise<ICommandParseResult>;
357
430
  formatHelp(): string;
358
431
  getCompletionMeta(): ICompletionMeta;
359
432
  }
@@ -382,51 +455,6 @@ declare function isIpv6(rawValue: string): boolean;
382
455
  declare function isIp(rawValue: string): boolean;
383
456
  declare function isDomain(rawValue: string): boolean;
384
457
 
385
- /**
386
- * Shell completion generators
387
- *
388
- * @module @guanghechen/commander
389
- */
390
-
391
- /**
392
- * Built-in completion command that generates shell completion scripts.
393
- *
394
- * @example
395
- * ```typescript
396
- * const root = new Command({ name: 'mycli', desc: 'My CLI' })
397
- * root.subcommand('completion', new CompletionCommand(root, {
398
- * paths: {
399
- * bash: `~/.local/share/bash-completion/completions/mycli`,
400
- * fish: `~/.config/fish/completions/mycli.fish`,
401
- * pwsh: `~/.config/powershell/Microsoft.PowerShell_profile.ps1`,
402
- * }
403
- * }))
404
- *
405
- * // Usage:
406
- * // mycli completion --bash > ~/.local/share/bash-completion/completions/mycli
407
- * // mycli completion --fish --write (writes to default path)
408
- * // mycli completion --fish --write /custom/path.fish
409
- * ```
410
- */
411
- declare class CompletionCommand extends Command {
412
- constructor(root: Command, config: ICompletionCommandConfig);
413
- }
414
- declare class BashCompletion {
415
- #private;
416
- constructor(meta: ICompletionMeta, programName: string);
417
- generate(): string;
418
- }
419
- declare class FishCompletion {
420
- #private;
421
- constructor(meta: ICompletionMeta, programName: string);
422
- generate(): string;
423
- }
424
- declare class PwshCompletion {
425
- #private;
426
- constructor(meta: ICompletionMeta, programName: string);
427
- generate(): string;
428
- }
429
-
430
458
  /**
431
459
  * Pre-defined common options for @guanghechen/commander
432
460
  *
@@ -510,5 +538,67 @@ declare const logColorfulOption: ICommandOptionConfig<boolean>;
510
538
  */
511
539
  declare const silentOption: ICommandOptionConfig<boolean>;
512
540
 
513
- export { BashCompletion, Coerce, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, isDomain, isIp, isIpv4, isIpv6, logColorfulOption, logDateOption, logLevelOption, silentOption };
514
- export type { ICommand, ICommandAction, ICommandActionParams, ICommandArgumentConfig, ICommandArgumentKind, ICommandArgumentType, ICommandBuiltinCommandConfig, ICommandBuiltinConfig, ICommandBuiltinOptionConfig, ICommandBuiltinOptionResolved, ICommandBuiltinResolved, ICommandConfig, ICommandContext, ICommandExample, ICommandOptionArgs, ICommandOptionConfig, ICommandOptionType, ICommandParseResult, ICommandParsedArgs, ICommandParsedOpts, ICommandResolveResult, ICommandRouteResult, ICommandRunParams, ICommandShiftResult, ICommandToken, ICommandTokenType, ICommandTokenizeResult, ICommanderErrorKind, ICompletionCommandConfig, ICompletionMeta, ICompletionOptionMeta, ICompletionPaths, ICompletionShellType, IHelpCommandLine, IHelpData, IHelpExampleLine, IHelpOptionLine, IInternalRouteResult, ISubcommandEntry };
541
+ /**
542
+ * Browser runtime adapter
543
+ *
544
+ * @module @guanghechen/commander
545
+ */
546
+
547
+ declare function createBrowserCommandRuntime(): ICommandRuntime;
548
+
549
+ /**
550
+ * Runtime adapters
551
+ *
552
+ * @module @guanghechen/commander
553
+ */
554
+
555
+ declare function getDefaultCommandRuntime(): ICommandRuntime;
556
+ declare function setDefaultCommandRuntime(runtime: ICommandRuntime): void;
557
+
558
+ /**
559
+ * Shell completion generators
560
+ *
561
+ * @module @guanghechen/commander
562
+ */
563
+
564
+ /**
565
+ * Built-in completion command that generates shell completion scripts.
566
+ *
567
+ * @example
568
+ * ```typescript
569
+ * const root = new Command({ name: 'mycli', desc: 'My CLI' })
570
+ * root.subcommand('completion', new CompletionCommand(root, {
571
+ * paths: {
572
+ * bash: `~/.local/share/bash-completion/completions/mycli`,
573
+ * fish: `~/.config/fish/completions/mycli.fish`,
574
+ * pwsh: `~/.config/powershell/Microsoft.PowerShell_profile.ps1`,
575
+ * }
576
+ * }))
577
+ *
578
+ * // Usage:
579
+ * // mycli completion --bash > ~/.local/share/bash-completion/completions/mycli
580
+ * // mycli completion --fish --write (writes to default path)
581
+ * // mycli completion --fish --write /custom/path.fish
582
+ * ```
583
+ */
584
+ declare class CompletionCommand extends Command {
585
+ constructor(root: Command, config?: ICompletionCommandConfig);
586
+ }
587
+ declare class BashCompletion {
588
+ #private;
589
+ constructor(meta: ICompletionMeta, programName: string);
590
+ generate(): string;
591
+ }
592
+ declare class FishCompletion {
593
+ #private;
594
+ constructor(meta: ICompletionMeta, programName: string);
595
+ generate(): string;
596
+ }
597
+ declare class PwshCompletion {
598
+ #private;
599
+ constructor(meta: ICompletionMeta, programName: string);
600
+ generate(): string;
601
+ }
602
+
603
+ export { BashCompletion, Coerce, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, createBrowserCommandRuntime, createNodeCommandRuntime, getDefaultCommandRuntime, isDomain, isIp, isIpv4, isIpv6, logColorfulOption, logDateOption, logLevelOption, setDefaultCommandRuntime, silentOption };
604
+ export type { ICommand, ICommandAction, ICommandActionParams, ICommandArgumentConfig, ICommandArgumentKind, ICommandArgumentType, ICommandBuiltinConfig, ICommandBuiltinOptionConfig, ICommandBuiltinOptionResolved, ICommandBuiltinResolved, ICommandConfig, ICommandContext, ICommandControlScanResult, ICommandControls, ICommandExample, ICommandInputSources, ICommandOptionArgs, ICommandOptionConfig, ICommandOptionType, ICommandParseResult, ICommandParsedArgs, ICommandParsedOpts, ICommandPresetConfig, ICommandPresetResult, ICommandResolveResult, ICommandRouteResult, ICommandRunParams, ICommandRuntime, ICommandRuntimeStats, ICommandShiftResult, ICommandToken, ICommandTokenType, ICommandTokenizeResult, ICommanderErrorKind, ICompletionCommandConfig, ICompletionMeta, ICompletionOptionMeta, ICompletionPaths, ICompletionShellType, IHelpCommandLine, IHelpData, IHelpExampleLine, IHelpOptionLine, ISubcommandEntry };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/commander",
3
- "version": "4.5.1",
3
+ "version": "4.7.0",
4
4
  "description": "A minimal, type-safe command-line interface builder with fluent API",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -20,17 +20,20 @@
20
20
  ],
21
21
  "type": "module",
22
22
  "exports": {
23
- ".": {
24
- "types": "./lib/types/index.d.ts",
25
- "source": "./src/index.ts",
26
- "import": "./lib/esm/index.mjs",
27
- "require": "./lib/cjs/index.cjs"
23
+ ".": null,
24
+ "./browser": {
25
+ "types": "./lib/types/browser.d.ts",
26
+ "source": "./src/runtime/browser/entry.ts",
27
+ "import": "./lib/esm/browser.mjs",
28
+ "require": "./lib/cjs/browser.cjs"
29
+ },
30
+ "./node": {
31
+ "types": "./lib/types/node.d.ts",
32
+ "source": "./src/runtime/node/index.ts",
33
+ "import": "./lib/esm/node.mjs",
34
+ "require": "./lib/cjs/node.cjs"
28
35
  }
29
36
  },
30
- "source": "./src/index.ts",
31
- "main": "./lib/cjs/index.cjs",
32
- "module": "./lib/esm/index.mjs",
33
- "types": "./lib/types/index.d.ts",
34
37
  "license": "MIT",
35
38
  "files": [
36
39
  "lib/",
@@ -41,7 +44,8 @@
41
44
  "README.md"
42
45
  ],
43
46
  "dependencies": {
44
- "@guanghechen/reporter": "^3.3.0"
47
+ "@guanghechen/reporter": "^3.3.0",
48
+ "@guanghechen/env": "^2.0.2"
45
49
  },
46
50
  "scripts": {
47
51
  "build": "rollup -c ../../rollup.config.mjs",