@guanghechen/commander 4.0.0 → 4.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/CHANGELOG.md CHANGED
@@ -1,5 +1,33 @@
1
1
  # Change Log
2
2
 
3
+ ## 4.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - ### @guanghechen/reporter
8
+ - feat: add `setLevel` method for dynamic log level change
9
+ - refactor: split source into modular files (`level.ts`, `chalk.ts`, `types.ts`, `reporter.ts`)
10
+ - refactor: move `IReporter` types from `@guanghechen/types` to `@guanghechen/reporter`
11
+ - export: add level utilities (`LogLevelEnum`, `ILogLevel`, `LOG_LEVELS`, `LOG_LEVEL_VALUES`,
12
+ `isLogLevel`, `getLogLevelValue`, `resolveLogLevel`)
13
+ - export: add chalk utilities (`ANSI`, `formatTag`)
14
+
15
+ ### @guanghechen/types
16
+ - refactor: remove `IReporter` and `IReporterLevel` exports (moved to `@guanghechen/reporter`)
17
+
18
+ ### @guanghechen/commander
19
+ - feat: add predefined options `logLevelOption` and `silentOption` with `apply` callback support
20
+ - refactor: use `ILogLevel` from `@guanghechen/reporter` instead of `IReporterLevel`
21
+
22
+ ### Dependent packages
23
+ - chore: bump version for packages depending on `@guanghechen/types`, `@guanghechen/reporter`, or
24
+ `@guanghechen/commander`
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies:
29
+ - @guanghechen/reporter@3.2.0
30
+
3
31
  ## 4.0.0
4
32
 
5
33
  ### Major Changes
package/lib/cjs/index.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ var reporter = require('@guanghechen/reporter');
3
4
  var fs = require('node:fs');
4
5
  var path = require('node:path');
5
6
 
@@ -37,20 +38,6 @@ class CommanderError extends Error {
37
38
  }
38
39
  }
39
40
 
