@clerc/core 0.18.0 → 0.20.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 +5 -14
- package/dist/index.js +31 -54
- package/dist/index.mjs +31 -54
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -121,7 +121,7 @@ type Raw<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> =
|
|
|
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
123
|
resolved: N extends keyof C ? true : boolean;
|
|
124
|
-
|
|
124
|
+
hasSingleCommand: boolean;
|
|
125
125
|
raw: Raw<C, N>;
|
|
126
126
|
parameters: TransformParameters<C, N>;
|
|
127
127
|
unknownFlags: ParsedFlags["unknownFlags"];
|
|
@@ -288,18 +288,9 @@ declare const defineHandler: <C extends Clerc<{}>, K extends keyof C["_commands"
|
|
|
288
288
|
declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
289
289
|
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
290
|
|
|
291
|
-
declare class SingleCommandError extends Error {
|
|
292
|
-
constructor();
|
|
293
|
-
}
|
|
294
|
-
declare class SingleCommandAliasError extends Error {
|
|
295
|
-
constructor();
|
|
296
|
-
}
|
|
297
291
|
declare class CommandExistsError extends Error {
|
|
298
292
|
constructor(name: string);
|
|
299
293
|
}
|
|
300
|
-
declare class CommonCommandExistsError extends Error {
|
|
301
|
-
constructor();
|
|
302
|
-
}
|
|
303
294
|
declare class NoSuchCommandError extends Error {
|
|
304
295
|
constructor(name: string);
|
|
305
296
|
}
|
|
@@ -322,11 +313,11 @@ declare class InvalidCommandNameError extends Error {
|
|
|
322
313
|
constructor(name: string);
|
|
323
314
|
}
|
|
324
315
|
|
|
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;
|
|
316
|
+
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof SingleCommand, CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>>;
|
|
317
|
+
declare function resolveCommand(commands: CommandRecord, name: string | string[] | SingleCommandType): Command<string | SingleCommandType> | undefined;
|
|
327
318
|
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
328
319
|
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
|
|
329
|
-
declare function resolveParametersBeforeFlag(argv: string[]
|
|
320
|
+
declare function resolveParametersBeforeFlag(argv: string[]): string[];
|
|
330
321
|
declare const resolveArgv: () => string[];
|
|
331
322
|
declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
|
|
332
323
|
declare const isInvalidName: (name: CommandType) => boolean;
|
|
@@ -339,4 +330,4 @@ interface ParsedParameter {
|
|
|
339
330
|
declare function parseParameters(parameters: string[]): ParsedParameter[];
|
|
340
331
|
declare function mapParametersToArguments(mapping: Record<string, string | string[]>, parameters: ParsedParameter[], cliArguments: string[]): Error | undefined;
|
|
341
332
|
|
|
342
|
-
export { Clerc, Command, CommandAlias, CommandCustomProperties, CommandExistsError, CommandNameConflictError, CommandOptions, CommandRecord, CommandType, CommandWithHandler,
|
|
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, SingleCommand, SingleCommandType, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, isInvalidName, mapParametersToArguments, parseParameters, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent };
|
package/dist/index.js
CHANGED
|
@@ -3,26 +3,11 @@ 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
9
|
}
|
|
20
10
|
}
|
|
21
|
-
class CommonCommandExistsError extends Error {
|
|
22
|
-
constructor() {
|
|
23
|
-
super("Common command exists.");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
11
|
class NoSuchCommandError extends Error {
|
|
27
12
|
constructor(name) {
|
|
28
13
|
super(`No such command: ${name}`);
|
|
@@ -61,6 +46,9 @@ class InvalidCommandNameError extends Error {
|
|
|
61
46
|
|
|
62
47
|
function resolveFlattenCommands(commands) {
|
|
63
48
|
const commandsMap = /* @__PURE__ */ new Map();
|
|
49
|
+
if (commands[SingleCommand]) {
|
|
50
|
+
commandsMap.set(SingleCommand, commands[SingleCommand]);
|
|
51
|
+
}
|
|
64
52
|
for (const command of Object.values(commands)) {
|
|
65
53
|
if (command.alias) {
|
|
66
54
|
const aliases = toArray(command.alias);
|
|
@@ -84,7 +72,12 @@ function resolveCommand(commands, name) {
|
|
|
84
72
|
let current;
|
|
85
73
|
let currentName;
|
|
86
74
|
commandsMap.forEach((v, k) => {
|
|
87
|
-
if (
|
|
75
|
+
if (k === SingleCommand) {
|
|
76
|
+
current = commandsMap.get(SingleCommand);
|
|
77
|
+
currentName = SingleCommand;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (arrayStartsWith(nameArr, k) && (!currentName || currentName === SingleCommand || k.length > currentName.length)) {
|
|
88
81
|
current = v;
|
|
89
82
|
currentName = k;
|
|
90
83
|
}
|
|
@@ -99,10 +92,7 @@ function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
|
99
92
|
});
|
|
100
93
|
}
|
|
101
94
|
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|
|
102
|
-
function resolveParametersBeforeFlag(argv
|
|
103
|
-
if (isSingleCommand) {
|
|
104
|
-
return [];
|
|
105
|
-
}
|
|
95
|
+
function resolveParametersBeforeFlag(argv) {
|
|
106
96
|
const parameters = [];
|
|
107
97
|
for (const arg of argv) {
|
|
108
98
|
if (arg.startsWith("-")) {
|
|
@@ -212,18 +202,18 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
212
202
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
213
203
|
return value;
|
|
214
204
|
};
|
|
215
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter,
|
|
205
|
+
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasSingleCommand;
|
|
216
206
|
const SingleCommand = Symbol("SingleCommand");
|
|
217
207
|
const _Clerc = class {
|
|
218
208
|
constructor() {
|
|
219
|
-
__privateAdd(this, _isSingleCommand);
|
|
220
|
-
__privateAdd(this, _hasCommands);
|
|
221
209
|
__privateAdd(this, _name, "");
|
|
222
210
|
__privateAdd(this, _description, "");
|
|
223
211
|
__privateAdd(this, _version, "");
|
|
224
212
|
__privateAdd(this, _inspectors, []);
|
|
225
213
|
__privateAdd(this, _commands, {});
|
|
226
214
|
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
215
|
+
__privateAdd(this, _usedNames, []);
|
|
216
|
+
__privateAdd(this, _hasSingleCommand, false);
|
|
227
217
|
}
|
|
228
218
|
get _name() {
|
|
229
219
|
return __privateGet(this, _name);
|
|
@@ -255,36 +245,29 @@ const _Clerc = class {
|
|
|
255
245
|
__privateSet(this, _version, version);
|
|
256
246
|
return this;
|
|
257
247
|
}
|
|
258
|
-
command(nameOrCommand, description, options) {
|
|
248
|
+
command(nameOrCommand, description, options = {}) {
|
|
259
249
|
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === SingleCommand);
|
|
260
250
|
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
261
251
|
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
262
252
|
if (__privateGet(this, _commands)[name]) {
|
|
263
|
-
|
|
264
|
-
throw new CommandExistsError("SingleCommand");
|
|
265
|
-
}
|
|
253
|
+
throw new CommandExistsError(typeof name === "symbol" ? "" : name);
|
|
266
254
|
}
|
|
267
255
|
if (isInvalidName(name)) {
|
|
268
256
|
throw new InvalidCommandNameError(name);
|
|
269
257
|
}
|
|
270
|
-
if (__privateGet(this, _isSingleCommand, isSingleCommand_get)) {
|
|
271
|
-
throw new SingleCommandError();
|
|
272
|
-
}
|
|
273
|
-
if (name === SingleCommand && __privateGet(this, _hasCommands, hasCommands_get)) {
|
|
274
|
-
throw new CommonCommandExistsError();
|
|
275
|
-
}
|
|
276
|
-
if (name === SingleCommand && (isCommandObject ? nameOrCommand : options).alias) {
|
|
277
|
-
throw new SingleCommandAliasError();
|
|
278
|
-
}
|
|
279
258
|
const { handler = void 0, ...commandToSave } = isCommandObject ? nameOrCommand : { name, description, ...options };
|
|
280
259
|
const nameList = [commandToSave.name];
|
|
281
260
|
commandToSave.alias && nameList.push(...toArray(commandToSave.alias));
|
|
282
261
|
for (const name2 of nameList) {
|
|
283
|
-
if (
|
|
262
|
+
if (__privateGet(this, _usedNames).includes(name2)) {
|
|
284
263
|
throw new CommandExistsError(name2);
|
|
285
264
|
}
|
|
286
265
|
}
|
|
266
|
+
if (nameList.includes(SingleCommand)) {
|
|
267
|
+
__privateSet(this, _hasSingleCommand, true);
|
|
268
|
+
}
|
|
287
269
|
__privateGet(this, _commands)[name] = commandToSave;
|
|
270
|
+
__privateGet(this, _usedNames).push(commandToSave.name, ...toArray(commandToSave.alias) || []);
|
|
288
271
|
isCommandObject && handler && this.on(nameOrCommand.name, handler);
|
|
289
272
|
return this;
|
|
290
273
|
}
|
|
@@ -309,24 +292,24 @@ const _Clerc = class {
|
|
|
309
292
|
if (!__privateGet(this, _version)) {
|
|
310
293
|
throw new VersionNotSetError();
|
|
311
294
|
}
|
|
312
|
-
const name = resolveParametersBeforeFlag(argv
|
|
295
|
+
const name = resolveParametersBeforeFlag(argv);
|
|
313
296
|
const stringName = name.join(" ");
|
|
314
|
-
const getCommand = () =>
|
|
297
|
+
const getCommand = () => resolveCommand(__privateGet(this, _commands), name);
|
|
315
298
|
const mapErrors = [];
|
|
316
299
|
const getContext = () => {
|
|
317
300
|
mapErrors.length = 0;
|
|
318
301
|
const command = getCommand();
|
|
319
302
|
const isCommandResolved = !!command;
|
|
320
303
|
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
321
|
-
const { _: args, flags } = parsed;
|
|
322
|
-
let parameters =
|
|
304
|
+
const { _: args, flags, unknownFlags } = parsed;
|
|
305
|
+
let parameters = !isCommandResolved || command.name === SingleCommand ? args : args.slice(command.name.split(" ").length);
|
|
323
306
|
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
324
307
|
const hasEof = commandParameters.indexOf("--");
|
|
325
308
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
326
309
|
const mapping = /* @__PURE__ */ Object.create(null);
|
|
327
310
|
if (hasEof > -1 && eofParameters.length > 0) {
|
|
328
311
|
commandParameters = commandParameters.slice(0, hasEof);
|
|
329
|
-
const eofArguments =
|
|
312
|
+
const eofArguments = args["--"];
|
|
330
313
|
parameters = parameters.slice(0, -eofArguments.length || void 0);
|
|
331
314
|
mapErrors.push(mapParametersToArguments(
|
|
332
315
|
mapping,
|
|
@@ -345,15 +328,15 @@ const _Clerc = class {
|
|
|
345
328
|
parameters
|
|
346
329
|
));
|
|
347
330
|
}
|
|
348
|
-
const mergedFlags = { ...
|
|
331
|
+
const mergedFlags = { ...flags, ...unknownFlags };
|
|
349
332
|
const context = {
|
|
350
333
|
name: command == null ? void 0 : command.name,
|
|
351
334
|
resolved: isCommandResolved,
|
|
352
|
-
|
|
335
|
+
hasSingleCommand: __privateGet(this, _hasSingleCommand),
|
|
353
336
|
raw: { ...parsed, parameters, mergedFlags },
|
|
354
337
|
parameters: mapping,
|
|
355
338
|
flags,
|
|
356
|
-
unknownFlags
|
|
339
|
+
unknownFlags,
|
|
357
340
|
cli: this
|
|
358
341
|
};
|
|
359
342
|
return context;
|
|
@@ -390,18 +373,12 @@ _version = new WeakMap();
|
|
|
390
373
|
_inspectors = new WeakMap();
|
|
391
374
|
_commands = new WeakMap();
|
|
392
375
|
_commandEmitter = new WeakMap();
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
return __privateGet(this, _commands)[SingleCommand] !== void 0;
|
|
396
|
-
};
|
|
397
|
-
_hasCommands = new WeakSet();
|
|
398
|
-
hasCommands_get = function() {
|
|
399
|
-
return Object.keys(__privateGet(this, _commands)).length > 0;
|
|
400
|
-
};
|
|
376
|
+
_usedNames = new WeakMap();
|
|
377
|
+
_hasSingleCommand = new WeakMap();
|
|
401
378
|
|
|
402
379
|
const definePlugin = (p) => p;
|
|
403
380
|
const defineHandler = (_cli, _key, handler) => handler;
|
|
404
381
|
const defineInspector = (_cli, inspector) => inspector;
|
|
405
382
|
const defineCommand = (command) => command;
|
|
406
383
|
|
|
407
|
-
export { Clerc, CommandExistsError, CommandNameConflictError,
|
|
384
|
+
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,26 +3,11 @@ 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
9
|
}
|
|
20
10
|
}
|
|
21
|
-
class CommonCommandExistsError extends Error {
|
|
22
|
-
constructor() {
|
|
23
|
-
super("Common command exists.");
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
11
|
class NoSuchCommandError extends Error {
|
|
27
12
|
constructor(name) {
|
|
28
13
|
super(`No such command: ${name}`);
|
|
@@ -61,6 +46,9 @@ class InvalidCommandNameError extends Error {
|
|
|
61
46
|
|
|
62
47
|
function resolveFlattenCommands(commands) {
|
|
63
48
|
const commandsMap = /* @__PURE__ */ new Map();
|
|
49
|
+
if (commands[SingleCommand]) {
|
|
50
|
+
commandsMap.set(SingleCommand, commands[SingleCommand]);
|
|
51
|
+
}
|
|
64
52
|
for (const command of Object.values(commands)) {
|
|
65
53
|
if (command.alias) {
|
|
66
54
|
const aliases = toArray(command.alias);
|
|
@@ -84,7 +72,12 @@ function resolveCommand(commands, name) {
|
|
|
84
72
|
let current;
|
|
85
73
|
let currentName;
|
|
86
74
|
commandsMap.forEach((v, k) => {
|
|
87
|
-
if (
|
|
75
|
+
if (k === SingleCommand) {
|
|
76
|
+
current = commandsMap.get(SingleCommand);
|
|
77
|
+
currentName = SingleCommand;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
if (arrayStartsWith(nameArr, k) && (!currentName || currentName === SingleCommand || k.length > currentName.length)) {
|
|
88
81
|
current = v;
|
|
89
82
|
currentName = k;
|
|
90
83
|
}
|
|
@@ -99,10 +92,7 @@ function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
|
99
92
|
});
|
|
100
93
|
}
|
|
101
94
|
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|
|
102
|
-
function resolveParametersBeforeFlag(argv
|
|
103
|
-
if (isSingleCommand) {
|
|
104
|
-
return [];
|
|
105
|
-
}
|
|
95
|
+
function resolveParametersBeforeFlag(argv) {
|
|
106
96
|
const parameters = [];
|
|
107
97
|
for (const arg of argv) {
|
|
108
98
|
if (arg.startsWith("-")) {
|
|
@@ -212,18 +202,18 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
212
202
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
213
203
|
return value;
|
|
214
204
|
};
|
|
215
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter,
|
|
205
|
+
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasSingleCommand;
|
|
216
206
|
const SingleCommand = Symbol("SingleCommand");
|
|
217
207
|
const _Clerc = class {
|
|
218
208
|
constructor() {
|
|
219
|
-
__privateAdd(this, _isSingleCommand);
|
|
220
|
-
__privateAdd(this, _hasCommands);
|
|
221
209
|
__privateAdd(this, _name, "");
|
|
222
210
|
__privateAdd(this, _description, "");
|
|
223
211
|
__privateAdd(this, _version, "");
|
|
224
212
|
__privateAdd(this, _inspectors, []);
|
|
225
213
|
__privateAdd(this, _commands, {});
|
|
226
214
|
__privateAdd(this, _commandEmitter, new LiteEmit());
|
|
215
|
+
__privateAdd(this, _usedNames, []);
|
|
216
|
+
__privateAdd(this, _hasSingleCommand, false);
|
|
227
217
|
}
|
|
228
218
|
get _name() {
|
|
229
219
|
return __privateGet(this, _name);
|
|
@@ -255,36 +245,29 @@ const _Clerc = class {
|
|
|
255
245
|
__privateSet(this, _version, version);
|
|
256
246
|
return this;
|
|
257
247
|
}
|
|
258
|
-
command(nameOrCommand, description, options) {
|
|
248
|
+
command(nameOrCommand, description, options = {}) {
|
|
259
249
|
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 === SingleCommand);
|
|
260
250
|
const isCommandObject = checkIsCommandObject(nameOrCommand);
|
|
261
251
|
const name = !isCommandObject ? nameOrCommand : nameOrCommand.name;
|
|
262
252
|
if (__privateGet(this, _commands)[name]) {
|
|
263
|
-
|
|
264
|
-
throw new CommandExistsError("SingleCommand");
|
|
265
|
-
}
|
|
253
|
+
throw new CommandExistsError(typeof name === "symbol" ? "" : name);
|
|
266
254
|
}
|
|
267
255
|
if (isInvalidName(name)) {
|
|
268
256
|
throw new InvalidCommandNameError(name);
|
|
269
257
|
}
|
|
270
|
-
if (__privateGet(this, _isSingleCommand, isSingleCommand_get)) {
|
|
271
|
-
throw new SingleCommandError();
|
|
272
|
-
}
|
|
273
|
-
if (name === SingleCommand && __privateGet(this, _hasCommands, hasCommands_get)) {
|
|
274
|
-
throw new CommonCommandExistsError();
|
|
275
|
-
}
|
|
276
|
-
if (name === SingleCommand && (isCommandObject ? nameOrCommand : options).alias) {
|
|
277
|
-
throw new SingleCommandAliasError();
|
|
278
|
-
}
|
|
279
258
|
const { handler = void 0, ...commandToSave } = isCommandObject ? nameOrCommand : { name, description, ...options };
|
|
280
259
|
const nameList = [commandToSave.name];
|
|
281
260
|
commandToSave.alias && nameList.push(...toArray(commandToSave.alias));
|
|
282
261
|
for (const name2 of nameList) {
|
|
283
|
-
if (
|
|
262
|
+
if (__privateGet(this, _usedNames).includes(name2)) {
|
|
284
263
|
throw new CommandExistsError(name2);
|
|
285
264
|
}
|
|
286
265
|
}
|
|
266
|
+
if (nameList.includes(SingleCommand)) {
|
|
267
|
+
__privateSet(this, _hasSingleCommand, true);
|
|
268
|
+
}
|
|
287
269
|
__privateGet(this, _commands)[name] = commandToSave;
|
|
270
|
+
__privateGet(this, _usedNames).push(commandToSave.name, ...toArray(commandToSave.alias) || []);
|
|
288
271
|
isCommandObject && handler && this.on(nameOrCommand.name, handler);
|
|
289
272
|
return this;
|
|
290
273
|
}
|
|
@@ -309,24 +292,24 @@ const _Clerc = class {
|
|
|
309
292
|
if (!__privateGet(this, _version)) {
|
|
310
293
|
throw new VersionNotSetError();
|
|
311
294
|
}
|
|
312
|
-
const name = resolveParametersBeforeFlag(argv
|
|
295
|
+
const name = resolveParametersBeforeFlag(argv);
|
|
313
296
|
const stringName = name.join(" ");
|
|
314
|
-
const getCommand = () =>
|
|
297
|
+
const getCommand = () => resolveCommand(__privateGet(this, _commands), name);
|
|
315
298
|
const mapErrors = [];
|
|
316
299
|
const getContext = () => {
|
|
317
300
|
mapErrors.length = 0;
|
|
318
301
|
const command = getCommand();
|
|
319
302
|
const isCommandResolved = !!command;
|
|
320
303
|
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
321
|
-
const { _: args, flags } = parsed;
|
|
322
|
-
let parameters =
|
|
304
|
+
const { _: args, flags, unknownFlags } = parsed;
|
|
305
|
+
let parameters = !isCommandResolved || command.name === SingleCommand ? args : args.slice(command.name.split(" ").length);
|
|
323
306
|
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
324
307
|
const hasEof = commandParameters.indexOf("--");
|
|
325
308
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
326
309
|
const mapping = /* @__PURE__ */ Object.create(null);
|
|
327
310
|
if (hasEof > -1 && eofParameters.length > 0) {
|
|
328
311
|
commandParameters = commandParameters.slice(0, hasEof);
|
|
329
|
-
const eofArguments =
|
|
312
|
+
const eofArguments = args["--"];
|
|
330
313
|
parameters = parameters.slice(0, -eofArguments.length || void 0);
|
|
331
314
|
mapErrors.push(mapParametersToArguments(
|
|
332
315
|
mapping,
|
|
@@ -345,15 +328,15 @@ const _Clerc = class {
|
|
|
345
328
|
parameters
|
|
346
329
|
));
|
|
347
330
|
}
|
|
348
|
-
const mergedFlags = { ...
|
|
331
|
+
const mergedFlags = { ...flags, ...unknownFlags };
|
|
349
332
|
const context = {
|
|
350
333
|
name: command == null ? void 0 : command.name,
|
|
351
334
|
resolved: isCommandResolved,
|
|
352
|
-
|
|
335
|
+
hasSingleCommand: __privateGet(this, _hasSingleCommand),
|
|
353
336
|
raw: { ...parsed, parameters, mergedFlags },
|
|
354
337
|
parameters: mapping,
|
|
355
338
|
flags,
|
|
356
|
-
unknownFlags
|
|
339
|
+
unknownFlags,
|
|
357
340
|
cli: this
|
|
358
341
|
};
|
|
359
342
|
return context;
|
|
@@ -390,18 +373,12 @@ _version = new WeakMap();
|
|
|
390
373
|
_inspectors = new WeakMap();
|
|
391
374
|
_commands = new WeakMap();
|
|
392
375
|
_commandEmitter = new WeakMap();
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
return __privateGet(this, _commands)[SingleCommand] !== void 0;
|
|
396
|
-
};
|
|
397
|
-
_hasCommands = new WeakSet();
|
|
398
|
-
hasCommands_get = function() {
|
|
399
|
-
return Object.keys(__privateGet(this, _commands)).length > 0;
|
|
400
|
-
};
|
|
376
|
+
_usedNames = new WeakMap();
|
|
377
|
+
_hasSingleCommand = new WeakMap();
|
|
401
378
|
|
|
402
379
|
const definePlugin = (p) => p;
|
|
403
380
|
const defineHandler = (_cli, _key, handler) => handler;
|
|
404
381
|
const defineInspector = (_cli, inspector) => inspector;
|
|
405
382
|
const defineCommand = (command) => command;
|
|
406
383
|
|
|
407
|
-
export { Clerc, CommandExistsError, CommandNameConflictError,
|
|
384
|
+
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.20.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.20.0"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "puild",
|