@clerc/core 0.42.2 → 0.44.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/dist/index.d.ts CHANGED
@@ -13,8 +13,7 @@ type FlagSchemaBase<TF> = {
13
13
  * @example
14
14
  *
15
15
  * ```
16
- * type: String
17
- * ```
16
+ * type: String;
18
17
  * ```
19
18
  *
20
19
  * @example Wrap in an array to accept multiple values. `type: [Boolean]`
@@ -30,8 +29,7 @@ type FlagSchemaBase<TF> = {
30
29
  * @example
31
30
  *
32
31
  * ```
33
- * alias: 's'
34
- * ```
32
+ * alias: "s";
35
33
  * ```
36
34
  */
37
35
  alias?: string;
@@ -44,15 +42,13 @@ type FlagSchemaDefault<TF, DefaultType = any> = FlagSchemaBase<TF> & {
44
42
  * @example
45
43
  *
46
44
  * ```
47
- * default: 'hello'
48
- * ```
45
+ * default: 'hello'
49
46
  * ```
50
47
  *
51
48
  * @example
52
49
  *
53
50
  * ```
54
- * default: () => [1, 2, 3]
55
- * ```
51
+ * default: () => [1, 2, 3]
56
52
  * ```
57
53
  */
58
54
  default: DefaultType | (() => DefaultType);
@@ -79,7 +75,7 @@ type TransformParameters<C extends Command> = {
79
75
  [Parameter in NonNullableParameters<C["parameters"]>[number] as CamelCase<StripBrackets<Parameter>>]: ParameterType<Parameter>;
80
76
  };
81
77
  type MakeEventMap<T extends Commands> = {
82
- [K in keyof T]: [InspectorContext];
78
+ [K in keyof T]: [InterceptorContext];
83
79
  };
84
80
  type FallbackFlags<F extends Flags | undefined> = Equals<NonNullableFlag<F>["flags"], {}> extends true ? Dict<any> : NonNullableFlag<F>["flags"];
85
81
  type NonNullableFlag<F extends Flags | undefined> = TypeFlag<NonNullable<F>>;
@@ -153,27 +149,47 @@ type HandlerInCommand<C extends HandlerContext> = (ctx: {
153
149
  [K in keyof C]: C[K];
154
150
  }) => void;
155
151
  type FallbackType<T, U> = {} extends T ? U : T;
156
- type InspectorContext<C extends Commands = Commands> = HandlerContext<C> & {
152
+ /**
153
+ * @deprecated This is a typo. Use `InterceptorContext` instead.
154
+ */
155
+ type InspectorContext<C extends Commands = Commands> = InterceptorContext<C>;
156
+ /**
157
+ * @deprecated This is a typo. Use `Interceptor` instead.
158
+ */
159
+ type Inspector<C extends Commands = Commands> = Interceptor<C>;
160
+ /**
161
+ * @deprecated This is a typo. Use `InspectorFn` instead.
162
+ */
163
+ type InspectorFn<C extends Commands = Commands> = InterceptorFn<C>;
164
+ /**
165
+ * @deprecated This is a typo. Use `InspectorObject` instead.
166
+ */
167
+ type InspectorObject<C extends Commands = Commands> = InterceptorObject<C>;
168
+ type InterceptorContext<C extends Commands = Commands> = HandlerContext<C> & {
157
169
  flags: FallbackType<TypeFlag<NonNullable<C[keyof C]["flags"]>>["flags"], Dict<any>>;
158
170
  };
159
- type Inspector<C extends Commands = Commands> = InspectorFn<C> | InspectorObject<C>;
160
- type InspectorFn<C extends Commands = Commands> = (ctx: InspectorContext<C>, next: () => void) => void;
161
- interface InspectorObject<C extends Commands = Commands> {
171
+ type Interceptor<C extends Commands = Commands> = InterceptorFn<C> | InterceptorObject<C>;
172
+ type InterceptorFn<C extends Commands = Commands> = (ctx: InterceptorContext<C>, next: () => void) => void;
173
+ interface InterceptorObject<C extends Commands = Commands> {
162
174
  enforce?: "pre" | "post";
163
- fn: InspectorFn<C>;
175
+ fn: InterceptorFn<C>;
164
176
  }
165
177
 
166
178
  declare const Root: unique symbol;
167
179
  type RootType = typeof Root;
168
180
  declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}> {
169
181
  #private;
170
- i18n: I18N;
182
+ get i18n(): I18N;
171
183
  private constructor();
172
184
  get _name(): string;
173
185
  get _scriptName(): string;
174
186
  get _description(): string;
175
187
  get _version(): string;
176
- get _inspectors(): Inspector[];
188
+ /**
189
+ * @deprecated This is a typo. Use `_interceptor` instead.
190
+ */
191
+ get _inspectors(): Interceptor[];
192
+ get _interceptors(): Interceptor[];
177
193
  get _commands(): C;
178
194
  get _flags(): GF;
179
195
  /**
@@ -325,6 +341,7 @@ declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}>
325
341
  * @param options
326
342
  * @returns
327
343
  */
344
+ command<C extends CommandWithHandler<any, any>[]>(c: [...C]): this;
328
345
  command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>, GF>;
329
346
  command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags>(name: N, description: string, options?: O & CommandOptions<[...P], A, F>): this & Clerc<C & Record<N, Command<N, O>>, GF>;
330
347
  /**
@@ -377,21 +394,25 @@ declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}>
377
394
  */
378
395
  use<T extends Clerc, U extends Clerc>(plugin: Plugin<T, U>): this & Clerc<C & U["_commands"]> & U;
379
396
  /**
380
- * Register a inspector
397
+ * @deprecated This is a typo. Use `intercetor()` instead.
398
+ */
399
+ inspector(interceptor: Interceptor): this;
400
+ /**
401
+ * Register a interceptor
381
402
  *
382
403
  * @example
383
404
  *
384
405
  * ```ts
385
- * Clerc.create().inspector((ctx, next) => {
406
+ * Clerc.create().interceptor((ctx, next) => {
386
407
  * console.log(ctx);
387
408
  * next();
388
409
  * });
389
410
  * ```
390
411
  *
391
- * @param inspector
412
+ * @param interceptor
392
413
  * @returns
393
414
  */
394
- inspector(inspector: Inspector): this;
415
+ interceptor(interceptor: Interceptor): this;
395
416
  /**
396
417
  * Parse the command line arguments
397
418
  *
@@ -457,17 +478,21 @@ type MaybeArray<T> = T | T[];
457
478
 
458
479
  declare const definePlugin: <T extends Clerc<{}, {}>, U extends Clerc<{}, {}>>(p: Plugin<T, U>) => Plugin<T, U>;
459
480
  declare const defineHandler: <C extends Clerc<{}, {}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K>) => Handler<C["_commands"], K>;
460
- declare const defineInspector: <C extends Clerc<{}, {}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
481
+ declare const defineInterceptor: <C extends Clerc<{}, {}>>(_cli: C, interceptor: Interceptor<C["_commands"]>) => Interceptor<C["_commands"]>;
482
+ /**
483
+ * @deprecated This is a typo. Use `defineInterceptor` instead.
484
+ */
485
+ declare const defineInspector: <C extends Clerc<{}, {}>>(_cli: C, interceptor: Interceptor<C["_commands"]>) => Interceptor<C["_commands"]>;
461
486
  declare const defineCommand: <N extends string | typeof Root, O extends CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>, P extends string[]>(command: Command<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>, handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N, {}>> | undefined) => CommandWithHandler<N, O & CommandOptions<[...P], MaybeArray<string | typeof Root>, Flags>>;
462
487
 
463
488
  declare function resolveFlattenCommands(commands: Commands, t: TranslateFn): Map<string[] | typeof Root, CommandAlias>;
464
489
  declare function resolveCommand(commands: Commands, argv: string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
465
490
  declare const resolveArgv: () => string[];
466
- declare function compose(inspectors: Inspector[]): (ctx: InspectorContext) => void;
491
+ declare function compose(interceptors: Interceptor[]): (ctx: InterceptorContext) => void;
467
492
  declare const isValidName: (name: CommandType) => boolean;
468
493
  declare const withBrackets: (s: string, isOptional?: boolean) => string;
469
494
  declare const formatCommandName: (name: string | string[] | RootType) => string;
470
495
  declare const detectLocale: () => string;
471
496
  declare const stripFlags: (argv: string[]) => string[];
472
497
 
473
- export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, GlobalFlagOption, GlobalFlagOptions, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, ScriptNameNotSetError, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveFlattenCommands, stripFlags, withBrackets };
498
+ export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, GlobalFlagOption, GlobalFlagOptions, Handler, HandlerContext, HandlerInCommand, I18N, Inspector, InspectorContext, InspectorFn, InspectorObject, Interceptor, InterceptorContext, InterceptorFn, InterceptorObject, InvalidCommandNameError, LocaleNotCalledFirstError, Locales, MakeEventMap, NoCommandGivenError, NoSuchCommandError, ParseOptions, Plugin, Root, RootType, ScriptNameNotSetError, TranslateFn, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, defineInterceptor, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveFlattenCommands, stripFlags, withBrackets };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { format } from 'node:util';
2
2
  import { camelCase, arrayStartsWith, toArray } from '@clerc/utils';
3
- import defu from 'defu';
3
+ import { defu } from 'defu';
4
4
  import { LiteEmit } from 'lite-emit';
5
5
  import { typeFlag } from 'type-flag';
6
6
  import { IS_NODE, IS_ELECTRON, IS_DENO } from 'is-platform';
@@ -213,31 +213,31 @@ const resolveArgv = () => IS_NODE ? process.argv.slice(IS_ELECTRON ? 1 : 2) : IS
213
213
  // @ts-expect-error Ignore
214
214
  Deno.args
215
215
  ) : [];
216
- function compose(inspectors) {
217
- const inspectorMap = {
216
+ function compose(interceptors) {
217
+ const interceptorMap = {
218
218
  pre: [],
219
219
  normal: [],
220
220
  post: []
221
221
  };
222
- for (const inspector of inspectors) {
223
- const objectInspector = typeof inspector === "object" ? inspector : { fn: inspector };
224
- const { enforce, fn } = objectInspector;
222
+ for (const interceptor of interceptors) {
223
+ const objectInterceptor = typeof interceptor === "object" ? interceptor : { fn: interceptor };
224
+ const { enforce, fn } = objectInterceptor;
225
225
  if (enforce === "post" || enforce === "pre") {
226
- inspectorMap[enforce].push(fn);
226
+ interceptorMap[enforce].push(fn);
227
227
  } else {
228
- inspectorMap.normal.push(fn);
228
+ interceptorMap.normal.push(fn);
229
229
  }
230
230
  }
231
- const mergedInspectorFns = [
232
- ...inspectorMap.pre,
233
- ...inspectorMap.normal,
234
- ...inspectorMap.post
231
+ const mergedInterceptorFns = [
232
+ ...interceptorMap.pre,
233
+ ...interceptorMap.normal,
234
+ ...interceptorMap.post
235
235
  ];
236
236
  return (ctx) => {
237
237
  return dispatch(0);
238
238
  function dispatch(i) {
239
- const inspector = mergedInspectorFns[i];
240
- return inspector(ctx, dispatch.bind(null, i + 1));
239
+ const interceptor = mergedInterceptorFns[i];
240
+ return interceptor(ctx, dispatch.bind(null, i + 1));
241
241
  }
242
242
  };
243
243
  }
@@ -271,9 +271,9 @@ var __privateMethod = (obj, member, method) => {
271
271
  __accessCheck(obj, member, "access private method");
272
272
  return method;
273
273
  };
274
- var _name, _scriptName, _description, _version, _inspectors, _commands, _commandEmitter, _flags, _usedNames, _argv, _errorHandlers, _isOtherMethodCalled, _defaultLocale, _locale, _locales, _hasRootOrAlias, hasRootOrAlias_get, _hasRoot, hasRoot_get, _addCoreLocales, addCoreLocales_fn, _otherMethodCalled, otherMethodCalled_fn, _command, command_fn, _validateMeta, validateMeta_fn, _getContext, getContext_fn, _handleError, handleError_fn, _callWithErrorHandling, callWithErrorHandling_fn, _runMatchedCommand, runMatchedCommand_fn;
274
+ var _name, _scriptName, _description, _version, _interceptors, _commands, _commandEmitter, _flags, _usedNames, _argv, _errorHandlers, _isOtherMethodCalled, _defaultLocale, _locale, _locales, _hasRootOrAlias, hasRootOrAlias_get, _hasRoot, hasRoot_get, _addCoreLocales, addCoreLocales_fn, _otherMethodCalled, otherMethodCalled_fn, _command, command_fn, _validateMeta, validateMeta_fn, _getContext, getContext_fn, _handleError, handleError_fn, _callWithErrorHandling, callWithErrorHandling_fn, _runMatchedCommand, runMatchedCommand_fn;
275
275
  const Root = Symbol.for("Clerc.Root");
276
- const _Clerc = class {
276
+ const _Clerc = class _Clerc {
277
277
  constructor(scriptName, description, version) {
278
278
  __privateAdd(this, _hasRootOrAlias);
279
279
  __privateAdd(this, _hasRoot);
@@ -289,7 +289,7 @@ const _Clerc = class {
289
289
  __privateAdd(this, _scriptName, "");
290
290
  __privateAdd(this, _description, "");
291
291
  __privateAdd(this, _version, "");
292
- __privateAdd(this, _inspectors, []);
292
+ __privateAdd(this, _interceptors, []);
293
293
  __privateAdd(this, _commands, /* @__PURE__ */ Object.create(null));
294
294
  __privateAdd(this, _commandEmitter, new LiteEmit({
295
295
  errorHandler: (msg) => {
@@ -304,7 +304,14 @@ const _Clerc = class {
304
304
  __privateAdd(this, _defaultLocale, "en");
305
305
  __privateAdd(this, _locale, "en");
306
306
  __privateAdd(this, _locales, /* @__PURE__ */ Object.create(null));
307
- this.i18n = {
307
+ __privateSet(this, _scriptName, scriptName != null ? scriptName : __privateGet(this, _scriptName));
308
+ __privateSet(this, _description, description != null ? description : __privateGet(this, _description));
309
+ __privateSet(this, _version, version != null ? version : __privateGet(this, _version));
310
+ __privateSet(this, _locale, detectLocale());
311
+ __privateMethod(this, _addCoreLocales, addCoreLocales_fn).call(this);
312
+ }
313
+ get i18n() {
314
+ return {
308
315
  add: (locales2) => {
309
316
  __privateSet(this, _locales, defu(__privateGet(this, _locales), locales2));
310
317
  },
@@ -314,11 +321,6 @@ const _Clerc = class {
314
321
  return localeObject[name] ? format(localeObject[name], ...args) : defaultLocaleObject[name] ? format(defaultLocaleObject[name], ...args) : void 0;
315
322
  }
316
323
  };
317
- __privateSet(this, _scriptName, scriptName != null ? scriptName : __privateGet(this, _scriptName));
318
- __privateSet(this, _description, description != null ? description : __privateGet(this, _description));
319
- __privateSet(this, _version, version != null ? version : __privateGet(this, _version));
320
- __privateSet(this, _locale, detectLocale());
321
- __privateMethod(this, _addCoreLocales, addCoreLocales_fn).call(this);
322
324
  }
323
325
  get _name() {
324
326
  return __privateGet(this, _name) || __privateGet(this, _scriptName);
@@ -332,8 +334,14 @@ const _Clerc = class {
332
334
  get _version() {
333
335
  return __privateGet(this, _version);
334
336
  }
337
+ /**
338
+ * @deprecated This is a typo. Use `_interceptor` instead.
339
+ */
335
340
  get _inspectors() {
336
- return __privateGet(this, _inspectors);
341
+ return __privateGet(this, _interceptors);
342
+ }
343
+ get _interceptors() {
344
+ return __privateGet(this, _interceptors);
337
345
  }
338
346
  get _commands() {
339
347
  return __privateGet(this, _commands);
@@ -489,7 +497,15 @@ const _Clerc = class {
489
497
  return this;
490
498
  }
491
499
  command(nameOrCommand, description, options = {}) {
492
- __privateMethod(this, _callWithErrorHandling, callWithErrorHandling_fn).call(this, () => __privateMethod(this, _command, command_fn).call(this, nameOrCommand, description, options));
500
+ __privateMethod(this, _callWithErrorHandling, callWithErrorHandling_fn).call(this, () => {
501
+ if (Array.isArray(nameOrCommand)) {
502
+ for (const command of nameOrCommand) {
503
+ __privateMethod(this, _command, command_fn).call(this, command);
504
+ }
505
+ } else {
506
+ __privateMethod(this, _command, command_fn).call(this, nameOrCommand, description, options);
507
+ }
508
+ });
493
509
  return this;
494
510
  }
495
511
  /**
@@ -554,23 +570,29 @@ const _Clerc = class {
554
570
  return plugin.setup(this);
555
571
  }
556
572
  /**
557
- * Register a inspector
573
+ * @deprecated This is a typo. Use `intercetor()` instead.
574
+ */
575
+ inspector(interceptor) {
576
+ return this.interceptor(interceptor);
577
+ }
578
+ /**
579
+ * Register a interceptor
558
580
  *
559
581
  * @example
560
582
  *
561
583
  * ```ts
562
- * Clerc.create().inspector((ctx, next) => {
584
+ * Clerc.create().interceptor((ctx, next) => {
563
585
  * console.log(ctx);
564
586
  * next();
565
587
  * });
566
588
  * ```
567
589
  *
568
- * @param inspector
590
+ * @param interceptor
569
591
  * @returns
570
592
  */
571
- inspector(inspector) {
593
+ interceptor(interceptor) {
572
594
  __privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
573
- __privateGet(this, _inspectors).push(inspector);
595
+ __privateGet(this, _interceptors).push(interceptor);
574
596
  return this;
575
597
  }
576
598
  /**
@@ -619,12 +641,11 @@ const _Clerc = class {
619
641
  return this;
620
642
  }
621
643
  };
622
- let Clerc = _Clerc;
623
644
  _name = new WeakMap();
624
645
  _scriptName = new WeakMap();
625
646
  _description = new WeakMap();
626
647
  _version = new WeakMap();
627
- _inspectors = new WeakMap();
648
+ _interceptors = new WeakMap();
628
649
  _commands = new WeakMap();
629
650
  _commandEmitter = new WeakMap();
630
651
  _flags = new WeakMap();
@@ -786,24 +807,22 @@ runMatchedCommand_fn = function() {
786
807
  const error = stringName ? new NoSuchCommandError(stringName, t) : new NoCommandGivenError(t);
787
808
  throw error;
788
809
  }
789
- try {
790
- __privateGet(this, _commandEmitter).emit(command.name, ctx);
791
- } catch (e) {
792
- console.log(1);
793
- }
810
+ __privateGet(this, _commandEmitter).emit(command.name, ctx);
794
811
  }
795
812
  };
796
- const inspectors = [...__privateGet(this, _inspectors), emitHandler];
797
- const callInspector = compose(inspectors);
798
- callInspector(getContext());
813
+ const interceptor = [...__privateGet(this, _interceptors), emitHandler];
814
+ const callInterceptor = compose(interceptor);
815
+ callInterceptor(getContext());
799
816
  };
817
+ let Clerc = _Clerc;
800
818
 
801
819
  const definePlugin = (p) => p;
802
820
  const defineHandler = (_cli, _key, handler) => handler;
803
- const defineInspector = (_cli, inspector) => inspector;
821
+ const defineInterceptor = (_cli, interceptor) => interceptor;
822
+ const defineInspector = defineInterceptor;
804
823
  const defineCommand = (command, handler) => ({
805
824
  ...command,
806
825
  handler
807
826
  });
808
827
 
809
- export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, LocaleNotCalledFirstError, NoCommandGivenError, NoSuchCommandError, Root, ScriptNameNotSetError, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveFlattenCommands, stripFlags, withBrackets };
828
+ export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, LocaleNotCalledFirstError, NoCommandGivenError, NoSuchCommandError, Root, ScriptNameNotSetError, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, defineInterceptor, definePlugin, detectLocale, formatCommandName, isValidName, resolveArgv, resolveCommand, resolveFlattenCommands, stripFlags, withBrackets };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/core",
3
- "version": "0.42.2",
3
+ "version": "0.44.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "type": "module",
6
6
  "description": "Clerc core",
@@ -50,9 +50,9 @@
50
50
  "defu": "^6.1.2",
51
51
  "is-platform": "^1.0.0",
52
52
  "lite-emit": "^2.3.0",
53
- "type-fest": "^4.0.0",
53
+ "type-fest": "^4.3.1",
54
54
  "type-flag": "^3.0.0",
55
- "@clerc/utils": "0.42.2"
55
+ "@clerc/utils": "0.44.0"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "pkgroll",