40
- class DefaultReporter {
41
- debug(message, ...args) {
42
- console.debug(message, ...args);
43
- }
44
- info(message, ...args) {
45
- console.info(message, ...args);
46
- }
47
- warn(message, ...args) {
48
- console.warn(message, ...args);
49
- }
50
- error(message, ...args) {
51
- console.error(message, ...args);
52
- }
53
- }
54
41
  const LONG_OPTION_REGEX = /^--[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
55
42
  const NEGATIVE_OPTION_REGEX = /^--no-[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
56
43
  function kebabToCamelCase(str) {
@@ -228,7 +215,7 @@ class Command {
228
215
  return this;
229
216
  }
230
217
  async run(params) {
231
- const { argv, envs, reporter } = params;
218
+ const { argv, envs, reporter: reporter$1 } = params;
232
219
  try {
233
220
  const processedArgv = this.#processHelpSubcommand(argv);
234
221
  const routeResult = this.#route(processedArgv);
@@ -253,7 +240,7 @@ class Command {
253
240
  const ctx = {
254
241
  cmd: leafCommand,
255
242
  envs,
256
- reporter: reporter ?? this.#reporter ?? new DefaultReporter(),
243
+ reporter: reporter$1 ?? this.#reporter ?? new reporter.Reporter(),
257
244
  argv,
258
245
  };
259
246
  const parseResult = this.#parse(chain, resolveResult, ctx, restArgs);
@@ -283,7 +270,7 @@ class Command {
283
270
  }
284
271
  }
285
272
  parse(params) {
286
- const { argv, envs, reporter } = params;
273
+ const { argv, envs, reporter: reporter$1 } = params;
287
274
  const processedArgv = this.#processHelpSubcommand(argv);
288
275
  const routeResult = this.#route(processedArgv);
289
276
  const { chain, remaining } = routeResult;
@@ -294,7 +281,7 @@ class Command {
294
281
  const ctx = {
295
282
  cmd: leafCommand,
296
283
  envs,
297
- reporter: reporter ?? this.#reporter ?? new DefaultReporter(),
284
+ reporter: reporter$1 ?? this.#reporter ?? new reporter.Reporter(),
298
285
  argv,
299
286
  };
300
287
  return this.#parse(chain, resolveResult, ctx, restArgs);
@@ -1214,9 +1201,37 @@ class PwshCompletion {
1214
1201
  }
1215
1202
  }
1216
1203
 
1204
+ const logLevelOption = {
1205
+ long: 'logLevel',
1206
+ type: 'string',
1207
+ args: 'required',
1208
+ desc: 'Set log level',
1209
+ default: 'info',
1210
+ choices: reporter.LOG_LEVELS,
1211
+ coerce: (raw) => {
1212
+ const level = reporter.resolveLogLevel(raw);
1213
+ if (level === undefined) {
1214
+ throw new Error(`Invalid log level: ${raw}`);
1215
+ }
1216
+ return level;
1217
+ },
1218
+ apply: (value, ctx) => {
1219
+ ctx.reporter.setLevel(value);
1220
+ },
1221
+ };
1222
+ const silentOption = {
1223
+ long: 'silent',
1224
+ type: 'boolean',
1225
+ args: 'none',
1226
+ desc: 'Suppress non-error output',
1227
+ default: false,
1228
+ };
1229
+
1217
1230
  exports.BashCompletion = BashCompletion;
1218
1231
  exports.Command = Command;
1219
1232
  exports.CommanderError = CommanderError;
1220
1233
  exports.CompletionCommand = CompletionCommand;
1221
1234
  exports.FishCompletion = FishCompletion;
1222
1235
  exports.PwshCompletion = PwshCompletion;
1236
+ exports.logLevelOption = logLevelOption;
1237
+ exports.silentOption = silentOption;
package/lib/esm/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { Reporter, LOG_LEVELS, resolveLogLevel } from '@guanghechen/reporter';
1
2
  import * as fs from 'node:fs';
2
3
  import * as path from 'node:path';
3
4
 
@@ -15,20 +16,6 @@ class CommanderError extends Error {
15
16
  }
16
17
  }
17
18
 
18
- class DefaultReporter {
19
- debug(message, ...args) {
20
- console.debug(message, ...args);
21
- }
22
- info(message, ...args) {
23
- console.info(message, ...args);
24
- }
25
- warn(message, ...args) {
26
- console.warn(message, ...args);
27
- }
28
- error(message, ...args) {
29
- console.error(message, ...args);
30
- }
31
- }
32
19
  const LONG_OPTION_REGEX = /^--[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
33
20
  const NEGATIVE_OPTION_REGEX = /^--no-[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
34
21
  function kebabToCamelCase(str) {
@@ -231,7 +218,7 @@ class Command {
231
218
  const ctx = {
232
219
  cmd: leafCommand,
233
220
  envs,
234
- reporter: reporter ?? this.#reporter ?? new DefaultReporter(),
221
+ reporter: reporter ?? this.#reporter ?? new Reporter(),
235
222
  argv,
236
223
  };
237
224
  const parseResult = this.#parse(chain, resolveResult, ctx, restArgs);
@@ -272,7 +259,7 @@ class Command {
272
259
  const ctx = {
273
260
  cmd: leafCommand,
274
261
  envs,
275
- reporter: reporter ?? this.#reporter ?? new DefaultReporter(),
262
+ reporter: reporter ?? this.#reporter ?? new Reporter(),
276
263
  argv,
277
264
  };
278
265
  return this.#parse(chain, resolveResult, ctx, restArgs);
@@ -1192,4 +1179,30 @@ class PwshCompletion {
1192
1179
  }
1193
1180
  }
1194
1181
 
1195
- export { BashCompletion, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion };
1182
+ const logLevelOption = {
1183
+ long: 'logLevel',
1184
+ type: 'string',
1185
+ args: 'required',
1186
+ desc: 'Set log level',
1187
+ default: 'info',
1188
+ choices: LOG_LEVELS,
1189
+ coerce: (raw) => {
1190
+ const level = resolveLogLevel(raw);
1191
+ if (level === undefined) {
1192
+ throw new Error(`Invalid log level: ${raw}`);
1193
+ }
1194
+ return level;
1195
+ },
1196
+ apply: (value, ctx) => {
1197
+ ctx.reporter.setLevel(value);
1198
+ },
1199
+ };
1200
+ const silentOption = {
1201
+ long: 'silent',
1202
+ type: 'boolean',
1203
+ args: 'none',
1204
+ desc: 'Suppress non-error output',
1205
+ default: false,
1206
+ };
1207
+
1208
+ export { BashCompletion, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, logLevelOption, silentOption };
@@ -1,18 +1,11 @@
1
+ import { IReporter } from '@guanghechen/reporter';
2
+
1
3
  /**
2
4
  * Type definitions for @guanghechen/commander
3
5
  *
4
6
  * @module @guanghechen/commander
5
7
  */
6
- /**
7
- * Reporter interface for logging.
8
- * Provided by @guanghechen/reporter or user implementation.
9
- */
10
- interface IReporter {
11
- debug(message: string, ...args: unknown[]): void;
12
- info(message: string, ...args: unknown[]): void;
13
- warn(message: string, ...args: unknown[]): void;
14
- error(message: string, ...args: unknown[]): void;
15
- }
8
+
16
9
  /** Token type: long option, short option, or positional */
17
10
  type ICommandTokenType = 'long' | 'short' | 'none';
18
11
  /**
@@ -326,5 +319,66 @@ declare class PwshCompletion {
326
319
  generate(): string;
327
320
  }
328
321
 
329
- export { BashCompletion, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion };
330
- export type { ICommand, ICommandAction, ICommandActionParams, ICommandArgumentConfig, ICommandArgumentKind, ICommandArgumentType, ICommandConfig, ICommandContext, ICommandOptionArgs, ICommandOptionConfig, ICommandOptionType, ICommandParseResult, ICommandParsedArgs, ICommandParsedOpts, ICommandResolveResult, ICommandRouteResult, ICommandRunParams, ICommandShiftResult, ICommandToken, ICommandTokenType, ICommandTokenizeResult, ICommanderErrorKind, ICompletionCommandConfig, ICompletionMeta, ICompletionOptionMeta, ICompletionPaths, ICompletionShellType, IReporter };
322
+ /**
323
+ * Pre-defined common options for @guanghechen/commander
324
+ *
325
+ * @module @guanghechen/commander/options
326
+ */
327
+
328
+ /**
329
+ * Pre-defined --log-level option for setting log verbosity.
330
+ *
331
+ * Supports: debug | info | hint | warn | error (case-insensitive)
332
+ *
333
+ * | Property | Value |
334
+ * | --------- | ---------------------------------- |
335
+ * | long | 'logLevel' |
336
+ * | type | 'string' |
337
+ * | args | 'required' |
338
+ * | default | 'info' |
339
+ * | choices | LOG_LEVELS |
340
+ * | coerce | resolveLogLevel (case-insensitive) |
341
+ * | apply | ctx.reporter.setLevel(value) |
342
+ *
343
+ * @example
344
+ * ```typescript
345
+ * import { logLevelOption } from '@guanghechen/commander'
346
+ *
347
+ * const cmd = new Command('app')
348
+ * .option(logLevelOption)
349
+ * .action(({ opts }) => {
350
+ * console.log(opts.logLevel) // 'debug' | 'info' | 'hint' | 'warn' | 'error'
351
+ * })
352
+ *
353
+ * // Override with spread syntax
354
+ * .option({ ...logLevelOption, default: 'warn' })
355
+ * ```
356
+ */
357
+ declare const logLevelOption: ICommandOptionConfig<string>;
358
+ /**
359
+ * Pre-defined --silent option for suppressing non-error output.
360
+ *
361
+ * | Property | Value |
362
+ * | --------- | --------- |
363
+ * | long | 'silent' |
364
+ * | type | 'boolean' |
365
+ * | args | 'none' |
366
+ * | default | false |
367
+ *
368
+ * @example
369
+ * ```typescript
370
+ * import { silentOption } from '@guanghechen/commander'
371
+ *
372
+ * const cmd = new Command('app')
373
+ * .option(silentOption)
374
+ * .action(({ opts }) => {
375
+ * if (!opts.silent) {
376
+ * console.log('Processing...')
377
+ * }
378
+ * })
379
+ * ```
380
+ */
381
+ declare const silentOption: ICommandOptionConfig<boolean>;
382
+
383
+ export { BashCompletion, Command, CommanderError, CompletionCommand, FishCompletion, PwshCompletion, logLevelOption, silentOption };
384
+ export type { ICommand, ICommandAction, ICommandActionParams, ICommandArgumentConfig, ICommandArgumentKind, ICommandArgumentType, ICommandConfig, ICommandContext, ICommandOptionArgs, ICommandOptionConfig, ICommandOptionType, ICommandParseResult, ICommandParsedArgs, ICommandParsedOpts, ICommandResolveResult, ICommandRouteResult, ICommandRunParams, ICommandShiftResult, ICommandToken, ICommandTokenType, ICommandTokenizeResult, ICommanderErrorKind, ICompletionCommandConfig, ICompletionMeta, ICompletionOptionMeta, ICompletionPaths, ICompletionShellType };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/commander",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "A minimal, type-safe command-line interface builder with fluent API",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -40,6 +40,9 @@
40
40
  "LICENSE",
41
41
  "README.md"
42
42
  ],
43
+ "dependencies": {
44
+ "@guanghechen/reporter": "^3.2.0"
45
+ },
43
46
  "scripts": {
44
47
  "build": "rollup -c ../../rollup.config.mjs",
45
48
  "clean": "rimraf lib",