@clerc/core 0.22.0 → 0.23.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 +40 -44
- package/dist/index.js +36 -29
- package/dist/index.mjs +36 -29
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _clerc_utils from '@clerc/utils';
|
|
2
|
-
import {
|
|
2
|
+
import { Dict, MaybeArray as MaybeArray$1, CamelCase, LiteralUnion } from '@clerc/utils';
|
|
3
3
|
|
|
4
4
|
declare const DOUBLE_DASH = "--";
|
|
5
5
|
type TypeFunction<ReturnType = any> = (value: any) => ReturnType;
|
|
@@ -56,7 +56,7 @@ type FlagSchemaDefault<TF, DefaultType = any> = FlagSchemaBase<TF> & {
|
|
|
56
56
|
};
|
|
57
57
|
type FlagSchema<TF = FlagType> = (FlagSchemaBase<TF> | FlagSchemaDefault<TF>);
|
|
58
58
|
type FlagTypeOrSchema<ExtraOptions = Record<string, unknown>> = FlagType | (FlagSchema & ExtraOptions);
|
|
59
|
-
type Flags<ExtraOptions = Record<string, unknown>> = Record<string, FlagTypeOrSchema<ExtraOptions>>;
|
|
59
|
+
type Flags$1<ExtraOptions = Record<string, unknown>> = Record<string, FlagTypeOrSchema<ExtraOptions>>;
|
|
60
60
|
type InferFlagType<Flag extends FlagTypeOrSchema> = (Flag extends (TypeFunctionArray<infer T> | FlagSchema<TypeFunctionArray<infer T>>) ? (Flag extends FlagSchemaDefault<TypeFunctionArray<T>, infer D> ? T[] | D : T[]) : (Flag extends TypeFunction<infer T> | FlagSchema<TypeFunction<infer T>> ? (Flag extends FlagSchemaDefault<TypeFunction<T>, infer D> ? T | D : T | undefined) : never));
|
|
61
61
|
interface ParsedFlags<Schemas = Record<string, unknown>> {
|
|
62
62
|
flags: Schemas;
|
|
@@ -65,7 +65,7 @@ interface ParsedFlags<Schemas = Record<string, unknown>> {
|
|
|
65
65
|
[DOUBLE_DASH]: string[];
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
|
-
type TypeFlag<Schemas extends Flags> = ParsedFlags<{
|
|
68
|
+
type TypeFlag<Schemas extends Flags$1> = ParsedFlags<{
|
|
69
69
|
[flag in keyof Schemas]: InferFlagType<Schemas[flag]>;
|
|
70
70
|
}>;
|
|
71
71
|
|
|
@@ -76,9 +76,10 @@ type FlagOptions = FlagSchema & {
|
|
|
76
76
|
type Flag = FlagOptions & {
|
|
77
77
|
name: string;
|
|
78
78
|
};
|
|
79
|
+
type Flags = Dict<FlagOptions>;
|
|
79
80
|
declare interface CommandCustomProperties {
|
|
80
81
|
}
|
|
81
|
-
interface CommandOptions<P extends string[] = string[], A extends MaybeArray<string | RootType> = MaybeArray<string | RootType>, F extends
|
|
82
|
+
interface CommandOptions<P extends string[] = string[], A extends MaybeArray$1<string | RootType> = MaybeArray$1<string | RootType>, F extends Flags = Flags> extends CommandCustomProperties {
|
|
82
83
|
alias?: A;
|
|
83
84
|
parameters?: P;
|
|
84
85
|
flags?: F;
|
|
@@ -93,7 +94,7 @@ type CommandAlias<N extends string | RootType = string, O extends CommandOptions
|
|
|
93
94
|
__isAlias?: true;
|
|
94
95
|
};
|
|
95
96
|
type CommandWithHandler<N extends string | RootType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
|
|
96
|
-
handler?: HandlerInCommand<Record<N, Command<N, O>> & Record<never, never>, N
|
|
97
|
+
handler?: HandlerInCommand<HandlerContext<Record<N, Command<N, O>> & Record<never, never>, N>>;
|
|
97
98
|
};
|
|
98
99
|
type StripBrackets<Parameter extends string> = (Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? (ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName) : never);
|
|
99
100
|
type ParameterType<Parameter extends string> = (Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never);
|
|
@@ -105,34 +106,33 @@ type MakeEventMap<T extends CommandRecord> = {
|
|
|
105
106
|
};
|
|
106
107
|
type PossibleInputKind = string | number | boolean | Dict<any>;
|
|
107
108
|
type NonNullableParameters<T extends string[] | undefined> = T extends undefined ? [] : NonNullable<T>;
|
|
108
|
-
type TransformParameters<C extends
|
|
109
|
-
[Parameter in
|
|
109
|
+
type TransformParameters<C extends Command> = {
|
|
110
|
+
[Parameter in NonNullableParameters<C["parameters"]>[number] as CamelCase<StripBrackets<Parameter>>]: ParameterType<Parameter>;
|
|
110
111
|
};
|
|
111
|
-
type
|
|
112
|
-
|
|
113
|
-
default: any[];
|
|
114
|
-
} : F[K] : F[K];
|
|
115
|
-
};
|
|
116
|
-
type TypeFlagWithDefault<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> = TypeFlag<TransformFlags<NonNullable<C[N]["flags"]>>>;
|
|
117
|
-
type Raw<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> = TypeFlagWithDefault<C, N> & {
|
|
112
|
+
type TypeFlagWithDefault<C extends Command> = TypeFlag<NonNullable<C["flags"]>>;
|
|
113
|
+
type Raw<C extends Command> = TypeFlagWithDefault<C> & {
|
|
118
114
|
parameters: string[];
|
|
119
|
-
mergedFlags: TypeFlagWithDefault<C
|
|
115
|
+
mergedFlags: TypeFlagWithDefault<C>["flags"] & TypeFlagWithDefault<C>["unknownFlags"];
|
|
120
116
|
};
|
|
121
117
|
interface HandlerContext<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> {
|
|
122
|
-
name
|
|
118
|
+
name?: N;
|
|
123
119
|
called?: string | RootType;
|
|
124
|
-
resolved:
|
|
120
|
+
resolved: boolean;
|
|
125
121
|
hasRootOrAlias: boolean;
|
|
126
122
|
hasRoot: boolean;
|
|
127
|
-
raw:
|
|
128
|
-
|
|
123
|
+
raw: {
|
|
124
|
+
[K in keyof Raw<C[N]>]: Raw<C[N]>[K];
|
|
125
|
+
};
|
|
126
|
+
parameters: {
|
|
127
|
+
[K in keyof TransformParameters<C[N]>]: TransformParameters<C[N]>[K];
|
|
128
|
+
};
|
|
129
129
|
unknownFlags: ParsedFlags["unknownFlags"];
|
|
130
|
-
flags: TypeFlagWithDefault<C
|
|
130
|
+
flags: TypeFlagWithDefault<C[N]>["flags"];
|
|
131
131
|
cli: Clerc<C>;
|
|
132
132
|
}
|
|
133
133
|
type Handler<C extends CommandRecord = CommandRecord, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K>) => void;
|
|
134
|
-
type HandlerInCommand<C extends
|
|
135
|
-
|
|
134
|
+
type HandlerInCommand<C extends HandlerContext> = (ctx: {
|
|
135
|
+
[K in keyof C]: C[K];
|
|
136
136
|
}) => void;
|
|
137
137
|
type FallbackType<T, U> = {} extends T ? U : T;
|
|
138
138
|
type InspectorContext<C extends CommandRecord = CommandRecord> = HandlerContext<C> & {
|
|
@@ -230,8 +230,8 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
230
230
|
* })
|
|
231
231
|
* ```
|
|
232
232
|
*/
|
|
233
|
-
command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string | RootType> = MaybeArray<string | RootType>, F extends
|
|
234
|
-
command<N extends string | RootType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string | RootType> = MaybeArray<string | RootType>, F extends
|
|
233
|
+
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>>>;
|
|
234
|
+
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>>>;
|
|
235
235
|
/**
|
|
236
236
|
* Register a handler
|
|
237
237
|
* @param name
|
|
@@ -285,18 +285,20 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
285
285
|
parse(argv?: string[]): this;
|
|
286
286
|
}
|
|
287
287
|
|
|
288
|
+
type MaybeArray<T> = T | T[];
|
|
289
|
+
|
|
288
290
|
declare const definePlugin: <T extends Clerc<{}>, U extends Clerc<{}>>(p: Plugin<T, U>) => Plugin<T, U>;
|
|
289
291
|
declare const defineHandler: <C extends Clerc<{}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K>) => Handler<C["_commands"], K>;
|
|
290
292
|
declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
291
|
-
declare const defineCommand: <N extends string | typeof Root, O extends CommandOptions<[...P],
|
|
293
|
+
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>>;
|
|
292
294
|
|
|
293
295
|
declare class CommandExistsError extends Error {
|
|
294
|
-
|
|
295
|
-
constructor(
|
|
296
|
+
commandName: string;
|
|
297
|
+
constructor(commandName: string);
|
|
296
298
|
}
|
|
297
299
|
declare class NoSuchCommandError extends Error {
|
|
298
|
-
|
|
299
|
-
constructor(
|
|
300
|
+
commandName: string;
|
|
301
|
+
constructor(commandName: string);
|
|
300
302
|
}
|
|
301
303
|
declare class NoCommandGivenError extends Error {
|
|
302
304
|
constructor();
|
|
@@ -316,25 +318,19 @@ declare class VersionNotSetError extends Error {
|
|
|
316
318
|
constructor();
|
|
317
319
|
}
|
|
318
320
|
declare class InvalidCommandNameError extends Error {
|
|
319
|
-
|
|
320
|
-
constructor(
|
|
321
|
+
commandName: string;
|
|
322
|
+
constructor(commandName: string);
|
|
321
323
|
}
|
|
322
324
|
|
|
323
|
-
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof Root, CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>,
|
|
325
|
+
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof Root, CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>, Flags>>>;
|
|
324
326
|
declare function resolveCommand(commands: CommandRecord, name: string | string[] | RootType): Command<string | RootType> | undefined;
|
|
325
|
-
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>,
|
|
326
|
-
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>,
|
|
327
|
+
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>, Flags>>[];
|
|
328
|
+
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>, Flags>>[];
|
|
327
329
|
declare function resolveParametersBeforeFlag(argv: string[]): string[];
|
|
328
330
|
declare const resolveArgv: () => string[];
|
|
329
331
|
declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
|
|
330
|
-
declare const isInvalidName: (name: CommandType) => boolean;
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
name: string;
|
|
334
|
-
required: boolean;
|
|
335
|
-
spread: boolean;
|
|
336
|
-
}
|
|
337
|
-
declare function parseParameters(parameters: string[]): ParsedParameter[];
|
|
338
|
-
declare function mapParametersToArguments(mapping: Record<string, string | string[]>, parameters: ParsedParameter[], cliArguments: string[]): Error | undefined;
|
|
332
|
+
declare const isInvalidName: (name: CommandType) => boolean;
|
|
333
|
+
declare const withBrackets: (s: string, isOptional?: boolean) => string;
|
|
334
|
+
declare const formatCommandName: (name: string | string[] | RootType) => string;
|
|
339
335
|
|
|
340
|
-
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandType, CommandWithHandler, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Handler, HandlerContext, HandlerInCommand, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Plugin, PossibleInputKind, Root, RootType, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin,
|
|
336
|
+
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandType, CommandWithHandler, DescriptionNotSetError, FallbackType, Flag, FlagOptions, Flags, Handler, HandlerContext, HandlerInCommand, Inspector, InspectorContext, InspectorFn, InspectorObject, InvalidCommandNameError, MakeEventMap, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Plugin, PossibleInputKind, Root, RootType, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, formatCommandName, isInvalidName, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
|
package/dist/index.js
CHANGED
|
@@ -4,15 +4,15 @@ import { toArray, arrayStartsWith, camelCase } from '@clerc/utils';
|
|
|
4
4
|
import { isNode, isDeno } from 'is-platform';
|
|
5
5
|
|
|
6
6
|
class CommandExistsError extends Error {
|
|
7
|
-
constructor(
|
|
8
|
-
super(`Command "${
|
|
9
|
-
this.
|
|
7
|
+
constructor(commandName) {
|
|
8
|
+
super(`Command "${commandName}" exists.`);
|
|
9
|
+
this.commandName = commandName;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
class NoSuchCommandError extends Error {
|
|
13
|
-
constructor(
|
|
14
|
-
super(`No such command: ${
|
|
15
|
-
this.
|
|
13
|
+
constructor(commandName) {
|
|
14
|
+
super(`No such command: ${commandName}`);
|
|
15
|
+
this.commandName = commandName;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
class NoCommandGivenError extends Error {
|
|
@@ -43,9 +43,9 @@ class VersionNotSetError extends Error {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
class InvalidCommandNameError extends Error {
|
|
46
|
-
constructor(
|
|
47
|
-
super(`Bad name format: ${
|
|
48
|
-
this.
|
|
46
|
+
constructor(commandName) {
|
|
47
|
+
super(`Bad name format: ${commandName}`);
|
|
48
|
+
this.commandName = commandName;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -113,18 +113,24 @@ function resolveParametersBeforeFlag(argv) {
|
|
|
113
113
|
}
|
|
114
114
|
const resolveArgv = () => isNode() ? process.argv.slice(2) : isDeno() ? Deno.args : [];
|
|
115
115
|
function compose(inspectors) {
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
const inspectorMap = {
|
|
117
|
+
pre: [],
|
|
118
|
+
normal: [],
|
|
119
|
+
post: []
|
|
120
|
+
};
|
|
119
121
|
for (const inspector of inspectors) {
|
|
120
122
|
const objectInspector = typeof inspector === "object" ? inspector : { fn: inspector };
|
|
121
|
-
const { enforce } = objectInspector;
|
|
122
|
-
(enforce === "
|
|
123
|
+
const { enforce, fn } = objectInspector;
|
|
124
|
+
if (enforce === "post" || enforce === "pre") {
|
|
125
|
+
inspectorMap[enforce].push(fn);
|
|
126
|
+
} else {
|
|
127
|
+
inspectorMap.normal.push(fn);
|
|
128
|
+
}
|
|
123
129
|
}
|
|
124
130
|
const mergedInspectorFns = [
|
|
125
|
-
...
|
|
126
|
-
...
|
|
127
|
-
...
|
|
131
|
+
...inspectorMap.pre,
|
|
132
|
+
...inspectorMap.normal,
|
|
133
|
+
...inspectorMap.post
|
|
128
134
|
];
|
|
129
135
|
return (getCtx) => {
|
|
130
136
|
return dispatch(0);
|
|
@@ -135,6 +141,9 @@ function compose(inspectors) {
|
|
|
135
141
|
};
|
|
136
142
|
}
|
|
137
143
|
const isInvalidName = (name) => typeof name === "string" && (name.startsWith(" ") || name.endsWith(" "));
|
|
144
|
+
const withBrackets = (s, isOptional) => isOptional ? `[${s}]` : `<${s}>`;
|
|
145
|
+
const ROOT = "<Root>";
|
|
146
|
+
const formatCommandName = (name) => Array.isArray(name) ? name.join(" ") : typeof name === "string" ? name : ROOT;
|
|
138
147
|
|
|
139
148
|
const { stringify } = JSON;
|
|
140
149
|
function parseParameters(parameters) {
|
|
@@ -223,7 +232,7 @@ const _Clerc = class {
|
|
|
223
232
|
__privateAdd(this, _inspectors, []);
|
|
224
233
|
__privateAdd(this, _commands, {});
|
|
225
234
|
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
226
|
-
__privateAdd(this, _usedNames,
|
|
235
|
+
__privateAdd(this, _usedNames, /* @__PURE__ */ new Set());
|
|
227
236
|
__privateSet(this, _name, name || __privateGet(this, _name));
|
|
228
237
|
__privateSet(this, _description, description || __privateGet(this, _description));
|
|
229
238
|
__privateSet(this, _version, version || __privateGet(this, _version));
|
|
@@ -262,9 +271,6 @@ const _Clerc = class {
|
|
|
262
271
|
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === Root);
|
|
263
272
|
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
264
273
|
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
265
|
-
if (__privateGet(this, _commands)[name]) {
|
|
266
|
-
throw new CommandExistsError(typeof name === "symbol" ? "" : name);
|
|
267
|
-
}
|
|
268
274
|
if (isInvalidName(name)) {
|
|
269
275
|
throw new InvalidCommandNameError(name);
|
|
270
276
|
}
|
|
@@ -272,12 +278,13 @@ const _Clerc = class {
|
|
|
272
278
|
const nameList = [commandToSave.name];
|
|
273
279
|
commandToSave.alias && nameList.push(...toArray(commandToSave.alias));
|
|
274
280
|
for (const name2 of nameList) {
|
|
275
|
-
if (__privateGet(this, _usedNames).
|
|
276
|
-
throw new CommandExistsError(name2);
|
|
281
|
+
if (__privateGet(this, _usedNames).has(name2)) {
|
|
282
|
+
throw new CommandExistsError(formatCommandName(name2));
|
|
277
283
|
}
|
|
278
284
|
}
|
|
279
285
|
__privateGet(this, _commands)[name] = commandToSave;
|
|
280
|
-
__privateGet(this, _usedNames).
|
|
286
|
+
__privateGet(this, _usedNames).add(commandToSave.name);
|
|
287
|
+
(toArray(commandToSave.alias) || []).forEach((a) => __privateGet(this, _usedNames).add(a));
|
|
281
288
|
isCommandObject && handler && this.on(nameOrCommand.name, handler);
|
|
282
289
|
return this;
|
|
283
290
|
}
|
|
@@ -341,7 +348,7 @@ const _Clerc = class {
|
|
|
341
348
|
const mergedFlags = { ...flags, ...unknownFlags };
|
|
342
349
|
const context = {
|
|
343
350
|
name: command == null ? void 0 : command.name,
|
|
344
|
-
called: name.length === 0 ? Root : stringName,
|
|
351
|
+
called: name.length === 0 && (command == null ? void 0 : command.name) ? Root : stringName,
|
|
345
352
|
resolved: isCommandResolved,
|
|
346
353
|
hasRootOrAlias: __privateGet(this, _hasRootOrAlias, hasRootOrAlias_get),
|
|
347
354
|
hasRoot: __privateGet(this, _hasRoot, hasRoot_get),
|
|
@@ -388,16 +395,16 @@ _commandEmitter = new WeakMap();
|
|
|
388
395
|
_usedNames = new WeakMap();
|
|
389
396
|
_hasRootOrAlias = new WeakSet();
|
|
390
397
|
hasRootOrAlias_get = function() {
|
|
391
|
-
return __privateGet(this, _usedNames).
|
|
398
|
+
return __privateGet(this, _usedNames).has(Root);
|
|
392
399
|
};
|
|
393
400
|
_hasRoot = new WeakSet();
|
|
394
401
|
hasRoot_get = function() {
|
|
395
|
-
return
|
|
402
|
+
return Object.prototype.hasOwnProperty.call(this._commands, Root);
|
|
396
403
|
};
|
|
397
404
|
|
|
398
405
|
const definePlugin = (p) => p;
|
|
399
406
|
const defineHandler = (_cli, _key, handler) => handler;
|
|
400
407
|
const defineInspector = (_cli, inspector) => inspector;
|
|
401
|
-
const defineCommand = (command) => command;
|
|
408
|
+
const defineCommand = (command, handler) => ({ ...command, handler });
|
|
402
409
|
|
|
403
|
-
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Root, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin,
|
|
410
|
+
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Root, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, formatCommandName, isInvalidName, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
|
package/dist/index.mjs
CHANGED
|
@@ -4,15 +4,15 @@ import { toArray, arrayStartsWith, camelCase } from '@clerc/utils';
|
|
|
4
4
|
import { isNode, isDeno } from 'is-platform';
|
|
5
5
|
|
|
6
6
|
class CommandExistsError extends Error {
|
|
7
|
-
constructor(
|
|
8
|
-
super(`Command "${
|
|
9
|
-
this.
|
|
7
|
+
constructor(commandName) {
|
|
8
|
+
super(`Command "${commandName}" exists.`);
|
|
9
|
+
this.commandName = commandName;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
class NoSuchCommandError extends Error {
|
|
13
|
-
constructor(
|
|
14
|
-
super(`No such command: ${
|
|
15
|
-
this.
|
|
13
|
+
constructor(commandName) {
|
|
14
|
+
super(`No such command: ${commandName}`);
|
|
15
|
+
this.commandName = commandName;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
class NoCommandGivenError extends Error {
|
|
@@ -43,9 +43,9 @@ class VersionNotSetError extends Error {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
class InvalidCommandNameError extends Error {
|
|
46
|
-
constructor(
|
|
47
|
-
super(`Bad name format: ${
|
|
48
|
-
this.
|
|
46
|
+
constructor(commandName) {
|
|
47
|
+
super(`Bad name format: ${commandName}`);
|
|
48
|
+
this.commandName = commandName;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -113,18 +113,24 @@ function resolveParametersBeforeFlag(argv) {
|
|
|
113
113
|
}
|
|
114
114
|
const resolveArgv = () => isNode() ? process.argv.slice(2) : isDeno() ? Deno.args : [];
|
|
115
115
|
function compose(inspectors) {
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
const inspectorMap = {
|
|
117
|
+
pre: [],
|
|
118
|
+
normal: [],
|
|
119
|
+
post: []
|
|
120
|
+
};
|
|
119
121
|
for (const inspector of inspectors) {
|
|
120
122
|
const objectInspector = typeof inspector === "object" ? inspector : { fn: inspector };
|
|
121
|
-
const { enforce } = objectInspector;
|
|
122
|
-
(enforce === "
|
|
123
|
+
const { enforce, fn } = objectInspector;
|
|
124
|
+
if (enforce === "post" || enforce === "pre") {
|
|
125
|
+
inspectorMap[enforce].push(fn);
|
|
126
|
+
} else {
|
|
127
|
+
inspectorMap.normal.push(fn);
|
|
128
|
+
}
|
|
123
129
|
}
|
|
124
130
|
const mergedInspectorFns = [
|
|
125
|
-
...
|
|
126
|
-
...
|
|
127
|
-
...
|
|
131
|
+
...inspectorMap.pre,
|
|
132
|
+
...inspectorMap.normal,
|
|
133
|
+
...inspectorMap.post
|
|
128
134
|
];
|
|
129
135
|
return (getCtx) => {
|
|
130
136
|
return dispatch(0);
|
|
@@ -135,6 +141,9 @@ function compose(inspectors) {
|
|
|
135
141
|
};
|
|
136
142
|
}
|
|
137
143
|
const isInvalidName = (name) => typeof name === "string" && (name.startsWith(" ") || name.endsWith(" "));
|
|
144
|
+
const withBrackets = (s, isOptional) => isOptional ? `[${s}]` : `<${s}>`;
|
|
145
|
+
const ROOT = "<Root>";
|
|
146
|
+
const formatCommandName = (name) => Array.isArray(name) ? name.join(" ") : typeof name === "string" ? name : ROOT;
|
|
138
147
|
|
|
139
148
|
const { stringify } = JSON;
|
|
140
149
|
function parseParameters(parameters) {
|
|
@@ -223,7 +232,7 @@ const _Clerc = class {
|
|
|
223
232
|
__privateAdd(this, _inspectors, []);
|
|
224
233
|
__privateAdd(this, _commands, {});
|
|
225
234
|
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
226
|
-
__privateAdd(this, _usedNames,
|
|
235
|
+
__privateAdd(this, _usedNames, /* @__PURE__ */ new Set());
|
|
227
236
|
__privateSet(this, _name, name || __privateGet(this, _name));
|
|
228
237
|
__privateSet(this, _description, description || __privateGet(this, _description));
|
|
229
238
|
__privateSet(this, _version, version || __privateGet(this, _version));
|
|
@@ -262,9 +271,6 @@ const _Clerc = class {
|
|
|
262
271
|
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === Root);
|
|
263
272
|
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
264
273
|
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
265
|
-
if (__privateGet(this, _commands)[name]) {
|
|
266
|
-
throw new CommandExistsError(typeof name === "symbol" ? "" : name);
|
|
267
|
-
}
|
|
268
274
|
if (isInvalidName(name)) {
|
|
269
275
|
throw new InvalidCommandNameError(name);
|
|
270
276
|
}
|
|
@@ -272,12 +278,13 @@ const _Clerc = class {
|
|
|
272
278
|
const nameList = [commandToSave.name];
|
|
273
279
|
commandToSave.alias && nameList.push(...toArray(commandToSave.alias));
|
|
274
280
|
for (const name2 of nameList) {
|
|
275
|
-
if (__privateGet(this, _usedNames).
|
|
276
|
-
throw new CommandExistsError(name2);
|
|
281
|
+
if (__privateGet(this, _usedNames).has(name2)) {
|
|
282
|
+
throw new CommandExistsError(formatCommandName(name2));
|
|
277
283
|
}
|
|
278
284
|
}
|
|
279
285
|
__privateGet(this, _commands)[name] = commandToSave;
|
|
280
|
-
__privateGet(this, _usedNames).
|
|
286
|
+
__privateGet(this, _usedNames).add(commandToSave.name);
|
|
287
|
+
(toArray(commandToSave.alias) || []).forEach((a) => __privateGet(this, _usedNames).add(a));
|
|
281
288
|
isCommandObject && handler && this.on(nameOrCommand.name, handler);
|
|
282
289
|
return this;
|
|
283
290
|
}
|
|
@@ -341,7 +348,7 @@ const _Clerc = class {
|
|
|
341
348
|
const mergedFlags = { ...flags, ...unknownFlags };
|
|
342
349
|
const context = {
|
|
343
350
|
name: command == null ? void 0 : command.name,
|
|
344
|
-
called: name.length === 0 ? Root : stringName,
|
|
351
|
+
called: name.length === 0 && (command == null ? void 0 : command.name) ? Root : stringName,
|
|
345
352
|
resolved: isCommandResolved,
|
|
346
353
|
hasRootOrAlias: __privateGet(this, _hasRootOrAlias, hasRootOrAlias_get),
|
|
347
354
|
hasRoot: __privateGet(this, _hasRoot, hasRoot_get),
|
|
@@ -388,16 +395,16 @@ _commandEmitter = new WeakMap();
|
|
|
388
395
|
_usedNames = new WeakMap();
|
|
389
396
|
_hasRootOrAlias = new WeakSet();
|
|
390
397
|
hasRootOrAlias_get = function() {
|
|
391
|
-
return __privateGet(this, _usedNames).
|
|
398
|
+
return __privateGet(this, _usedNames).has(Root);
|
|
392
399
|
};
|
|
393
400
|
_hasRoot = new WeakSet();
|
|
394
401
|
hasRoot_get = function() {
|
|
395
|
-
return
|
|
402
|
+
return Object.prototype.hasOwnProperty.call(this._commands, Root);
|
|
396
403
|
};
|
|
397
404
|
|
|
398
405
|
const definePlugin = (p) => p;
|
|
399
406
|
const defineHandler = (_cli, _key, handler) => handler;
|
|
400
407
|
const defineInspector = (_cli, inspector) => inspector;
|
|
401
|
-
const defineCommand = (command) => command;
|
|
408
|
+
const defineCommand = (command, handler) => ({ ...command, handler });
|
|
402
409
|
|
|
403
|
-
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Root, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin,
|
|
410
|
+
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Root, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, formatCommandName, isInvalidName, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc core",
|
|
6
6
|
"keywords": [
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"is-platform": "^0.2.0",
|
|
51
51
|
"lite-emit": "^1.4.0",
|
|
52
52
|
"type-flag": "^3.0.0",
|
|
53
|
-
"@clerc/utils": "0.
|
|
53
|
+
"@clerc/utils": "0.23.0"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "puild",
|