@clerc/core 0.42.1 → 0.43.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,31 @@ 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
+ type InterceptorContext<C extends Commands = Commands> = HandlerContext<C> & {
157
153
  flags: FallbackType<TypeFlag<NonNullable<C[keyof C]["flags"]>>["flags"], Dict<any>>;
158
154
  };
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> {
155
+ type Interceptor<C extends Commands = Commands> = InterceptorFn<C> | InterceptorObject<C>;
156
+ type InterceptorFn<C extends Commands = Commands> = (ctx: InterceptorContext<C>, next: () => void) => void;
157
+ interface InterceptorObject<C extends Commands = Commands> {
162
158
  enforce?: "pre" | "post";
163
- fn: InspectorFn<C>;
159
+ fn: InterceptorFn<C>;
164
160
  }
165
161
 
166
162
  declare const Root: unique symbol;
167
163
  type RootType = typeof Root;
168
164
  declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}> {
169
165
  #private;
170
- i18n: I18N;
166
+ get i18n(): I18N;
171
167
  private constructor();
172
168
  get _name(): string;
173
169
  get _scriptName(): string;
174
170
  get _description(): string;
175
171
  get _version(): string;
176
- get _inspectors(): Inspector[];
172
+ /**
173
+ * @deprecated This is a typo. Use `_interceptor` instead.
174
+ */
175
+ get _inspectors(): Interceptor[];
176
+ get _interceptors(): Interceptor[];
177
177
  get _commands(): C;
178
178
  get _flags(): GF;
179
179
  /**
@@ -377,21 +377,25 @@ declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}>
377
377
  */
378
378
  use<T extends Clerc, U extends Clerc>(plugin: Plugin<T, U>): this & Clerc<C & U["_commands"]> & U;
379
379
  /**
380
- * Register a inspector
380
+ * @deprecated This is a typo. Use `intercetor()` instead.
381
+ */
382
+ inspector(interceptor: Interceptor): this;
383
+ /**
384
+ * Register a interceptor
381
385
  *
382
386
  * @example
383
387
  *
384
388
  * ```ts
385
- * Clerc.create().inspector((ctx, next) => {
389
+ * Clerc.create().interceptor((ctx, next) => {
386
390
  * console.log(ctx);
387
391
  * next();
388
392
  * });
389
393
  * ```
390
394
  *
391
- * @param inspector
395
+ * @param interceptor
392
396
  * @returns
393
397
  */
394
- inspector(inspector: Inspector): this;
398
+ interceptor(interceptor: Interceptor): this;
395
399
  /**
396
400
  * Parse the command line arguments
397
401
  *
@@ -457,17 +461,21 @@ type MaybeArray<T> = T | T[];
457
461
 
458
462
  declare const definePlugin: <T extends Clerc<{}, {}>, U extends Clerc<{}, {}>>(p: Plugin<T, U>) => Plugin<T, U>;
459
463
  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"]>;
464
+ declare const defineInterceptor: <C extends Clerc<{}, {}>>(_cli: C, interceptor: Interceptor<C["_commands"]>) => Interceptor<C["_commands"]>;
465
+ /**
466
+ * @deprecated This is a typo. Use `defineInterceptor` instead.
467
+ */
468
+ declare const defineInspector: <C extends Clerc<{}, {}>>(_cli: C, interceptor: Interceptor<C["_commands"]>) => Interceptor<C["_commands"]>;
461
469
  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
470
 
463
471
  declare function resolveFlattenCommands(commands: Commands, t: TranslateFn): Map<string[] | typeof Root, CommandAlias>;
464
472
  declare function resolveCommand(commands: Commands, argv: string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
465
473
  declare const resolveArgv: () => string[];
466
- declare function compose(inspectors: Inspector[]): (ctx: InspectorContext) => void;
474
+ declare function compose(interceptors: Interceptor[]): (ctx: InterceptorContext) => void;
467
475
  declare const isValidName: (name: CommandType) => boolean;
468
476
  declare const withBrackets: (s: string, isOptional?: boolean) => string;
469
477
  declare const formatCommandName: (name: string | string[] | RootType) => string;
470
478
  declare const detectLocale: () => string;
471
479
  declare const stripFlags: (argv: string[]) => string[];
472
480
 
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 };
481
+ export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandType, CommandWithHandler, Commands, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, GlobalFlagOption, GlobalFlagOptions, Handler, HandlerContext, HandlerInCommand, I18N, 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, _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);
@@ -282,15 +282,20 @@ const _Clerc = class {
282
282
  __privateAdd(this, _command);
283
283
  __privateAdd(this, _validateMeta);
284
284
  __privateAdd(this, _getContext);
285
+ __privateAdd(this, _handleError);
285
286
  __privateAdd(this, _callWithErrorHandling);
286
287
  __privateAdd(this, _runMatchedCommand);
287
288
  __privateAdd(this, _name, "");
288
289
  __privateAdd(this, _scriptName, "");
289
290
  __privateAdd(this, _description, "");
290
291
  __privateAdd(this, _version, "");
291
- __privateAdd(this, _inspectors, []);
292
+ __privateAdd(this, _interceptors, []);
292
293
  __privateAdd(this, _commands, /* @__PURE__ */ Object.create(null));
293
- __privateAdd(this, _commandEmitter, new LiteEmit());
294
+ __privateAdd(this, _commandEmitter, new LiteEmit({
295
+ errorHandler: (msg) => {
296
+ __privateMethod(this, _handleError, handleError_fn).call(this, new Error(msg));
297
+ }
298
+ }));
294
299
  __privateAdd(this, _flags, /* @__PURE__ */ Object.create(null));
295
300
  __privateAdd(this, _usedNames, /* @__PURE__ */ new Set());
296
301
  __privateAdd(this, _argv, void 0);
@@ -299,7 +304,14 @@ const _Clerc = class {
299
304
  __privateAdd(this, _defaultLocale, "en");
300
305
  __privateAdd(this, _locale, "en");
301
306
  __privateAdd(this, _locales, /* @__PURE__ */ Object.create(null));
302
- 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 {
303
315
  add: (locales2) => {
304
316
  __privateSet(this, _locales, defu(__privateGet(this, _locales), locales2));
305
317
  },
@@ -309,11 +321,6 @@ const _Clerc = class {
309
321
  return localeObject[name] ? format(localeObject[name], ...args) : defaultLocaleObject[name] ? format(defaultLocaleObject[name], ...args) : void 0;
310
322
  }
311
323
  };
312
- __privateSet(this, _scriptName, scriptName != null ? scriptName : __privateGet(this, _scriptName));
313
- __privateSet(this, _description, description != null ? description : __privateGet(this, _description));
314
- __privateSet(this, _version, version != null ? version : __privateGet(this, _version));
315
- __privateSet(this, _locale, detectLocale());
316
- __privateMethod(this, _addCoreLocales, addCoreLocales_fn).call(this);
317
324
  }
318
325
  get _name() {
319
326
  return __privateGet(this, _name) || __privateGet(this, _scriptName);
@@ -327,8 +334,14 @@ const _Clerc = class {
327
334
  get _version() {
328
335
  return __privateGet(this, _version);
329
336
  }
337
+ /**
338
+ * @deprecated This is a typo. Use `_interceptor` instead.
339
+ */
330
340
  get _inspectors() {
331
- return __privateGet(this, _inspectors);
341
+ return __privateGet(this, _interceptors);
342
+ }
343
+ get _interceptors() {
344
+ return __privateGet(this, _interceptors);
332
345
  }
333
346
  get _commands() {
334
347
  return __privateGet(this, _commands);
@@ -549,23 +562,29 @@ const _Clerc = class {
549
562
  return plugin.setup(this);
550
563
  }
551
564
  /**
552
- * Register a inspector
565
+ * @deprecated This is a typo. Use `intercetor()` instead.
566
+ */
567
+ inspector(interceptor) {
568
+ return this.interceptor(interceptor);
569
+ }
570
+ /**
571
+ * Register a interceptor
553
572
  *
554
573
  * @example
555
574
  *
556
575
  * ```ts
557
- * Clerc.create().inspector((ctx, next) => {
576
+ * Clerc.create().interceptor((ctx, next) => {
558
577
  * console.log(ctx);
559
578
  * next();
560
579
  * });
561
580
  * ```
562
581
  *
563
- * @param inspector
582
+ * @param interceptor
564
583
  * @returns
565
584
  */
566
- inspector(inspector) {
585
+ interceptor(interceptor) {
567
586
  __privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
568
- __privateGet(this, _inspectors).push(inspector);
587
+ __privateGet(this, _interceptors).push(interceptor);
569
588
  return this;
570
589
  }
571
590
  /**
@@ -614,12 +633,11 @@ const _Clerc = class {
614
633
  return this;
615
634
  }
616
635
  };
617
- let Clerc = _Clerc;
618
636
  _name = new WeakMap();
619
637
  _scriptName = new WeakMap();
620
638
  _description = new WeakMap();
621
639
  _version = new WeakMap();
622
- _inspectors = new WeakMap();
640
+ _interceptors = new WeakMap();
623
641
  _commands = new WeakMap();
624
642
  _commandEmitter = new WeakMap();
625
643
  _flags = new WeakMap();
@@ -744,18 +762,22 @@ getContext_fn = function(getCommand) {
744
762
  };
745
763
  return context;
746
764
  };
765
+ _handleError = new WeakSet();
766
+ handleError_fn = function(e) {
767
+ if (__privateGet(this, _errorHandlers).length > 0) {
768
+ for (const cb of __privateGet(this, _errorHandlers)) {
769
+ cb(e);
770
+ }
771
+ } else {
772
+ throw e;
773
+ }
774
+ };
747
775
  _callWithErrorHandling = new WeakSet();
748
776
  callWithErrorHandling_fn = function(fn) {
749
777
  try {
750
778
  fn();
751
779
  } catch (e) {
752
- if (__privateGet(this, _errorHandlers).length > 0) {
753
- for (const cb of __privateGet(this, _errorHandlers)) {
754
- cb(e);
755
- }
756
- } else {
757
- throw e;
758
- }
780
+ __privateMethod(this, _handleError, handleError_fn).call(this, e);
759
781
  }
760
782
  };
761
783
  _runMatchedCommand = new WeakSet();
@@ -780,17 +802,19 @@ runMatchedCommand_fn = function() {
780
802
  __privateGet(this, _commandEmitter).emit(command.name, ctx);
781
803
  }
782
804
  };
783
- const inspectors = [...__privateGet(this, _inspectors), emitHandler];
784
- const callInspector = compose(inspectors);
785
- callInspector(getContext());
805
+ const interceptor = [...__privateGet(this, _interceptors), emitHandler];
806
+ const callInterceptor = compose(interceptor);
807
+ callInterceptor(getContext());
786
808
  };
809
+ let Clerc = _Clerc;
787
810
 
788
811
  const definePlugin = (p) => p;
789
812
  const defineHandler = (_cli, _key, handler) => handler;
790
- const defineInspector = (_cli, inspector) => inspector;
813
+ const defineInterceptor = (_cli, interceptor) => interceptor;
814
+ const defineInspector = defineInterceptor;
791
815
  const defineCommand = (command, handler) => ({
792
816
  ...command,
793
817
  handler
794
818
  });
795
819
 
796
- 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 };
820
+ 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.1",
3
+ "version": "0.43.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "type": "module",
6
6
  "description": "Clerc core",
@@ -49,10 +49,10 @@
49
49
  "dependencies": {
50
50
  "defu": "^6.1.2",
51
51
  "is-platform": "^1.0.0",
52
- "lite-emit": "^2.2.0",
53
- "type-fest": "^4.0.0",
52
+ "lite-emit": "^2.3.0",
53
+ "type-fest": "^4.3.1",
54
54
  "type-flag": "^3.0.0",
55
- "@clerc/utils": "0.42.1"
55
+ "@clerc/utils": "0.43.0"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "pkgroll",