@clerc/core 0.21.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 +29 -35
- package/dist/index.js +56 -49
- package/dist/index.mjs +56 -49
- 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 |
|
|
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,10 +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 |
|
|
123
|
+
called?: string | RootType;
|
|
124
124
|
resolved: N extends keyof C ? true : boolean;
|
|
125
|
-
|
|
126
|
-
|
|
125
|
+
hasRootOrAlias: boolean;
|
|
126
|
+
hasRoot: boolean;
|
|
127
127
|
raw: Raw<C, N>;
|
|
128
128
|
parameters: TransformParameters<C, N>;
|
|
129
129
|
unknownFlags: ParsedFlags["unknownFlags"];
|
|
@@ -148,8 +148,8 @@ interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
|
|
|
148
148
|
setup: (cli: T) => U;
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
declare const
|
|
152
|
-
type
|
|
151
|
+
declare const Root: unique symbol;
|
|
152
|
+
type RootType = typeof Root;
|
|
153
153
|
declare class Clerc<C extends CommandRecord = {}> {
|
|
154
154
|
#private;
|
|
155
155
|
private constructor();
|
|
@@ -220,7 +220,7 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
220
220
|
* @example
|
|
221
221
|
* ```ts
|
|
222
222
|
* Clerc.create()
|
|
223
|
-
* .command("", "
|
|
223
|
+
* .command("", "root", {
|
|
224
224
|
* flags: {
|
|
225
225
|
* foo: {
|
|
226
226
|
* alias: "f",
|
|
@@ -230,8 +230,8 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
230
230
|
* })
|
|
231
231
|
* ```
|
|
232
232
|
*/
|
|
233
|
-
command<N extends string |
|
|
234
|
-
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>>>;
|
|
235
235
|
/**
|
|
236
236
|
* Register a handler
|
|
237
237
|
* @param name
|
|
@@ -288,15 +288,15 @@ declare class Clerc<C extends CommandRecord = {}> {
|
|
|
288
288
|
declare const definePlugin: <T extends Clerc<{}>, U extends Clerc<{}>>(p: Plugin<T, U>) => Plugin<T, U>;
|
|
289
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>;
|
|
290
290
|
declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspector<C["_commands"]>) => Inspector<C["_commands"]>;
|
|
291
|
-
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>>;
|
|
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,25 +316,19 @@ 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
|
-
declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof
|
|
324
|
-
declare function resolveCommand(commands: CommandRecord, name: string | string[] |
|
|
325
|
-
declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof
|
|
326
|
-
declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof
|
|
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>>>[];
|
|
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,
|
|
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
|
|
|
@@ -62,9 +62,9 @@ function setCommand(commandsMap, commands, command) {
|
|
|
62
62
|
}
|
|
63
63
|
function resolveFlattenCommands(commands) {
|
|
64
64
|
const commandsMap = /* @__PURE__ */ new Map();
|
|
65
|
-
if (commands[
|
|
66
|
-
commandsMap.set(
|
|
67
|
-
setCommand(commandsMap, commands, commands[
|
|
65
|
+
if (commands[Root]) {
|
|
66
|
+
commandsMap.set(Root, commands[Root]);
|
|
67
|
+
setCommand(commandsMap, commands, commands[Root]);
|
|
68
68
|
}
|
|
69
69
|
for (const command of Object.values(commands)) {
|
|
70
70
|
setCommand(commandsMap, commands, command);
|
|
@@ -73,20 +73,20 @@ function resolveFlattenCommands(commands) {
|
|
|
73
73
|
return commandsMap;
|
|
74
74
|
}
|
|
75
75
|
function resolveCommand(commands, name) {
|
|
76
|
-
if (name ===
|
|
77
|
-
return commands[
|
|
76
|
+
if (name === Root) {
|
|
77
|
+
return commands[Root];
|
|
78
78
|
}
|
|
79
79
|
const nameArr = toArray(name);
|
|
80
80
|
const commandsMap = resolveFlattenCommands(commands);
|
|
81
81
|
let current;
|
|
82
82
|
let currentName;
|
|
83
83
|
commandsMap.forEach((v, k) => {
|
|
84
|
-
if (k ===
|
|
85
|
-
current = commandsMap.get(
|
|
86
|
-
currentName =
|
|
84
|
+
if (k === Root) {
|
|
85
|
+
current = commandsMap.get(Root);
|
|
86
|
+
currentName = Root;
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
89
|
-
if (arrayStartsWith(nameArr, k) && (!currentName || currentName ===
|
|
89
|
+
if (arrayStartsWith(nameArr, k) && (!currentName || currentName === Root || k.length > currentName.length)) {
|
|
90
90
|
current = v;
|
|
91
91
|
currentName = k;
|
|
92
92
|
}
|
|
@@ -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) {
|
|
@@ -211,19 +220,19 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
211
220
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
212
221
|
return value;
|
|
213
222
|
};
|
|
214
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames,
|
|
215
|
-
const
|
|
223
|
+
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasRootOrAlias, hasRootOrAlias_get, _hasRoot, hasRoot_get;
|
|
224
|
+
const Root = Symbol("Root");
|
|
216
225
|
const _Clerc = class {
|
|
217
226
|
constructor(name, description, version) {
|
|
218
|
-
__privateAdd(this,
|
|
219
|
-
__privateAdd(this,
|
|
227
|
+
__privateAdd(this, _hasRootOrAlias);
|
|
228
|
+
__privateAdd(this, _hasRoot);
|
|
220
229
|
__privateAdd(this, _name, "");
|
|
221
230
|
__privateAdd(this, _description, "");
|
|
222
231
|
__privateAdd(this, _version, "");
|
|
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));
|
|
@@ -259,12 +268,9 @@ const _Clerc = class {
|
|
|
259
268
|
return this;
|
|
260
269
|
}
|
|
261
270
|
command(nameOrCommand, description, options = {}) {
|
|
262
|
-
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 ===
|
|
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
|
}
|
|
@@ -312,7 +319,7 @@ const _Clerc = class {
|
|
|
312
319
|
const isCommandResolved = !!command;
|
|
313
320
|
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
314
321
|
const { _: args, flags, unknownFlags } = parsed;
|
|
315
|
-
let parameters = !isCommandResolved || command.name ===
|
|
322
|
+
let parameters = !isCommandResolved || command.name === Root ? args : args.slice(command.name.split(" ").length);
|
|
316
323
|
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
317
324
|
const hasEof = commandParameters.indexOf("--");
|
|
318
325
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
@@ -341,10 +348,10 @@ 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 ?
|
|
351
|
+
called: name.length === 0 && (command == null ? void 0 : command.name) ? Root : stringName,
|
|
345
352
|
resolved: isCommandResolved,
|
|
346
|
-
|
|
347
|
-
|
|
353
|
+
hasRootOrAlias: __privateGet(this, _hasRootOrAlias, hasRootOrAlias_get),
|
|
354
|
+
hasRoot: __privateGet(this, _hasRoot, hasRoot_get),
|
|
348
355
|
raw: { ...parsed, parameters, mergedFlags },
|
|
349
356
|
parameters: mapping,
|
|
350
357
|
flags,
|
|
@@ -386,13 +393,13 @@ _inspectors = new WeakMap();
|
|
|
386
393
|
_commands = new WeakMap();
|
|
387
394
|
_commandEmitter = new WeakMap();
|
|
388
395
|
_usedNames = new WeakMap();
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
return __privateGet(this, _usedNames).
|
|
396
|
+
_hasRootOrAlias = new WeakSet();
|
|
397
|
+
hasRootOrAlias_get = function() {
|
|
398
|
+
return __privateGet(this, _usedNames).has(Root);
|
|
392
399
|
};
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
return
|
|
400
|
+
_hasRoot = new WeakSet();
|
|
401
|
+
hasRoot_get = function() {
|
|
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,
|
|
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
|
|
|
@@ -62,9 +62,9 @@ function setCommand(commandsMap, commands, command) {
|
|
|
62
62
|
}
|
|
63
63
|
function resolveFlattenCommands(commands) {
|
|
64
64
|
const commandsMap = /* @__PURE__ */ new Map();
|
|
65
|
-
if (commands[
|
|
66
|
-
commandsMap.set(
|
|
67
|
-
setCommand(commandsMap, commands, commands[
|
|
65
|
+
if (commands[Root]) {
|
|
66
|
+
commandsMap.set(Root, commands[Root]);
|
|
67
|
+
setCommand(commandsMap, commands, commands[Root]);
|
|
68
68
|
}
|
|
69
69
|
for (const command of Object.values(commands)) {
|
|
70
70
|
setCommand(commandsMap, commands, command);
|
|
@@ -73,20 +73,20 @@ function resolveFlattenCommands(commands) {
|
|
|
73
73
|
return commandsMap;
|
|
74
74
|
}
|
|
75
75
|
function resolveCommand(commands, name) {
|
|
76
|
-
if (name ===
|
|
77
|
-
return commands[
|
|
76
|
+
if (name === Root) {
|
|
77
|
+
return commands[Root];
|
|
78
78
|
}
|
|
79
79
|
const nameArr = toArray(name);
|
|
80
80
|
const commandsMap = resolveFlattenCommands(commands);
|
|
81
81
|
let current;
|
|
82
82
|
let currentName;
|
|
83
83
|
commandsMap.forEach((v, k) => {
|
|
84
|
-
if (k ===
|
|
85
|
-
current = commandsMap.get(
|
|
86
|
-
currentName =
|
|
84
|
+
if (k === Root) {
|
|
85
|
+
current = commandsMap.get(Root);
|
|
86
|
+
currentName = Root;
|
|
87
87
|
return;
|
|
88
88
|
}
|
|
89
|
-
if (arrayStartsWith(nameArr, k) && (!currentName || currentName ===
|
|
89
|
+
if (arrayStartsWith(nameArr, k) && (!currentName || currentName === Root || k.length > currentName.length)) {
|
|
90
90
|
current = v;
|
|
91
91
|
currentName = k;
|
|
92
92
|
}
|
|
@@ -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) {
|
|
@@ -211,19 +220,19 @@ var __privateSet = (obj, member, value, setter) => {
|
|
|
211
220
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
212
221
|
return value;
|
|
213
222
|
};
|
|
214
|
-
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames,
|
|
215
|
-
const
|
|
223
|
+
var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasRootOrAlias, hasRootOrAlias_get, _hasRoot, hasRoot_get;
|
|
224
|
+
const Root = Symbol("Root");
|
|
216
225
|
const _Clerc = class {
|
|
217
226
|
constructor(name, description, version) {
|
|
218
|
-
__privateAdd(this,
|
|
219
|
-
__privateAdd(this,
|
|
227
|
+
__privateAdd(this, _hasRootOrAlias);
|
|
228
|
+
__privateAdd(this, _hasRoot);
|
|
220
229
|
__privateAdd(this, _name, "");
|
|
221
230
|
__privateAdd(this, _description, "");
|
|
222
231
|
__privateAdd(this, _version, "");
|
|
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));
|
|
@@ -259,12 +268,9 @@ const _Clerc = class {
|
|
|
259
268
|
return this;
|
|
260
269
|
}
|
|
261
270
|
command(nameOrCommand, description, options = {}) {
|
|
262
|
-
const checkIsCommandObject = (nameOrCommand2) => !(typeof nameOrCommand2 === "string" || nameOrCommand2 ===
|
|
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
|
}
|
|
@@ -312,7 +319,7 @@ const _Clerc = class {
|
|
|
312
319
|
const isCommandResolved = !!command;
|
|
313
320
|
const parsed = typeFlag((command == null ? void 0 : command.flags) || {}, [...argv]);
|
|
314
321
|
const { _: args, flags, unknownFlags } = parsed;
|
|
315
|
-
let parameters = !isCommandResolved || command.name ===
|
|
322
|
+
let parameters = !isCommandResolved || command.name === Root ? args : args.slice(command.name.split(" ").length);
|
|
316
323
|
let commandParameters = (command == null ? void 0 : command.parameters) || [];
|
|
317
324
|
const hasEof = commandParameters.indexOf("--");
|
|
318
325
|
const eofParameters = commandParameters.slice(hasEof + 1) || [];
|
|
@@ -341,10 +348,10 @@ 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 ?
|
|
351
|
+
called: name.length === 0 && (command == null ? void 0 : command.name) ? Root : stringName,
|
|
345
352
|
resolved: isCommandResolved,
|
|
346
|
-
|
|
347
|
-
|
|
353
|
+
hasRootOrAlias: __privateGet(this, _hasRootOrAlias, hasRootOrAlias_get),
|
|
354
|
+
hasRoot: __privateGet(this, _hasRoot, hasRoot_get),
|
|
348
355
|
raw: { ...parsed, parameters, mergedFlags },
|
|
349
356
|
parameters: mapping,
|
|
350
357
|
flags,
|
|
@@ -386,13 +393,13 @@ _inspectors = new WeakMap();
|
|
|
386
393
|
_commands = new WeakMap();
|
|
387
394
|
_commandEmitter = new WeakMap();
|
|
388
395
|
_usedNames = new WeakMap();
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
return __privateGet(this, _usedNames).
|
|
396
|
+
_hasRootOrAlias = new WeakSet();
|
|
397
|
+
hasRootOrAlias_get = function() {
|
|
398
|
+
return __privateGet(this, _usedNames).has(Root);
|
|
392
399
|
};
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
return
|
|
400
|
+
_hasRoot = new WeakSet();
|
|
401
|
+
hasRoot_get = function() {
|
|
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,
|
|
410
|
+
export { Clerc, CommandExistsError, CommandNameConflictError, DescriptionNotSetError, InvalidCommandNameError, NameNotSetError, NoCommandGivenError, NoSuchCommandError, Root, VersionNotSetError, compose, defineCommand, defineHandler, defineInspector, definePlugin, formatCommandName, isInvalidName, resolveArgv, resolveCommand, resolveFlattenCommands, resolveParametersBeforeFlag, resolveRootCommands, resolveSubcommandsByParent, withBrackets };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.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.
|
|
53
|
+
"@clerc/utils": "0.22.1"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build": "puild",
|