@clerc/core 0.42.0 → 0.42.1
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 +21 -21
- package/dist/index.js +26 -25
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -92,10 +92,10 @@ type ParseRaw<C extends Command, GF extends GlobalFlagOptions = {}> = NonNullabl
|
|
|
92
92
|
type ParseParameters<C extends Commands = Commands, N extends keyof C = keyof C> = Equals<TransformParameters<C[N]>, {}> extends true ? N extends keyof C ? TransformParameters<C[N]> : Dict<string | string[] | undefined> : TransformParameters<C[N]>;
|
|
93
93
|
|
|
94
94
|
type Locales = Dict<Dict<string>>;
|
|
95
|
-
type
|
|
95
|
+
type TranslateFn = (name: string, ...args: string[]) => string | undefined;
|
|
96
96
|
interface I18N {
|
|
97
97
|
add: (locales: Locales) => void;
|
|
98
|
-
t:
|
|
98
|
+
t: TranslateFn;
|
|
99
99
|
}
|
|
100
100
|
|
|
101
101
|
interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
|
|
@@ -148,19 +148,19 @@ interface HandlerContext<C extends Commands = Commands, N extends keyof C = keyo
|
|
|
148
148
|
flags: Simplify<ParseFlag<C, N, GF> & Record<string, any>>;
|
|
149
149
|
cli: Clerc<C, GF>;
|
|
150
150
|
}
|
|
151
|
-
type Handler<C extends Commands = Commands, K extends keyof C = keyof C, GF extends GlobalFlagOptions = {}> = (
|
|
152
|
-
type HandlerInCommand<C extends HandlerContext> = (
|
|
151
|
+
type Handler<C extends Commands = Commands, K extends keyof C = keyof C, GF extends GlobalFlagOptions = {}> = (ctx: HandlerContext<C, K, GF>) => void;
|
|
152
|
+
type HandlerInCommand<C extends HandlerContext> = (ctx: {
|
|
153
153
|
[K in keyof C]: C[K];
|
|
154
154
|
}) => void;
|
|
155
155
|
type FallbackType<T, U> = {} extends T ? U : T;
|
|
156
156
|
type InspectorContext<C extends Commands = Commands> = HandlerContext<C> & {
|
|
157
157
|
flags: FallbackType<TypeFlag<NonNullable<C[keyof C]["flags"]>>["flags"], Dict<any>>;
|
|
158
158
|
};
|
|
159
|
-
type Inspector<C extends Commands = Commands> =
|
|
160
|
-
type
|
|
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
161
|
interface InspectorObject<C extends Commands = Commands> {
|
|
162
162
|
enforce?: "pre" | "post";
|
|
163
|
-
fn:
|
|
163
|
+
fn: InspectorFn<C>;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
166
|
declare const Root: unique symbol;
|
|
@@ -289,7 +289,7 @@ declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}>
|
|
|
289
289
|
* @param handler
|
|
290
290
|
* @returns
|
|
291
291
|
*/
|
|
292
|
-
errorHandler(handler: (
|
|
292
|
+
errorHandler(handler: (err: any) => void): this;
|
|
293
293
|
/**
|
|
294
294
|
* Register a command
|
|
295
295
|
*
|
|
@@ -422,35 +422,35 @@ declare class Clerc<C extends Commands = {}, GF extends GlobalFlagOptions = {}>
|
|
|
422
422
|
|
|
423
423
|
declare class CommandExistsError extends Error {
|
|
424
424
|
commandName: string;
|
|
425
|
-
constructor(commandName: string, t:
|
|
425
|
+
constructor(commandName: string, t: TranslateFn);
|
|
426
426
|
}
|
|
427
427
|
declare class NoSuchCommandError extends Error {
|
|
428
428
|
commandName: string;
|
|
429
|
-
constructor(commandName: string, t:
|
|
429
|
+
constructor(commandName: string, t: TranslateFn);
|
|
430
430
|
}
|
|
431
431
|
declare class NoCommandGivenError extends Error {
|
|
432
|
-
constructor(t:
|
|
432
|
+
constructor(t: TranslateFn);
|
|
433
433
|
}
|
|
434
434
|
declare class CommandNameConflictError extends Error {
|
|
435
435
|
n1: string;
|
|
436
436
|
n2: string;
|
|
437
|
-
constructor(n1: string, n2: string, t:
|
|
437
|
+
constructor(n1: string, n2: string, t: TranslateFn);
|
|
438
438
|
}
|
|
439
439
|
declare class ScriptNameNotSetError extends Error {
|
|
440
|
-
constructor(t:
|
|
440
|
+
constructor(t: TranslateFn);
|
|
441
441
|
}
|
|
442
442
|
declare class DescriptionNotSetError extends Error {
|
|
443
|
-
constructor(t:
|
|
443
|
+
constructor(t: TranslateFn);
|
|
444
444
|
}
|
|
445
445
|
declare class VersionNotSetError extends Error {
|
|
446
|
-
constructor(t:
|
|
446
|
+
constructor(t: TranslateFn);
|
|
447
447
|
}
|
|
448
448
|
declare class InvalidCommandNameError extends Error {
|
|
449
449
|
commandName: string;
|
|
450
|
-
constructor(commandName: string, t:
|
|
450
|
+
constructor(commandName: string, t: TranslateFn);
|
|
451
451
|
}
|
|
452
452
|
declare class LocaleNotCalledFirstError extends Error {
|
|
453
|
-
constructor(t:
|
|
453
|
+
constructor(t: TranslateFn);
|
|
454
454
|
}
|
|
455
455
|
|
|
456
456
|
type MaybeArray<T> = T | T[];
|
|
@@ -460,14 +460,14 @@ declare const defineHandler: <C extends Clerc<{}, {}>, K extends keyof C["_comma
|
|
|
460
460
|
declare const defineInspector: <C extends Clerc<{}, {}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
461
461
|
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
462
|
|
|
463
|
-
declare function resolveFlattenCommands(commands: Commands, t:
|
|
464
|
-
declare function resolveCommand(commands: Commands, argv: string[], t:
|
|
463
|
+
declare function resolveFlattenCommands(commands: Commands, t: TranslateFn): Map<string[] | typeof Root, CommandAlias>;
|
|
464
|
+
declare function resolveCommand(commands: Commands, argv: string[], t: TranslateFn): [Command<string | RootType> | undefined, string[] | RootType | undefined];
|
|
465
465
|
declare const resolveArgv: () => string[];
|
|
466
|
-
declare function compose(inspectors: Inspector[]): (
|
|
466
|
+
declare function compose(inspectors: Inspector[]): (ctx: InspectorContext) => void;
|
|
467
467
|
declare const isValidName: (name: CommandType) => boolean;
|
|
468
468
|
declare const withBrackets: (s: string, isOptional?: boolean) => string;
|
|
469
469
|
declare const formatCommandName: (name: string | string[] | RootType) => string;
|
|
470
470
|
declare const detectLocale: () => string;
|
|
471
471
|
declare const stripFlags: (argv: string[]) => string[];
|
|
472
472
|
|
|
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,
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -144,15 +144,15 @@ function parseParameters(parameters, t) {
|
|
|
144
144
|
return parsedParameters;
|
|
145
145
|
}
|
|
146
146
|
function mapParametersToArguments(mapping, parameters, cliArguments, t) {
|
|
147
|
-
for (let
|
|
148
|
-
const { name, required, spread } = parameters[
|
|
147
|
+
for (let i = 0; i < parameters.length; i += 1) {
|
|
148
|
+
const { name, required, spread } = parameters[i];
|
|
149
149
|
const camelCaseName = camelCase(name);
|
|
150
150
|
if (camelCaseName in mapping) {
|
|
151
151
|
throw new Error(t("core.parameterIsUsedMoreThanOnce", stringify(name)));
|
|
152
152
|
}
|
|
153
|
-
const value = spread ? cliArguments.slice(
|
|
153
|
+
const value = spread ? cliArguments.slice(i) : cliArguments[i];
|
|
154
154
|
if (spread) {
|
|
155
|
-
|
|
155
|
+
i = parameters.length;
|
|
156
156
|
}
|
|
157
157
|
if (required && (!value || spread && value.length === 0)) {
|
|
158
158
|
throw new Error(t("core.missingRequiredParameter", stringify(name)));
|
|
@@ -196,11 +196,11 @@ function resolveCommand(commands, argv, t) {
|
|
|
196
196
|
const commandsMap = resolveFlattenCommands(commands, t);
|
|
197
197
|
for (const [name, command] of commandsMap.entries()) {
|
|
198
198
|
const parsed = typeFlag((_a = command == null ? void 0 : command.flags) != null ? _a : /* @__PURE__ */ Object.create(null), [...argv]);
|
|
199
|
-
const { _:
|
|
199
|
+
const { _: args } = parsed;
|
|
200
200
|
if (name === Root) {
|
|
201
201
|
continue;
|
|
202
202
|
}
|
|
203
|
-
if (arrayStartsWith(
|
|
203
|
+
if (arrayStartsWith(args, name)) {
|
|
204
204
|
return [command, name];
|
|
205
205
|
}
|
|
206
206
|
}
|
|
@@ -228,16 +228,16 @@ function compose(inspectors) {
|
|
|
228
228
|
inspectorMap.normal.push(fn);
|
|
229
229
|
}
|
|
230
230
|
}
|
|
231
|
-
const
|
|
231
|
+
const mergedInspectorFns = [
|
|
232
232
|
...inspectorMap.pre,
|
|
233
233
|
...inspectorMap.normal,
|
|
234
234
|
...inspectorMap.post
|
|
235
235
|
];
|
|
236
|
-
return (
|
|
236
|
+
return (ctx) => {
|
|
237
237
|
return dispatch(0);
|
|
238
|
-
function dispatch(
|
|
239
|
-
const inspector =
|
|
240
|
-
return inspector(
|
|
238
|
+
function dispatch(i) {
|
|
239
|
+
const inspector = mergedInspectorFns[i];
|
|
240
|
+
return inspector(ctx, dispatch.bind(null, i + 1));
|
|
241
241
|
}
|
|
242
242
|
};
|
|
243
243
|
}
|
|
@@ -247,7 +247,7 @@ const withBrackets = (s, isOptional) => isOptional ? `[${s}]` : `<${s}>`;
|
|
|
247
247
|
const ROOT = "<Root>";
|
|
248
248
|
const formatCommandName = (name) => Array.isArray(name) ? name.join(" ") : typeof name === "string" ? name : ROOT;
|
|
249
249
|
const detectLocale = () => process.env.CLERC_LOCALE ? process.env.CLERC_LOCALE : Intl.DateTimeFormat().resolvedOptions().locale;
|
|
250
|
-
const stripFlags = (argv) => argv.filter((
|
|
250
|
+
const stripFlags = (argv) => argv.filter((arg) => !arg.startsWith("-"));
|
|
251
251
|
|
|
252
252
|
var __accessCheck = (obj, member, msg) => {
|
|
253
253
|
if (!member.has(obj))
|
|
@@ -303,10 +303,10 @@ const _Clerc = class {
|
|
|
303
303
|
add: (locales2) => {
|
|
304
304
|
__privateSet(this, _locales, defu(__privateGet(this, _locales), locales2));
|
|
305
305
|
},
|
|
306
|
-
t: (name, ...
|
|
306
|
+
t: (name, ...args) => {
|
|
307
307
|
const localeObject = __privateGet(this, _locales)[__privateGet(this, _locale)] || __privateGet(this, _locales)[__privateGet(this, _defaultLocale)];
|
|
308
308
|
const defaultLocaleObject = __privateGet(this, _locales)[__privateGet(this, _defaultLocale)];
|
|
309
|
-
return localeObject[name] ? format(localeObject[name], ...
|
|
309
|
+
return localeObject[name] ? format(localeObject[name], ...args) : defaultLocaleObject[name] ? format(defaultLocaleObject[name], ...args) : void 0;
|
|
310
310
|
}
|
|
311
311
|
};
|
|
312
312
|
__privateSet(this, _scriptName, scriptName != null ? scriptName : __privateGet(this, _scriptName));
|
|
@@ -698,15 +698,16 @@ getContext_fn = function(getCommand) {
|
|
|
698
698
|
...command == null ? void 0 : command.flags
|
|
699
699
|
};
|
|
700
700
|
const parsed = typeFlag(flagsMerged, [...argv]);
|
|
701
|
-
const
|
|
702
|
-
|
|
701
|
+
const parametersOffset = (command == null ? void 0 : command.name) === Root || called === Root ? 0 : (called == null ? void 0 : called.length) ? called.length : command == null ? void 0 : command.name.split(" ").length;
|
|
702
|
+
const { _: args, flags, unknownFlags } = parsed;
|
|
703
|
+
let parameters = !isCommandResolved || command.name === Root ? args : args.slice(parametersOffset);
|
|
703
704
|
let commandParameters = (_a = command == null ? void 0 : command.parameters) != null ? _a : [];
|
|
704
705
|
const hasEof = commandParameters.indexOf("--");
|
|
705
706
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
706
707
|
const mapping = /* @__PURE__ */ Object.create(null);
|
|
707
708
|
if (hasEof > -1 && eofParameters.length > 0) {
|
|
708
709
|
commandParameters = commandParameters.slice(0, hasEof);
|
|
709
|
-
const eofArguments =
|
|
710
|
+
const eofArguments = args["--"];
|
|
710
711
|
parameters = parameters.slice(0, -eofArguments.length || void 0);
|
|
711
712
|
mapParametersToArguments(
|
|
712
713
|
mapping,
|
|
@@ -744,16 +745,16 @@ getContext_fn = function(getCommand) {
|
|
|
744
745
|
return context;
|
|
745
746
|
};
|
|
746
747
|
_callWithErrorHandling = new WeakSet();
|
|
747
|
-
callWithErrorHandling_fn = function(
|
|
748
|
+
callWithErrorHandling_fn = function(fn) {
|
|
748
749
|
try {
|
|
749
|
-
|
|
750
|
-
} catch (
|
|
750
|
+
fn();
|
|
751
|
+
} catch (e) {
|
|
751
752
|
if (__privateGet(this, _errorHandlers).length > 0) {
|
|
752
|
-
for (const
|
|
753
|
-
|
|
753
|
+
for (const cb of __privateGet(this, _errorHandlers)) {
|
|
754
|
+
cb(e);
|
|
754
755
|
}
|
|
755
756
|
} else {
|
|
756
|
-
throw
|
|
757
|
+
throw e;
|
|
757
758
|
}
|
|
758
759
|
}
|
|
759
760
|
};
|
|
@@ -769,14 +770,14 @@ runMatchedCommand_fn = function() {
|
|
|
769
770
|
const getContext = () => __privateMethod(this, _getContext, getContext_fn).call(this, getCommand);
|
|
770
771
|
const emitHandler = {
|
|
771
772
|
enforce: "post",
|
|
772
|
-
fn: (
|
|
773
|
+
fn: (ctx) => {
|
|
773
774
|
const [command] = getCommand();
|
|
774
775
|
const stringName = stripFlags(argv).join(" ");
|
|
775
776
|
if (!command) {
|
|
776
777
|
const error = stringName ? new NoSuchCommandError(stringName, t) : new NoCommandGivenError(t);
|
|
777
778
|
throw error;
|
|
778
779
|
}
|
|
779
|
-
__privateGet(this, _commandEmitter).emit(command.name,
|
|
780
|
+
__privateGet(this, _commandEmitter).emit(command.name, ctx);
|
|
780
781
|
}
|
|
781
782
|
};
|
|
782
783
|
const inspectors = [...__privateGet(this, _inspectors), emitHandler];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.42.
|
|
3
|
+
"version": "0.42.1",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc core",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"lite-emit": "^2.2.0",
|
|
53
53
|
"type-fest": "^4.0.0",
|
|
54
54
|
"type-flag": "^3.0.0",
|
|
55
|
-
"@clerc/utils": "0.42.
|
|
55
|
+
"@clerc/utils": "0.42.1"
|
|
56
56
|
},
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "pkgroll",
|