@clerc/core 0.20.0 → 0.22.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 +26 -19
- package/dist/index.js +50 -31
- package/dist/index.mjs +50 -31
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -69,7 +69,7 @@ type TypeFlag<Schemas extends Flags> = ParsedFlags<{
|
|
|
69
69
|
[flag in keyof Schemas]: InferFlagType<Schemas[flag]>;
|
|
70
70
|
}>;
|
|
71
71
|
|
|
72
|
-
type CommandType =
|
|
72
|
+
type CommandType = RootType | string;
|
|
73
73
|
type FlagOptions = FlagSchema & {
|
|
74
74
|
description?: string;
|
|
75
75
|
};
|
|
@@ -78,27 +78,27 @@ type Flag = FlagOptions & {
|
|
|
78
78
|
};
|
|
79
79
|
declare interface CommandCustomProperties {
|
|
80
80
|
}
|
|
81
|
-
interface CommandOptions<P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, F extends Dict<FlagOptions> = Dict<FlagOptions>> extends CommandCustomProperties {
|
|
81
|
+
interface CommandOptions<P extends string[] = string[], A extends MaybeArray<string | RootType> = MaybeArray<string | RootType>, F extends Dict<FlagOptions> = Dict<FlagOptions>> extends CommandCustomProperties {
|
|
82
82
|
alias?: A;
|
|
83
83
|
parameters?: P;
|
|
84
84
|
flags?: F;
|
|
85
85
|
examples?: [string, string][];
|
|
86
86
|
notes?: string[];
|
|
87
87
|
}
|
|
88
|
-
type Command<N extends string |
|
|
88
|
+
type Command<N extends string | RootType = string, O extends CommandOptions = CommandOptions> = O & {
|
|
89
89
|
name: N;
|
|
90
90
|
description: string;
|
|
91
91
|
};
|
|
92
|
-
type CommandAlias<N extends string |
|
|
92
|
+
type CommandAlias<N extends string | RootType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
|
|
93
93
|
__isAlias?: true;
|
|
94
94
|
};
|
|
95
|
-
type CommandWithHandler<N extends string |
|
|
95
|
+
type CommandWithHandler<N extends string | RootType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
|
|
96
96
|
handler?: HandlerInCommand<Record<N, Command<N, O>> & Record<never, never>, N>;
|
|
97
97
|
};
|
|
98
98
|
type StripBrackets<Parameter extends string> = (Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? (ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName) : never);
|
|
99
99
|
type ParameterType<Parameter extends string> = (Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never);
|
|
100
100
|
type CommandRecord = Dict<Command> & {
|
|
101
|
-
[
|
|
101
|
+
[Root]?: Command;
|
|
102
102
|
};
|
|
103
103
|
type MakeEventMap<T extends CommandRecord> = {
|
|
104
104
|
[K in keyof T]: [InspectorContext];
|
|
@@ -120,8 +120,10 @@ type Raw<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> =
|
|
|
120
120
|
};
|
|
121
121
|
interface HandlerContext<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> {
|
|
122
122
|
name: N extends keyof C ? N : N | undefined;
|
|
123
|
+
called?: string | RootType;
|
|
123
124
|
resolved: N extends keyof C ? true : boolean;
|
|
124
|
-
|
|
125
|
+
hasRootOrAlias: boolean;
|
|
126
|
+
hasRoot: boolean;
|
|
125
127
|
raw: Raw<C, N>;
|
|
126
128
|
parameters: TransformParameters<C, N>;
|
|
127
129
|
unknownFlags: ParsedFlags["unknownFlags"];
|
|
@@ -146,8 +148,8 @@ interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
|
|
|
146
148
|
setup: (cli: T) => U;
|
|
147
149
|
}
|
|
148
150
|
|
|
149
|
-
declare const
|
|
150
|
-
type
|
|
151
|
+
declare const Root: unique symbol;
|
|
152
|
+
type RootType = typeof Root;
|
|
151
153
|
declare class Clerc<C extends CommandRecord = {}> {
|
|
152
154
|
#private;
|
|
153
155
|
private constructor();
|
|
@@ -164,7 +166,7 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
164
166
|
* const cli = Clerc.create()
|
|
165
167
|
* ```
|
|
166
168
|
*/
|
|
167
|
-
static create(): Clerc<{}>;
|
|
169
|
+
static create(name?: string, description?: string, version?: string): Clerc<{}>;
|
|
168
170
|
/**
|
|
169
171
|
* Set the name of the cli
|
|
170
172
|
* @param name
|
|
@@ -218,7 +220,7 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
218
220
|
* @example
|
|
219
221
|
* ```ts
|
|
220
222
|
* Clerc.create()
|
|
221
|
-
* .command("", "
|
|
223
|
+
* .command("", "root", {
|
|
222
224
|
* flags: {
|
|
223
225
|
* foo: {
|
|
224
226
|
* alias: "f",
|
|
@@ -228,8 +230,8 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
228
230
|
* })
|
|
229
231
|
* ```
|
|
230
232
|
*/
|
|
231
|
-
command<N extends string |
|
|
232
|
-
command<N extends string |
|
|
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 Dict<FlagOptions> = Dict<FlagOptions>>(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<string | RootType> = MaybeArray<string | RootType>, F extends Dict<FlagOptions> = Dict<FlagOptions>>(name: N, description: string, options?: O & CommandOptions<[...P], A, F>): this & Clerc<C & Record<N, Command<N, O>>>;
|
|
233
235
|
/**
|
|
234
236
|
* Register a handler
|
|
235
237
|
* @param name
|
|
@@ -286,18 +288,22 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
286
288
|
declare const definePlugin: <T extends Clerc<{}>, U extends Clerc<{}>>(p: Plugin<T, U>) => Plugin<T, U>;
|
|
287
289
|
declare const defineHandler: <C extends Clerc<{}>, K extends keyof C["_commands"]>(_cli: C, _key: K, handler: Handler<C["_commands"], K>) => Handler<C["_commands"], K>;
|
|
288
290
|
declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
289
|
-
declare const defineCommand: <N extends string | typeof
|
|
291
|
+
declare const defineCommand: <N extends string | typeof Root, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, F extends Dict<FlagOptions> = Dict<FlagOptions>>(command: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>) => CommandWithHandler<N, O & CommandOptions<[...P], A, F>>;
|
|
290
292
|
|
|
291
293
|
declare class CommandExistsError extends Error {
|
|
294
|
+
name: string;
|
|
292
295
|
constructor(name: string);
|
|
293
296
|
}
|
|
294
297
|
declare class NoSuchCommandError extends Error {
|
|
298
|
+
name: string;
|
|
295
299
|
constructor(name: string);
|
|
296
300
|
}
|
|
297
301
|
declare class NoCommandGivenError extends Error {
|
|
298
302
|
constructor();
|
|
299
303
|
}
|
|
300
304
|
declare class CommandNameConflictError extends Error {
|
|
305
|
+
n1: string;
|
|
306
|
+
n2: string;
|
|
301
307
|
constructor(n1: string, n2: string);
|
|
302
308
|
}
|
|
303
309
|
declare class NameNotSetError extends Error {
|
|
@@ -310,13 +316,14 @@ declare class VersionNotSetError extends Error {
|
|
|
310
316
|
constructor();
|
|
311
317
|
}
|
|
312
318
|
declare class InvalidCommandNameError extends Error {
|
|
319
|
+
name: string;
|
|
313
320
|
constructor(name: string);
|
|
314
321
|
}
|
|
315
322
|
|
|
316
|
-
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof
|
|
317
|
-
declare function resolveCommand(commands: CommandRecord, name: string | string[] |
|
|
318
|
-
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
319
|
-
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
323
|
+
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof Root, CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>, _clerc_utils.Dict<FlagOptions>>>>;
|
|
324
|
+
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>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
326
|
+
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
320
327
|
declare function resolveParametersBeforeFlag(argv: string[]): string[];
|
|
321
328
|
declare const resolveArgv: () => string[];
|
|
322
329
|
declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
|
|
@@ -330,4 +337,4 @@ interface ParsedParameter {
|
|
|
330
337
|
declare function parseParameters(parameters: string[]): ParsedParameter[];
|
|
331
338
|
declare function mapParametersToArguments(mapping: Record<string, string | string[]>, parameters: ParsedParameter[], cliArguments: string[]): Error | undefined;
|
|
332
339
|
|
|
333
|
-
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,
|
|
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, isInvalidName, mapParametersToArguments, parseParameters, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent };
|
package/dist/index.js
CHANGED
|
@@ -6,11 +6,13 @@ import { isNode, isDeno } from 'is-platform';
|
|
|
6
6
|
class CommandExistsError extends Error {
|
|
7
7
|
constructor(name) {
|
|
8
8
|
super(`Command "${name}" exists.`);
|
|
9
|
+
this.name = name;
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
12
|
class NoSuchCommandError extends Error {
|
|
12
13
|
constructor(name) {
|
|
13
14
|
super(`No such command: ${name}`);
|
|
15
|
+
this.name = name;
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
class NoCommandGivenError extends Error {
|
|
@@ -21,6 +23,8 @@ class NoCommandGivenError extends Error {
|
|
|
21
23
|
class CommandNameConflictError extends Error {
|
|
22
24
|
constructor(n1, n2) {
|
|
23
25
|
super(`Command name ${n1} conflicts with ${n2}. Maybe caused by alias.`);
|
|
26
|
+
this.n1 = n1;
|
|
27
|
+
this.n2 = n2;
|
|
24
28
|
}
|
|
25
29
|
}
|
|
26
30
|
class NameNotSetError extends Error {
|
|
@@ -41,43 +45,48 @@ class VersionNotSetError extends Error {
|
|
|
41
45
|
class InvalidCommandNameError extends Error {
|
|
42
46
|
constructor(name) {
|
|
43
47
|
super(`Bad name format: ${name}`);
|
|
48
|
+
this.name = name;
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
51
|
|
|
52
|
+
function setCommand(commandsMap, commands, command) {
|
|
53
|
+
if (command.alias) {
|
|
54
|
+
const aliases = toArray(command.alias);
|
|
55
|
+
for (const alias of aliases) {
|
|
56
|
+
if (alias in commands) {
|
|
57
|
+
throw new CommandNameConflictError(commands[alias].name, command.name);
|
|
58
|
+
}
|
|
59
|
+
commandsMap.set(typeof alias === "symbol" ? alias : alias.split(" "), { ...command, __isAlias: true });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
47
63
|
function resolveFlattenCommands(commands) {
|
|
48
64
|
const commandsMap = /* @__PURE__ */ new Map();
|
|
49
|
-
if (commands[
|
|
50
|
-
commandsMap.set(
|
|
65
|
+
if (commands[Root]) {
|
|
66
|
+
commandsMap.set(Root, commands[Root]);
|
|
67
|
+
setCommand(commandsMap, commands, commands[Root]);
|
|
51
68
|
}
|
|
52
69
|
for (const command of Object.values(commands)) {
|
|
53
|
-
|
|
54
|
-
const aliases = toArray(command.alias);
|
|
55
|
-
for (const alias of aliases) {
|
|
56
|
-
if (alias in commands) {
|
|
57
|
-
throw new CommandNameConflictError(commands[alias].name, command.name);
|
|
58
|
-
}
|
|
59
|
-
commandsMap.set(alias.split(" "), { ...command, __isAlias: true });
|
|
60
|
-
}
|
|
61
|
-
}
|
|
70
|
+
setCommand(commandsMap, commands, command);
|
|
62
71
|
commandsMap.set(command.name.split(" "), command);
|
|
63
72
|
}
|
|
64
73
|
return commandsMap;
|
|
65
74
|
}
|
|
66
75
|
function resolveCommand(commands, name) {
|
|
67
|
-
if (name ===
|
|
68
|
-
return commands[
|
|
76
|
+
if (name === Root) {
|
|
77
|
+
return commands[Root];
|
|
69
78
|
}
|
|
70
79
|
const nameArr = toArray(name);
|
|
71
80
|
const commandsMap = resolveFlattenCommands(commands);
|
|
72
81
|
let current;
|
|
73
82
|
let currentName;
|
|
74
83
|
commandsMap.forEach((v, k) => {
|
|
75
|
-
if (k ===
|
|
76
|
-
current = commandsMap.get(
|
|
77
|
-
currentName =
|
|
84
|
+
if (k === Root) {
|
|
85
|
+
current = commandsMap.get(Root);
|
|
86
|
+
currentName = Root;
|
|
78
87
|
return;
|
|
79
88
|
}
|
|
80
|
-
if (arrayStartsWith(nameArr, k) && (!currentName || currentName ===
|
|
89
|
+
if (arrayStartsWith(nameArr, k) && (!currentName || currentName === Root || k.length > currentName.length)) {
|
|
81
90
|
current = v;
|
|
82
91
|
currentName = k;
|
|
83
92
|
}
|
|
@@ -202,10 +211,12 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
202
211
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
203
212
|
return value;
|
|
204
213
|
};
|
|
205
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames,
|
|
206
|
-
const
|
|
214
|
+
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasRootOrAlias, hasRootOrAlias_get, _hasRoot, hasRoot_get;
|
|
215
|
+
const Root = Symbol("Root");
|
|
207
216
|
const _Clerc = class {
|
|
208
|
-
constructor() {
|
|
217
|
+
constructor(name, description, version) {
|
|
218
|
+
__privateAdd(this, _hasRootOrAlias);
|
|
219
|
+
__privateAdd(this, _hasRoot);
|
|
209
220
|
__privateAdd(this, _name, "");
|
|
210
221
|
__privateAdd(this, _description, "");
|
|
211
222
|
__privateAdd(this, _version, "");
|
|
@@ -213,7 +224,9 @@ const _Clerc = class {
|
|
|
213
224
|
__privateAdd(this, _commands, {});
|
|
214
225
|
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
215
226
|
__privateAdd(this, _usedNames, []);
|
|
216
|
-
|
|
227
|
+
__privateSet(this, _name, name || __privateGet(this, _name));
|
|
228
|
+
__privateSet(this, _description, description || __privateGet(this, _description));
|
|
229
|
+
__privateSet(this, _version, version || __privateGet(this, _version));
|
|
217
230
|
}
|
|
218
231
|
get _name() {
|
|
219
232
|
return __privateGet(this, _name);
|
|
@@ -230,8 +243,8 @@ const _Clerc = class {
|
|
|
230
243
|
get _commands() {
|
|
231
244
|
return __privateGet(this, _commands);
|
|
232
245
|
}
|
|
233
|
-
static create() {
|
|
234
|
-
return new _Clerc();
|
|
246
|
+
static create(name, description, version) {
|
|
247
|
+
return new _Clerc(name, description, version);
|
|
235
248
|
}
|
|
236
249
|
name(name) {
|
|
237
250
|
__privateSet(this, _name, name);
|
|
@@ -246,7 +259,7 @@ const _Clerc = class {
|
|
|
246
259
|
return this;
|
|
247
260
|
}
|
|
248
261
|
command(nameOrCommand, description, options = {}) {
|
|
249
|
-
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 ===
|
|
262
|
+
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === Root);
|
|
250
263
|
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
251
264
|
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
252
265
|
if (__privateGet(this, _commands)[name]) {
|
|
@@ -263,9 +276,6 @@ const _Clerc = class {
|
|
|
263
276
|
throw new CommandExistsError(name2);
|
|
264
277
|
}
|
|
265
278
|
}
|
|
266
|
-
if (nameList.includes(SingleCommand)) {
|
|
267
|
-
__privateSet(this, _hasSingleCommand, true);
|
|
268
|
-
}
|
|
269
279
|
__privateGet(this, _commands)[name] = commandToSave;
|
|
270
280
|
__privateGet(this, _usedNames).push(commandToSave.name, ...toArray(commandToSave.alias) || []);
|
|
271
281
|
isCommandObject && handler && this.on(nameOrCommand.name, handler);
|
|
@@ -302,7 +312,7 @@ const _Clerc = class {
|
|
|
302
312
|
const isCommandResolved = !!command;
|
|
303
313
|
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
304
314
|
const { _: args, flags, unknownFlags } = parsed;
|
|
305
|
-
let parameters = !isCommandResolved || command.name ===
|
|
315
|
+
let parameters = !isCommandResolved || command.name === Root ? args : args.slice(command.name.split(" ").length);
|
|
306
316
|
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
307
317
|
const hasEof = commandParameters.indexOf("--");
|
|
308
318
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
@@ -331,8 +341,10 @@ const _Clerc = class {
|
|
|
331
341
|
const mergedFlags = { ...flags, ...unknownFlags };
|
|
332
342
|
const context = {
|
|
333
343
|
name: command == null ? void 0 : command.name,
|
|
344
|
+
called: name.length === 0 ? Root : stringName,
|
|
334
345
|
resolved: isCommandResolved,
|
|
335
|
-
|
|
346
|
+
hasRootOrAlias: __privateGet(this, _hasRootOrAlias, hasRootOrAlias_get),
|
|
347
|
+
hasRoot: __privateGet(this, _hasRoot, hasRoot_get),
|
|
336
348
|
raw: { ...parsed, parameters, mergedFlags },
|
|
337
349
|
parameters: mapping,
|
|
338
350
|
flags,
|
|
@@ -374,11 +386,18 @@ _inspectors = new WeakMap();
|
|
|
374
386
|
_commands = new WeakMap();
|
|
375
387
|
_commandEmitter = new WeakMap();
|
|
376
388
|
_usedNames = new WeakMap();
|
|
377
|
-
|
|
389
|
+
_hasRootOrAlias = new WeakSet();
|
|
390
|
+
hasRootOrAlias_get = function() {
|
|
391
|
+
return __privateGet(this, _usedNames).includes(Root);
|
|
392
|
+
};
|
|
393
|
+
_hasRoot = new WeakSet();
|
|
394
|
+
hasRoot_get = function() {
|
|
395
|
+
return !!__privateGet(this, _commands)[Root];
|
|
396
|
+
};
|
|
378
397
|
|
|
379
398
|
const definePlugin = (p) => p;
|
|
380
399
|
const defineHandler = (_cli, _key, handler) => handler;
|
|
381
400
|
const defineInspector = (_cli, inspector) => inspector;
|
|
382
401
|
const defineCommand = (command) => command;
|
|
383
402
|
|
|
384
|
-
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError,
|
|
403
|
+
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Root, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, isInvalidName, mapParametersToArguments, parseParameters, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent };
|
package/dist/index.mjs
CHANGED
|
@@ -6,11 +6,13 @@ import { isNode, isDeno } from 'is-platform';
|
|
|
6
6
|
class CommandExistsError extends Error {
|
|
7
7
|
constructor(name) {
|
|
8
8
|
super(`Command "${name}" exists.`);
|
|
9
|
+
this.name = name;
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
12
|
class NoSuchCommandError extends Error {
|
|
12
13
|
constructor(name) {
|
|
13
14
|
super(`No such command: ${name}`);
|
|
15
|
+
this.name = name;
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
18
|
class NoCommandGivenError extends Error {
|
|
@@ -21,6 +23,8 @@ class NoCommandGivenError extends Error {
|
|
|
21
23
|
class CommandNameConflictError extends Error {
|
|
22
24
|
constructor(n1, n2) {
|
|
23
25
|
super(`Command name ${n1} conflicts with ${n2}. Maybe caused by alias.`);
|
|
26
|
+
this.n1 = n1;
|
|
27
|
+
this.n2 = n2;
|
|
24
28
|
}
|
|
25
29
|
}
|
|
26
30
|
class NameNotSetError extends Error {
|
|
@@ -41,43 +45,48 @@ class VersionNotSetError extends Error {
|
|
|
41
45
|
class InvalidCommandNameError extends Error {
|
|
42
46
|
constructor(name) {
|
|
43
47
|
super(`Bad name format: ${name}`);
|
|
48
|
+
this.name = name;
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
51
|
|
|
52
|
+
function setCommand(commandsMap, commands, command) {
|
|
53
|
+
if (command.alias) {
|
|
54
|
+
const aliases = toArray(command.alias);
|
|
55
|
+
for (const alias of aliases) {
|
|
56
|
+
if (alias in commands) {
|
|
57
|
+
throw new CommandNameConflictError(commands[alias].name, command.name);
|
|
58
|
+
}
|
|
59
|
+
commandsMap.set(typeof alias === "symbol" ? alias : alias.split(" "), { ...command, __isAlias: true });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
47
63
|
function resolveFlattenCommands(commands) {
|
|
48
64
|
const commandsMap = /* @__PURE__ */ new Map();
|
|
49
|
-
if (commands[
|
|
50
|
-
commandsMap.set(
|
|
65
|
+
if (commands[Root]) {
|
|
66
|
+
commandsMap.set(Root, commands[Root]);
|
|
67
|
+
setCommand(commandsMap, commands, commands[Root]);
|
|
51
68
|
}
|
|
52
69
|
for (const command of Object.values(commands)) {
|
|
53
|
-
|
|
54
|
-
const aliases = toArray(command.alias);
|
|
55
|
-
for (const alias of aliases) {
|
|
56
|
-
if (alias in commands) {
|
|
57
|
-
throw new CommandNameConflictError(commands[alias].name, command.name);
|
|
58
|
-
}
|
|
59
|
-
commandsMap.set(alias.split(" "), { ...command, __isAlias: true });
|
|
60
|
-
}
|
|
61
|
-
}
|
|
70
|
+
setCommand(commandsMap, commands, command);
|
|
62
71
|
commandsMap.set(command.name.split(" "), command);
|
|
63
72
|
}
|
|
64
73
|
return commandsMap;
|
|
65
74
|
}
|
|
66
75
|
function resolveCommand(commands, name) {
|
|
67
|
-
if (name ===
|
|
68
|
-
return commands[
|
|
76
|
+
if (name === Root) {
|
|
77
|
+
return commands[Root];
|
|
69
78
|
}
|
|
70
79
|
const nameArr = toArray(name);
|
|
71
80
|
const commandsMap = resolveFlattenCommands(commands);
|
|
72
81
|
let current;
|
|
73
82
|
let currentName;
|
|
74
83
|
commandsMap.forEach((v, k) => {
|
|
75
|
-
if (k ===
|
|
76
|
-
current = commandsMap.get(
|
|
77
|
-
currentName =
|
|
84
|
+
if (k === Root) {
|
|
85
|
+
current = commandsMap.get(Root);
|
|
86
|
+
currentName = Root;
|
|
78
87
|
return;
|
|
79
88
|
}
|
|
80
|
-
if (arrayStartsWith(nameArr, k) && (!currentName || currentName ===
|
|
89
|
+
if (arrayStartsWith(nameArr, k) && (!currentName || currentName === Root || k.length > currentName.length)) {
|
|
81
90
|
current = v;
|
|
82
91
|
currentName = k;
|
|
83
92
|
}
|
|
@@ -202,10 +211,12 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
202
211
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
203
212
|
return value;
|
|
204
213
|
};
|
|
205
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames,
|
|
206
|
-
const
|
|
214
|
+
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasRootOrAlias, hasRootOrAlias_get, _hasRoot, hasRoot_get;
|
|
215
|
+
const Root = Symbol("Root");
|
|
207
216
|
const _Clerc = class {
|
|
208
|
-
constructor() {
|
|
217
|
+
constructor(name, description, version) {
|
|
218
|
+
__privateAdd(this, _hasRootOrAlias);
|
|
219
|
+
__privateAdd(this, _hasRoot);
|
|
209
220
|
__privateAdd(this, _name, "");
|
|
210
221
|
__privateAdd(this, _description, "");
|
|
211
222
|
__privateAdd(this, _version, "");
|
|
@@ -213,7 +224,9 @@ const _Clerc = class {
|
|
|
213
224
|
__privateAdd(this, _commands, {});
|
|
214
225
|
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
215
226
|
__privateAdd(this, _usedNames, []);
|
|
216
|
-
|
|
227
|
+
__privateSet(this, _name, name || __privateGet(this, _name));
|
|
228
|
+
__privateSet(this, _description, description || __privateGet(this, _description));
|
|
229
|
+
__privateSet(this, _version, version || __privateGet(this, _version));
|
|
217
230
|
}
|
|
218
231
|
get _name() {
|
|
219
232
|
return __privateGet(this, _name);
|
|
@@ -230,8 +243,8 @@ const _Clerc = class {
|
|
|
230
243
|
get _commands() {
|
|
231
244
|
return __privateGet(this, _commands);
|
|
232
245
|
}
|
|
233
|
-
static create() {
|
|
234
|
-
return new _Clerc();
|
|
246
|
+
static create(name, description, version) {
|
|
247
|
+
return new _Clerc(name, description, version);
|
|
235
248
|
}
|
|
236
249
|
name(name) {
|
|
237
250
|
__privateSet(this, _name, name);
|
|
@@ -246,7 +259,7 @@ const _Clerc = class {
|
|
|
246
259
|
return this;
|
|
247
260
|
}
|
|
248
261
|
command(nameOrCommand, description, options = {}) {
|
|
249
|
-
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 ===
|
|
262
|
+
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === Root);
|
|
250
263
|
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
251
264
|
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
252
265
|
if (__privateGet(this, _commands)[name]) {
|
|
@@ -263,9 +276,6 @@ const _Clerc = class {
|
|
|
263
276
|
throw new CommandExistsError(name2);
|
|
264
277
|
}
|
|
265
278
|
}
|
|
266
|
-
if (nameList.includes(SingleCommand)) {
|
|
267
|
-
__privateSet(this, _hasSingleCommand, true);
|
|
268
|
-
}
|
|
269
279
|
__privateGet(this, _commands)[name] = commandToSave;
|
|
270
280
|
__privateGet(this, _usedNames).push(commandToSave.name, ...toArray(commandToSave.alias) || []);
|
|
271
281
|
isCommandObject && handler && this.on(nameOrCommand.name, handler);
|
|
@@ -302,7 +312,7 @@ const _Clerc = class {
|
|
|
302
312
|
const isCommandResolved = !!command;
|
|
303
313
|
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
304
314
|
const { _: args, flags, unknownFlags } = parsed;
|
|
305
|
-
let parameters = !isCommandResolved || command.name ===
|
|
315
|
+
let parameters = !isCommandResolved || command.name === Root ? args : args.slice(command.name.split(" ").length);
|
|
306
316
|
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
307
317
|
const hasEof = commandParameters.indexOf("--");
|
|
308
318
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
@@ -331,8 +341,10 @@ const _Clerc = class {
|
|
|
331
341
|
const mergedFlags = { ...flags, ...unknownFlags };
|
|
332
342
|
const context = {
|
|
333
343
|
name: command == null ? void 0 : command.name,
|
|
344
|
+
called: name.length === 0 ? Root : stringName,
|
|
334
345
|
resolved: isCommandResolved,
|
|
335
|
-
|
|
346
|
+
hasRootOrAlias: __privateGet(this, _hasRootOrAlias, hasRootOrAlias_get),
|
|
347
|
+
hasRoot: __privateGet(this, _hasRoot, hasRoot_get),
|
|
336
348
|
raw: { ...parsed, parameters, mergedFlags },
|
|
337
349
|
parameters: mapping,
|
|
338
350
|
flags,
|
|
@@ -374,11 +386,18 @@ _inspectors = new WeakMap();
|
|
|
374
386
|
_commands = new WeakMap();
|
|
375
387
|
_commandEmitter = new WeakMap();
|
|
376
388
|
_usedNames = new WeakMap();
|
|
377
|
-
|
|
389
|
+
_hasRootOrAlias = new WeakSet();
|
|
390
|
+
hasRootOrAlias_get = function() {
|
|
391
|
+
return __privateGet(this, _usedNames).includes(Root);
|
|
392
|
+
};
|
|
393
|
+
_hasRoot = new WeakSet();
|
|
394
|
+
hasRoot_get = function() {
|
|
395
|
+
return !!__privateGet(this, _commands)[Root];
|
|
396
|
+
};
|
|
378
397
|
|
|
379
398
|
const definePlugin = (p) => p;
|
|
380
399
|
const defineHandler = (_cli, _key, handler) => handler;
|
|
381
400
|
const defineInspector = (_cli, inspector) => inspector;
|
|
382
401
|
const defineCommand = (command) => command;
|
|
383
402
|
|
|
384
|
-
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError,
|
|
403
|
+
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Root, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, isInvalidName, mapParametersToArguments, parseParameters, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.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.22.0"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "puild",
|