@clerc/core 0.19.0 → 0.21.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 +18 -20
- package/dist/index.js +52 -59
- package/dist/index.mjs +52 -59
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -78,7 +78,7 @@ 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 | SingleCommandType> = MaybeArray<string | SingleCommandType>, F extends Dict<FlagOptions> = Dict<FlagOptions>> extends CommandCustomProperties {
|
|
82
82
|
alias?: A;
|
|
83
83
|
parameters?: P;
|
|
84
84
|
flags?: F;
|
|
@@ -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 | SingleCommandType;
|
|
123
124
|
resolved: N extends keyof C ? true : boolean;
|
|
124
|
-
|
|
125
|
+
hasSingleCommandOrAlias: boolean;
|
|
126
|
+
hasSingleCommand: boolean;
|
|
125
127
|
raw: Raw<C, N>;
|
|
126
128
|
parameters: TransformParameters<C, N>;
|
|
127
129
|
unknownFlags: ParsedFlags["unknownFlags"];
|
|
@@ -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
|
|
@@ -228,8 +230,8 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
228
230
|
* })
|
|
229
231
|
* ```
|
|
230
232
|
*/
|
|
231
|
-
command<N extends string | SingleCommandType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, F extends Dict<FlagOptions> = Dict<FlagOptions>>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>>;
|
|
232
|
-
command<N extends string | SingleCommandType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, 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
|
+
command<N extends string | SingleCommandType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string | SingleCommandType> = MaybeArray<string | SingleCommandType>, 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 | SingleCommandType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string | SingleCommandType> = MaybeArray<string | SingleCommandType>, 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
|
|
@@ -288,25 +290,20 @@ declare const defineHandler: <C extends Clerc<{}>, K extends keyof C["_commands"
|
|
|
288
290
|
declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
289
291
|
declare const defineCommand: <N extends string | typeof SingleCommand, 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
|
-
declare class SingleCommandError extends Error {
|
|
292
|
-
constructor();
|
|
293
|
-
}
|
|
294
|
-
declare class SingleCommandAliasError extends Error {
|
|
295
|
-
constructor();
|
|
296
|
-
}
|
|
297
293
|
declare class CommandExistsError extends Error {
|
|
294
|
+
name: string;
|
|
298
295
|
constructor(name: string);
|
|
299
296
|
}
|
|
300
|
-
declare class CommonCommandExistsError extends Error {
|
|
301
|
-
constructor();
|
|
302
|
-
}
|
|
303
297
|
declare class NoSuchCommandError extends Error {
|
|
298
|
+
name: string;
|
|
304
299
|
constructor(name: string);
|
|
305
300
|
}
|
|
306
301
|
declare class NoCommandGivenError extends Error {
|
|
307
302
|
constructor();
|
|
308
303
|
}
|
|
309
304
|
declare class CommandNameConflictError extends Error {
|
|
305
|
+
n1: string;
|
|
306
|
+
n2: string;
|
|
310
307
|
constructor(n1: string, n2: string);
|
|
311
308
|
}
|
|
312
309
|
declare class NameNotSetError extends Error {
|
|
@@ -319,14 +316,15 @@ declare class VersionNotSetError extends Error {
|
|
|
319
316
|
constructor();
|
|
320
317
|
}
|
|
321
318
|
declare class InvalidCommandNameError extends Error {
|
|
319
|
+
name: string;
|
|
322
320
|
constructor(name: string);
|
|
323
321
|
}
|
|
324
322
|
|
|
325
|
-
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[], CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>>;
|
|
326
|
-
declare function resolveCommand(commands: CommandRecord, name: string | string[] | SingleCommandType): Command | undefined;
|
|
327
|
-
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
328
|
-
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
329
|
-
declare function resolveParametersBeforeFlag(argv: string[]
|
|
323
|
+
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof SingleCommand, CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof SingleCommand>, _clerc_utils.Dict<FlagOptions>>>>;
|
|
324
|
+
declare function resolveCommand(commands: CommandRecord, name: string | string[] | SingleCommandType): Command<string | SingleCommandType> | undefined;
|
|
325
|
+
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof SingleCommand>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
326
|
+
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof SingleCommand>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
327
|
+
declare function resolveParametersBeforeFlag(argv: string[]): string[];
|
|
330
328
|
declare const resolveArgv: () => string[];
|
|
331
329
|
declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
|
|
332
330
|
declare const isInvalidName: (name: CommandType) => boolean;
|
|
@@ -339,4 +337,4 @@ interface ParsedParameter {
|
|
|
339
337
|
declare function parseParameters(parameters: string[]): ParsedParameter[];
|
|
340
338
|
declare function mapParametersToArguments(mapping: Record<string, string | string[]>, parameters: ParsedParameter[], cliArguments: string[]): Error | undefined;
|
|
341
339
|
|
|
342
|
-
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandType, CommandWithHandler,
|
|
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, SingleCommand, SingleCommandType, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, isInvalidName, mapParametersToArguments, parseParameters, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent };
|
package/dist/index.js
CHANGED
|
@@ -3,29 +3,16 @@ import { typeFlag } from 'type-flag';
|
|
|
3
3
|
import { toArray, arrayStartsWith, camelCase } from '@clerc/utils';
|
|
4
4
|
import { isNode, isDeno } from 'is-platform';
|
|
5
5
|
|
|
6
|
-
class SingleCommandError extends Error {
|
|
7
|
-
constructor() {
|
|
8
|
-
super("Single command mode enabled.");
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
class SingleCommandAliasError extends Error {
|
|
12
|
-
constructor() {
|
|
13
|
-
super("Single command cannot have alias.");
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
6
|
class CommandExistsError extends Error {
|
|
17
7
|
constructor(name) {
|
|
18
8
|
super(`Command "${name}" exists.`);
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
class CommonCommandExistsError extends Error {
|
|
22
|
-
constructor() {
|
|
23
|
-
super("Common command exists.");
|
|
9
|
+
this.name = name;
|
|
24
10
|
}
|
|
25
11
|
}
|
|
26
12
|
class NoSuchCommandError extends Error {
|
|
27
13
|
constructor(name) {
|
|
28
14
|
super(`No such command: ${name}`);
|
|
15
|
+
this.name = name;
|
|
29
16
|
}
|
|
30
17
|
}
|
|
31
18
|
class NoCommandGivenError extends Error {
|
|
@@ -36,6 +23,8 @@ class NoCommandGivenError extends Error {
|
|
|
36
23
|
class CommandNameConflictError extends Error {
|
|
37
24
|
constructor(n1, n2) {
|
|
38
25
|
super(`Command name ${n1} conflicts with ${n2}. Maybe caused by alias.`);
|
|
26
|
+
this.n1 = n1;
|
|
27
|
+
this.n2 = n2;
|
|
39
28
|
}
|
|
40
29
|
}
|
|
41
30
|
class NameNotSetError extends Error {
|
|
@@ -56,21 +45,29 @@ class VersionNotSetError extends Error {
|
|
|
56
45
|
class InvalidCommandNameError extends Error {
|
|
57
46
|
constructor(name) {
|
|
58
47
|
super(`Bad name format: ${name}`);
|
|
48
|
+
this.name = name;
|
|
59
49
|
}
|
|
60
50
|
}
|
|
61
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
|
+
}
|
|
62
63
|
function resolveFlattenCommands(commands) {
|
|
63
64
|
const commandsMap = /* @__PURE__ */ new Map();
|
|
65
|
+
if (commands[SingleCommand]) {
|
|
66
|
+
commandsMap.set(SingleCommand, commands[SingleCommand]);
|
|
67
|
+
setCommand(commandsMap, commands, commands[SingleCommand]);
|
|
68
|
+
}
|
|
64
69
|
for (const command of Object.values(commands)) {
|
|
65
|
-
|
|
66
|
-
const aliases = toArray(command.alias);
|
|
67
|
-
for (const alias of aliases) {
|
|
68
|
-
if (alias in commands) {
|
|
69
|
-
throw new CommandNameConflictError(commands[alias].name, command.name);
|
|
70
|
-
}
|
|
71
|
-
commandsMap.set(alias.split(" "), { ...command, __isAlias: true });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
70
|
+
setCommand(commandsMap, commands, command);
|
|
74
71
|
commandsMap.set(command.name.split(" "), command);
|
|
75
72
|
}
|
|
76
73
|
return commandsMap;
|
|
@@ -84,7 +81,12 @@ function resolveCommand(commands, name) {
|
|
|
84
81
|
let current;
|
|
85
82
|
let currentName;
|
|
86
83
|
commandsMap.forEach((v, k) => {
|
|
87
|
-
if (
|
|
84
|
+
if (k === SingleCommand) {
|
|
85
|
+
current = commandsMap.get(SingleCommand);
|
|
86
|
+
currentName = SingleCommand;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (arrayStartsWith(nameArr, k) && (!currentName || currentName === SingleCommand || k.length > currentName.length)) {
|
|
88
90
|
current = v;
|
|
89
91
|
currentName = k;
|
|
90
92
|
}
|
|
@@ -99,10 +101,7 @@ function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
|
99
101
|
});
|
|
100
102
|
}
|
|
101
103
|
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|
|
102
|
-
function resolveParametersBeforeFlag(argv
|
|
103
|
-
if (isSingleCommand) {
|
|
104
|
-
return [];
|
|
105
|
-
}
|
|
104
|
+
function resolveParametersBeforeFlag(argv) {
|
|
106
105
|
const parameters = [];
|
|
107
106
|
for (const arg of argv) {
|
|
108
107
|
if (arg.startsWith("-")) {
|
|
@@ -212,12 +211,12 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
212
211
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
213
212
|
return value;
|
|
214
213
|
};
|
|
215
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames,
|
|
214
|
+
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasSingleCommandOrAlias, hasSingleCommandOrAlias_get, _hasSingleCommand, hasSingleCommand_get;
|
|
216
215
|
const SingleCommand = Symbol("SingleCommand");
|
|
217
216
|
const _Clerc = class {
|
|
218
|
-
constructor() {
|
|
219
|
-
__privateAdd(this,
|
|
220
|
-
__privateAdd(this,
|
|
217
|
+
constructor(name, description, version) {
|
|
218
|
+
__privateAdd(this, _hasSingleCommandOrAlias);
|
|
219
|
+
__privateAdd(this, _hasSingleCommand);
|
|
221
220
|
__privateAdd(this, _name, "");
|
|
222
221
|
__privateAdd(this, _description, "");
|
|
223
222
|
__privateAdd(this, _version, "");
|
|
@@ -225,6 +224,9 @@ const _Clerc = class {
|
|
|
225
224
|
__privateAdd(this, _commands, {});
|
|
226
225
|
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
227
226
|
__privateAdd(this, _usedNames, []);
|
|
227
|
+
__privateSet(this, _name, name || __privateGet(this, _name));
|
|
228
|
+
__privateSet(this, _description, description || __privateGet(this, _description));
|
|
229
|
+
__privateSet(this, _version, version || __privateGet(this, _version));
|
|
228
230
|
}
|
|
229
231
|
get _name() {
|
|
230
232
|
return __privateGet(this, _name);
|
|
@@ -241,8 +243,8 @@ const _Clerc = class {
|
|
|
241
243
|
get _commands() {
|
|
242
244
|
return __privateGet(this, _commands);
|
|
243
245
|
}
|
|
244
|
-
static create() {
|
|
245
|
-
return new _Clerc();
|
|
246
|
+
static create(name, description, version) {
|
|
247
|
+
return new _Clerc(name, description, version);
|
|
246
248
|
}
|
|
247
249
|
name(name) {
|
|
248
250
|
__privateSet(this, _name, name);
|
|
@@ -256,27 +258,16 @@ const _Clerc = class {
|
|
|
256
258
|
__privateSet(this, _version, version);
|
|
257
259
|
return this;
|
|
258
260
|
}
|
|
259
|
-
command(nameOrCommand, description, options) {
|
|
261
|
+
command(nameOrCommand, description, options = {}) {
|
|
260
262
|
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === SingleCommand);
|
|
261
263
|
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
262
264
|
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
263
265
|
if (__privateGet(this, _commands)[name]) {
|
|
264
|
-
|
|
265
|
-
throw new CommandExistsError("SingleCommand");
|
|
266
|
-
}
|
|
266
|
+
throw new CommandExistsError(typeof name === "symbol" ? "" : name);
|
|
267
267
|
}
|
|
268
268
|
if (isInvalidName(name)) {
|
|
269
269
|
throw new InvalidCommandNameError(name);
|
|
270
270
|
}
|
|
271
|
-
if (__privateGet(this, _isSingleCommand, isSingleCommand_get)) {
|
|
272
|
-
throw new SingleCommandError();
|
|
273
|
-
}
|
|
274
|
-
if (name === SingleCommand && __privateGet(this, _hasCommands, hasCommands_get)) {
|
|
275
|
-
throw new CommonCommandExistsError();
|
|
276
|
-
}
|
|
277
|
-
if (name === SingleCommand && (isCommandObject ? nameOrCommand : options).alias) {
|
|
278
|
-
throw new SingleCommandAliasError();
|
|
279
|
-
}
|
|
280
271
|
const { handler = void 0, ...commandToSave } = isCommandObject ? nameOrCommand : { name, description, ...options };
|
|
281
272
|
const nameList = [commandToSave.name];
|
|
282
273
|
commandToSave.alias && nameList.push(...toArray(commandToSave.alias));
|
|
@@ -311,9 +302,9 @@ const _Clerc = class {
|
|
|
311
302
|
if (!__privateGet(this, _version)) {
|
|
312
303
|
throw new VersionNotSetError();
|
|
313
304
|
}
|
|
314
|
-
const name = resolveParametersBeforeFlag(argv
|
|
305
|
+
const name = resolveParametersBeforeFlag(argv);
|
|
315
306
|
const stringName = name.join(" ");
|
|
316
|
-
const getCommand = () =>
|
|
307
|
+
const getCommand = () => resolveCommand(__privateGet(this, _commands), name);
|
|
317
308
|
const mapErrors = [];
|
|
318
309
|
const getContext = () => {
|
|
319
310
|
mapErrors.length = 0;
|
|
@@ -321,7 +312,7 @@ const _Clerc = class {
|
|
|
321
312
|
const isCommandResolved = !!command;
|
|
322
313
|
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
323
314
|
const { _: args, flags, unknownFlags } = parsed;
|
|
324
|
-
let parameters =
|
|
315
|
+
let parameters = !isCommandResolved || command.name === SingleCommand ? args : args.slice(command.name.split(" ").length);
|
|
325
316
|
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
326
317
|
const hasEof = commandParameters.indexOf("--");
|
|
327
318
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
@@ -350,8 +341,10 @@ const _Clerc = class {
|
|
|
350
341
|
const mergedFlags = { ...flags, ...unknownFlags };
|
|
351
342
|
const context = {
|
|
352
343
|
name: command == null ? void 0 : command.name,
|
|
344
|
+
called: name.length === 0 ? SingleCommand : stringName,
|
|
353
345
|
resolved: isCommandResolved,
|
|
354
|
-
|
|
346
|
+
hasSingleCommandOrAlias: __privateGet(this, _hasSingleCommandOrAlias, hasSingleCommandOrAlias_get),
|
|
347
|
+
hasSingleCommand: __privateGet(this, _hasSingleCommand, hasSingleCommand_get),
|
|
355
348
|
raw: { ...parsed, parameters, mergedFlags },
|
|
356
349
|
parameters: mapping,
|
|
357
350
|
flags,
|
|
@@ -393,13 +386,13 @@ _inspectors = new WeakMap();
|
|
|
393
386
|
_commands = new WeakMap();
|
|
394
387
|
_commandEmitter = new WeakMap();
|
|
395
388
|
_usedNames = new WeakMap();
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
return __privateGet(this,
|
|
389
|
+
_hasSingleCommandOrAlias = new WeakSet();
|
|
390
|
+
hasSingleCommandOrAlias_get = function() {
|
|
391
|
+
return __privateGet(this, _usedNames).includes(SingleCommand);
|
|
399
392
|
};
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
return
|
|
393
|
+
_hasSingleCommand = new WeakSet();
|
|
394
|
+
hasSingleCommand_get = function() {
|
|
395
|
+
return !!__privateGet(this, _commands)[SingleCommand];
|
|
403
396
|
};
|
|
404
397
|
|
|
405
398
|
const definePlugin = (p) => p;
|
|
@@ -407,4 +400,4 @@ const defineHandler = (_cli, _key, handler) => handler;
|
|
|
407
400
|
const defineInspector = (_cli, inspector) => inspector;
|
|
408
401
|
const defineCommand = (command) => command;
|
|
409
402
|
|
|
410
|
-
export { Clerc, CommandExistsError, CommandNameConflictError,
|
|
403
|
+
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, SingleCommand, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, isInvalidName, mapParametersToArguments, parseParameters, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent };
|
package/dist/index.mjs
CHANGED
|
@@ -3,29 +3,16 @@ import { typeFlag } from 'type-flag';
|
|
|
3
3
|
import { toArray, arrayStartsWith, camelCase } from '@clerc/utils';
|
|
4
4
|
import { isNode, isDeno } from 'is-platform';
|
|
5
5
|
|
|
6
|
-
class SingleCommandError extends Error {
|
|
7
|
-
constructor() {
|
|
8
|
-
super("Single command mode enabled.");
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
class SingleCommandAliasError extends Error {
|
|
12
|
-
constructor() {
|
|
13
|
-
super("Single command cannot have alias.");
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
6
|
class CommandExistsError extends Error {
|
|
17
7
|
constructor(name) {
|
|
18
8
|
super(`Command "${name}" exists.`);
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
class CommonCommandExistsError extends Error {
|
|
22
|
-
constructor() {
|
|
23
|
-
super("Common command exists.");
|
|
9
|
+
this.name = name;
|
|
24
10
|
}
|
|
25
11
|
}
|
|
26
12
|
class NoSuchCommandError extends Error {
|
|
27
13
|
constructor(name) {
|
|
28
14
|
super(`No such command: ${name}`);
|
|
15
|
+
this.name = name;
|
|
29
16
|
}
|
|
30
17
|
}
|
|
31
18
|
class NoCommandGivenError extends Error {
|
|
@@ -36,6 +23,8 @@ class NoCommandGivenError extends Error {
|
|
|
36
23
|
class CommandNameConflictError extends Error {
|
|
37
24
|
constructor(n1, n2) {
|
|
38
25
|
super(`Command name ${n1} conflicts with ${n2}. Maybe caused by alias.`);
|
|
26
|
+
this.n1 = n1;
|
|
27
|
+
this.n2 = n2;
|
|
39
28
|
}
|
|
40
29
|
}
|
|
41
30
|
class NameNotSetError extends Error {
|
|
@@ -56,21 +45,29 @@ class VersionNotSetError extends Error {
|
|
|
56
45
|
class InvalidCommandNameError extends Error {
|
|
57
46
|
constructor(name) {
|
|
58
47
|
super(`Bad name format: ${name}`);
|
|
48
|
+
this.name = name;
|
|
59
49
|
}
|
|
60
50
|
}
|
|
61
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
|
+
}
|
|
62
63
|
function resolveFlattenCommands(commands) {
|
|
63
64
|
const commandsMap = /* @__PURE__ */ new Map();
|
|
65
|
+
if (commands[SingleCommand]) {
|
|
66
|
+
commandsMap.set(SingleCommand, commands[SingleCommand]);
|
|
67
|
+
setCommand(commandsMap, commands, commands[SingleCommand]);
|
|
68
|
+
}
|
|
64
69
|
for (const command of Object.values(commands)) {
|
|
65
|
-
|
|
66
|
-
const aliases = toArray(command.alias);
|
|
67
|
-
for (const alias of aliases) {
|
|
68
|
-
if (alias in commands) {
|
|
69
|
-
throw new CommandNameConflictError(commands[alias].name, command.name);
|
|
70
|
-
}
|
|
71
|
-
commandsMap.set(alias.split(" "), { ...command, __isAlias: true });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
70
|
+
setCommand(commandsMap, commands, command);
|
|
74
71
|
commandsMap.set(command.name.split(" "), command);
|
|
75
72
|
}
|
|
76
73
|
return commandsMap;
|
|
@@ -84,7 +81,12 @@ function resolveCommand(commands, name) {
|
|
|
84
81
|
let current;
|
|
85
82
|
let currentName;
|
|
86
83
|
commandsMap.forEach((v, k) => {
|
|
87
|
-
if (
|
|
84
|
+
if (k === SingleCommand) {
|
|
85
|
+
current = commandsMap.get(SingleCommand);
|
|
86
|
+
currentName = SingleCommand;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (arrayStartsWith(nameArr, k) && (!currentName || currentName === SingleCommand || k.length > currentName.length)) {
|
|
88
90
|
current = v;
|
|
89
91
|
currentName = k;
|
|
90
92
|
}
|
|
@@ -99,10 +101,7 @@ function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
|
99
101
|
});
|
|
100
102
|
}
|
|
101
103
|
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|
|
102
|
-
function resolveParametersBeforeFlag(argv
|
|
103
|
-
if (isSingleCommand) {
|
|
104
|
-
return [];
|
|
105
|
-
}
|
|
104
|
+
function resolveParametersBeforeFlag(argv) {
|
|
106
105
|
const parameters = [];
|
|
107
106
|
for (const arg of argv) {
|
|
108
107
|
if (arg.startsWith("-")) {
|
|
@@ -212,12 +211,12 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
212
211
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
213
212
|
return value;
|
|
214
213
|
};
|
|
215
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames,
|
|
214
|
+
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasSingleCommandOrAlias, hasSingleCommandOrAlias_get, _hasSingleCommand, hasSingleCommand_get;
|
|
216
215
|
const SingleCommand = Symbol("SingleCommand");
|
|
217
216
|
const _Clerc = class {
|
|
218
|
-
constructor() {
|
|
219
|
-
__privateAdd(this,
|
|
220
|
-
__privateAdd(this,
|
|
217
|
+
constructor(name, description, version) {
|
|
218
|
+
__privateAdd(this, _hasSingleCommandOrAlias);
|
|
219
|
+
__privateAdd(this, _hasSingleCommand);
|
|
221
220
|
__privateAdd(this, _name, "");
|
|
222
221
|
__privateAdd(this, _description, "");
|
|
223
222
|
__privateAdd(this, _version, "");
|
|
@@ -225,6 +224,9 @@ const _Clerc = class {
|
|
|
225
224
|
__privateAdd(this, _commands, {});
|
|
226
225
|
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
227
226
|
__privateAdd(this, _usedNames, []);
|
|
227
|
+
__privateSet(this, _name, name || __privateGet(this, _name));
|
|
228
|
+
__privateSet(this, _description, description || __privateGet(this, _description));
|
|
229
|
+
__privateSet(this, _version, version || __privateGet(this, _version));
|
|
228
230
|
}
|
|
229
231
|
get _name() {
|
|
230
232
|
return __privateGet(this, _name);
|
|
@@ -241,8 +243,8 @@ const _Clerc = class {
|
|
|
241
243
|
get _commands() {
|
|
242
244
|
return __privateGet(this, _commands);
|
|
243
245
|
}
|
|
244
|
-
static create() {
|
|
245
|
-
return new _Clerc();
|
|
246
|
+
static create(name, description, version) {
|
|
247
|
+
return new _Clerc(name, description, version);
|
|
246
248
|
}
|
|
247
249
|
name(name) {
|
|
248
250
|
__privateSet(this, _name, name);
|
|
@@ -256,27 +258,16 @@ const _Clerc = class {
|
|
|
256
258
|
__privateSet(this, _version, version);
|
|
257
259
|
return this;
|
|
258
260
|
}
|
|
259
|
-
command(nameOrCommand, description, options) {
|
|
261
|
+
command(nameOrCommand, description, options = {}) {
|
|
260
262
|
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === SingleCommand);
|
|
261
263
|
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
262
264
|
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
263
265
|
if (__privateGet(this, _commands)[name]) {
|
|
264
|
-
|
|
265
|
-
throw new CommandExistsError("SingleCommand");
|
|
266
|
-
}
|
|
266
|
+
throw new CommandExistsError(typeof name === "symbol" ? "" : name);
|
|
267
267
|
}
|
|
268
268
|
if (isInvalidName(name)) {
|
|
269
269
|
throw new InvalidCommandNameError(name);
|
|
270
270
|
}
|
|
271
|
-
if (__privateGet(this, _isSingleCommand, isSingleCommand_get)) {
|
|
272
|
-
throw new SingleCommandError();
|
|
273
|
-
}
|
|
274
|
-
if (name === SingleCommand && __privateGet(this, _hasCommands, hasCommands_get)) {
|
|
275
|
-
throw new CommonCommandExistsError();
|
|
276
|
-
}
|
|
277
|
-
if (name === SingleCommand && (isCommandObject ? nameOrCommand : options).alias) {
|
|
278
|
-
throw new SingleCommandAliasError();
|
|
279
|
-
}
|
|
280
271
|
const { handler = void 0, ...commandToSave } = isCommandObject ? nameOrCommand : { name, description, ...options };
|
|
281
272
|
const nameList = [commandToSave.name];
|
|
282
273
|
commandToSave.alias && nameList.push(...toArray(commandToSave.alias));
|
|
@@ -311,9 +302,9 @@ const _Clerc = class {
|
|
|
311
302
|
if (!__privateGet(this, _version)) {
|
|
312
303
|
throw new VersionNotSetError();
|
|
313
304
|
}
|
|
314
|
-
const name = resolveParametersBeforeFlag(argv
|
|
305
|
+
const name = resolveParametersBeforeFlag(argv);
|
|
315
306
|
const stringName = name.join(" ");
|
|
316
|
-
const getCommand = () =>
|
|
307
|
+
const getCommand = () => resolveCommand(__privateGet(this, _commands), name);
|
|
317
308
|
const mapErrors = [];
|
|
318
309
|
const getContext = () => {
|
|
319
310
|
mapErrors.length = 0;
|
|
@@ -321,7 +312,7 @@ const _Clerc = class {
|
|
|
321
312
|
const isCommandResolved = !!command;
|
|
322
313
|
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
323
314
|
const { _: args, flags, unknownFlags } = parsed;
|
|
324
|
-
let parameters =
|
|
315
|
+
let parameters = !isCommandResolved || command.name === SingleCommand ? args : args.slice(command.name.split(" ").length);
|
|
325
316
|
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
326
317
|
const hasEof = commandParameters.indexOf("--");
|
|
327
318
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
@@ -350,8 +341,10 @@ const _Clerc = class {
|
|
|
350
341
|
const mergedFlags = { ...flags, ...unknownFlags };
|
|
351
342
|
const context = {
|
|
352
343
|
name: command == null ? void 0 : command.name,
|
|
344
|
+
called: name.length === 0 ? SingleCommand : stringName,
|
|
353
345
|
resolved: isCommandResolved,
|
|
354
|
-
|
|
346
|
+
hasSingleCommandOrAlias: __privateGet(this, _hasSingleCommandOrAlias, hasSingleCommandOrAlias_get),
|
|
347
|
+
hasSingleCommand: __privateGet(this, _hasSingleCommand, hasSingleCommand_get),
|
|
355
348
|
raw: { ...parsed, parameters, mergedFlags },
|
|
356
349
|
parameters: mapping,
|
|
357
350
|
flags,
|
|
@@ -393,13 +386,13 @@ _inspectors = new WeakMap();
|
|
|
393
386
|
_commands = new WeakMap();
|
|
394
387
|
_commandEmitter = new WeakMap();
|
|
395
388
|
_usedNames = new WeakMap();
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
return __privateGet(this,
|
|
389
|
+
_hasSingleCommandOrAlias = new WeakSet();
|
|
390
|
+
hasSingleCommandOrAlias_get = function() {
|
|
391
|
+
return __privateGet(this, _usedNames).includes(SingleCommand);
|
|
399
392
|
};
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
return
|
|
393
|
+
_hasSingleCommand = new WeakSet();
|
|
394
|
+
hasSingleCommand_get = function() {
|
|
395
|
+
return !!__privateGet(this, _commands)[SingleCommand];
|
|
403
396
|
};
|
|
404
397
|
|
|
405
398
|
const definePlugin = (p) => p;
|
|
@@ -407,4 +400,4 @@ const defineHandler = (_cli, _key, handler) => handler;
|
|
|
407
400
|
const defineInspector = (_cli, inspector) => inspector;
|
|
408
401
|
const defineCommand = (command) => command;
|
|
409
402
|
|
|
410
|
-
export { Clerc, CommandExistsError, CommandNameConflictError,
|
|
403
|
+
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, SingleCommand, 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.21.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.21.0"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "puild",
|