@clerc/core 0.22.0 → 0.22.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +10 -16
- package/dist/index.js +35 -28
- package/dist/index.mjs +35 -28
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -291,12 +291,12 @@ declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspect
|
|
|
291
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>>;
|
|
292
292
|
|
|
293
293
|
declare class CommandExistsError extends Error {
|
|
294
|
-
|
|
295
|
-
constructor(
|
|
294
|
+
commandName: string;
|
|
295
|
+
constructor(commandName: string);
|
|
296
296
|
}
|
|
297
297
|
declare class NoSuchCommandError extends Error {
|
|
298
|
-
|
|
299
|
-
constructor(
|
|
298
|
+
commandName: string;
|
|
299
|
+
constructor(commandName: string);
|
|
300
300
|
}
|
|
301
301
|
declare class NoCommandGivenError extends Error {
|
|
302
302
|
constructor();
|
|
@@ -316,8 +316,8 @@ declare class VersionNotSetError extends Error {
|
|
|
316
316
|
constructor();
|
|
317
317
|
}
|
|
318
318
|
declare class InvalidCommandNameError extends Error {
|
|
319
|
-
|
|
320
|
-
constructor(
|
|
319
|
+
commandName: string;
|
|
320
|
+
constructor(commandName: string);
|
|
321
321
|
}
|
|
322
322
|
|
|
323
323
|
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof Root, CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof Root>, _clerc_utils.Dict<FlagOptions>>>>;
|
|
@@ -327,14 +327,8 @@ declare const resolveRootCommands: (commands: CommandRecord) => Command<string,
|
|
|
327
327
|
declare function resolveParametersBeforeFlag(argv: string[]): string[];
|
|
328
328
|
declare const resolveArgv: () => string[];
|
|
329
329
|
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;
|
|
330
|
+
declare const isInvalidName: (name: CommandType) => boolean;
|
|
331
|
+
declare const withBrackets: (s: string, isOptional?: boolean) => string;
|
|
332
|
+
declare const formatCommandName: (name: string | string[] | RootType) => string;
|
|
339
333
|
|
|
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,
|
|
334
|
+
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, 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,11 +395,11 @@ _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.hasOwn(this._commands, Root);
|
|
396
403
|
};
|
|
397
404
|
|
|
398
405
|
const definePlugin = (p) => p;
|
|
@@ -400,4 +407,4 @@ const defineHandler = (_cli, _key, handler) => handler;
|
|
|
400
407
|
const defineInspector = (_cli, inspector) => inspector;
|
|
401
408
|
const defineCommand = (command) => command;
|
|
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,11 +395,11 @@ _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.hasOwn(this._commands, Root);
|
|
396
403
|
};
|
|
397
404
|
|
|
398
405
|
const definePlugin = (p) => p;
|
|
@@ -400,4 +407,4 @@ const defineHandler = (_cli, _key, handler) => handler;
|
|
|
400
407
|
const defineInspector = (_cli, inspector) => inspector;
|
|
401
408
|
const defineCommand = (command) => command;
|
|
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.22.
|
|
3
|
+
"version": "0.22.1",
|
|
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.22.
|
|
53
|
+
"@clerc/utils": "0.22.1"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "puild",
|