@clerc/core 0.42.2 → 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 +31 -23
- package/dist/index.js +52 -41
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -13,8 +13,7 @@ type FlagSchemaBase<TF> = {
|
|
|
13
13
|
* @example
|
|
14
14
|
*
|
|
15
15
|
* ```
|
|
16
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
48
|
-
* ```
|
|
45
|
+
* default: 'hello'
|
|
49
46
|
* ```
|
|
50
47
|
*
|
|
51
48
|
* @example
|
|
52
49
|
*
|
|
53
50
|
* ```
|
|
54
|
-
*
|
|
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]: [
|
|
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
|
|
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
|
|
160
|
-
type
|
|
161
|
-
interface
|
|
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:
|
|
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
|
-
|
|
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
|
-
*
|
|
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().
|
|
389
|
+
* Clerc.create().interceptor((ctx, next) => {
|
|
386
390
|
* console.log(ctx);
|
|
387
391
|
* next();
|
|
388
392
|
* });
|
|
389
393
|
* ```
|
|
390
394
|
*
|
|
391
|
-
* @param
|
|
395
|
+
* @param interceptor
|
|
392
396
|
* @returns
|
|
393
397
|
*/
|
|
394
|
-
|
|
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
|
|
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(
|
|
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,
|
|
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(
|
|
217
|
-
const
|
|
216
|
+
function compose(interceptors) {
|
|
217
|
+
const interceptorMap = {
|
|
218
218
|
pre: [],
|
|
219
219
|
normal: [],
|
|
220
220
|
post: []
|
|
221
221
|
};
|
|
222
|
-
for (const
|
|
223
|
-
const
|
|
224
|
-
const { enforce, fn } =
|
|
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
|
-
|
|
226
|
+
interceptorMap[enforce].push(fn);
|
|
227
227
|
} else {
|
|
228
|
-
|
|
228
|
+
interceptorMap.normal.push(fn);
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
|
-
const
|
|
232
|
-
...
|
|
233
|
-
...
|
|
234
|
-
...
|
|
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
|
|
240
|
-
return
|
|
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,
|
|
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,
|
|
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
|
|
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,
|
|
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);
|
|
@@ -554,23 +562,29 @@ const _Clerc = class {
|
|
|
554
562
|
return plugin.setup(this);
|
|
555
563
|
}
|
|
556
564
|
/**
|
|
557
|
-
*
|
|
565
|
+
* @deprecated This is a typo. Use `intercetor()` instead.
|
|
566
|
+
*/
|
|
567
|
+
inspector(interceptor) {
|
|
568
|
+
return this.interceptor(interceptor);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Register a interceptor
|
|
558
572
|
*
|
|
559
573
|
* @example
|
|
560
574
|
*
|
|
561
575
|
* ```ts
|
|
562
|
-
* Clerc.create().
|
|
576
|
+
* Clerc.create().interceptor((ctx, next) => {
|
|
563
577
|
* console.log(ctx);
|
|
564
578
|
* next();
|
|
565
579
|
* });
|
|
566
580
|
* ```
|
|
567
581
|
*
|
|
568
|
-
* @param
|
|
582
|
+
* @param interceptor
|
|
569
583
|
* @returns
|
|
570
584
|
*/
|
|
571
|
-
|
|
585
|
+
interceptor(interceptor) {
|
|
572
586
|
__privateMethod(this, _otherMethodCalled, otherMethodCalled_fn).call(this);
|
|
573
|
-
__privateGet(this,
|
|
587
|
+
__privateGet(this, _interceptors).push(interceptor);
|
|
574
588
|
return this;
|
|
575
589
|
}
|
|
576
590
|
/**
|
|
@@ -619,12 +633,11 @@ const _Clerc = class {
|
|
|
619
633
|
return this;
|
|
620
634
|
}
|
|
621
635
|
};
|
|
622
|
-
let Clerc = _Clerc;
|
|
623
636
|
_name = new WeakMap();
|
|
624
637
|
_scriptName = new WeakMap();
|
|
625
638
|
_description = new WeakMap();
|
|
626
639
|
_version = new WeakMap();
|
|
627
|
-
|
|
640
|
+
_interceptors = new WeakMap();
|
|
628
641
|
_commands = new WeakMap();
|
|
629
642
|
_commandEmitter = new WeakMap();
|
|
630
643
|
_flags = new WeakMap();
|
|
@@ -786,24 +799,22 @@ runMatchedCommand_fn = function() {
|
|
|
786
799
|
const error = stringName ? new NoSuchCommandError(stringName, t) : new NoCommandGivenError(t);
|
|
787
800
|
throw error;
|
|
788
801
|
}
|
|
789
|
-
|
|
790
|
-
__privateGet(this, _commandEmitter).emit(command.name, ctx);
|
|
791
|
-
} catch (e) {
|
|
792
|
-
console.log(1);
|
|
793
|
-
}
|
|
802
|
+
__privateGet(this, _commandEmitter).emit(command.name, ctx);
|
|
794
803
|
}
|
|
795
804
|
};
|
|
796
|
-
const
|
|
797
|
-
const
|
|
798
|
-
|
|
805
|
+
const interceptor = [...__privateGet(this, _interceptors), emitHandler];
|
|
806
|
+
const callInterceptor = compose(interceptor);
|
|
807
|
+
callInterceptor(getContext());
|
|
799
808
|
};
|
|
809
|
+
let Clerc = _Clerc;
|
|
800
810
|
|
|
801
811
|
const definePlugin = (p) => p;
|
|
802
812
|
const defineHandler = (_cli, _key, handler) => handler;
|
|
803
|
-
const
|
|
813
|
+
const defineInterceptor = (_cli, interceptor) => interceptor;
|
|
814
|
+
const defineInspector = defineInterceptor;
|
|
804
815
|
const defineCommand = (command, handler) => ({
|
|
805
816
|
...command,
|
|
806
817
|
handler
|
|
807
818
|
});
|
|
808
819
|
|
|
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 };
|
|
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.
|
|
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",
|
|
@@ -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.
|
|
53
|
+
"type-fest": "^4.3.1",
|
|
54
54
|
"type-flag": "^3.0.0",
|
|
55
|
-
"@clerc/utils": "0.
|
|
55
|
+
"@clerc/utils": "0.43.0"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "pkgroll",
|