@clerc/core 0.20.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 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,7 +120,9 @@ 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;
125
+ hasSingleCommandOrAlias: boolean;
124
126
  hasSingleCommand: boolean;
125
127
  raw: Raw<C, N>;
126
128
  parameters: TransformParameters<C, N>;
@@ -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
@@ -289,15 +291,19 @@ declare const defineInspector: <C extends Clerc<{}>>(_cli: C, inspector: Inspect
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
293
  declare class CommandExistsError extends Error {
294
+ name: string;
292
295
  constructor(name: string);
293
296
  }
294
297
  declare class NoSuchCommandError extends Error {
298
+ name: string;
295
299
  constructor(name: string);
296
300
  }
297
301
  declare class NoCommandGivenError extends Error {
298
302
  constructor();
299
303
  }
300
304
  declare class CommandNameConflictError extends Error {
305
+ n1: string;
306
+ n2: string;
301
307
  constructor(n1: string, n2: string);
302
308
  }
303
309
  declare class NameNotSetError extends Error {
@@ -310,13 +316,14 @@ declare class VersionNotSetError extends Error {
310
316
  constructor();
311
317
  }
312
318
  declare class InvalidCommandNameError extends Error {
319
+ name: string;
313
320
  constructor(name: string);
314
321
  }
315
322
 
316
- declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof SingleCommand, CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>>;
323
+ declare function resolveFlattenCommands(commands: CommandRecord): Map<string[] | typeof SingleCommand, CommandAlias<string, CommandOptions<string[], _clerc_utils.MaybeArray<string | typeof SingleCommand>, _clerc_utils.Dict<FlagOptions>>>>;
317
324
  declare function resolveCommand(commands: CommandRecord, name: string | string[] | SingleCommandType): Command<string | SingleCommandType> | undefined;
318
- declare function resolveSubcommandsByParent(commands: CommandRecord, parent: string | string[], depth?: number): Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
319
- declare const resolveRootCommands: (commands: CommandRecord) => Command<string, CommandOptions<string[], _clerc_utils.MaybeArray<string>, _clerc_utils.Dict<FlagOptions>>>[];
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>>>[];
320
327
  declare function resolveParametersBeforeFlag(argv: string[]): string[];
321
328
  declare const resolveArgv: () => string[];
322
329
  declare function compose(inspectors: Inspector[]): (getCtx: () => InspectorContext) => void;
package/dist/index.js CHANGED
@@ -6,11 +6,13 @@ import { isNode, isDeno } from 'is-platform';
6
6
  class CommandExistsError extends Error {
7
7
  constructor(name) {
8
8
  super(`Command "${name}" exists.`);
9
+ this.name = name;
9
10
  }
10
11
  }
11
12
  class NoSuchCommandError extends Error {
12
13
  constructor(name) {
13
14
  super(`No such command: ${name}`);
15
+ this.name = name;
14
16
  }
15
17
  }
16
18
  class NoCommandGivenError extends Error {
@@ -21,6 +23,8 @@ class NoCommandGivenError extends Error {
21
23
  class CommandNameConflictError extends Error {
22
24
  constructor(n1, n2) {
23
25
  super(`Command name ${n1} conflicts with ${n2}. Maybe caused by alias.`);
26
+ this.n1 = n1;
27
+ this.n2 = n2;
24
28
  }
25
29
  }
26
30
  class NameNotSetError extends Error {
@@ -41,24 +45,29 @@ class VersionNotSetError extends Error {
41
45
  class InvalidCommandNameError extends Error {
42
46
  constructor(name) {
43
47
  super(`Bad name format: ${name}`);
48
+ this.name = name;
44
49
  }
45
50
  }
46
51
 
52
+ function setCommand(commandsMap, commands, command) {
53
+ if (command.alias) {
54
+ const aliases = toArray(command.alias);
55
+ for (const alias of aliases) {
56
+ if (alias in commands) {
57
+ throw new CommandNameConflictError(commands[alias].name, command.name);
58
+ }
59
+ commandsMap.set(typeof alias === "symbol" ? alias : alias.split(" "), { ...command, __isAlias: true });
60
+ }
61
+ }
62
+ }
47
63
  function resolveFlattenCommands(commands) {
48
64
  const commandsMap = /* @__PURE__ */ new Map();
49
65
  if (commands[SingleCommand]) {
50
66
  commandsMap.set(SingleCommand, commands[SingleCommand]);
67
+ setCommand(commandsMap, commands, commands[SingleCommand]);
51
68
  }
52
69
  for (const command of Object.values(commands)) {
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(alias.split(" "), { ...command, __isAlias: true });
60
- }
61
- }
70
+ setCommand(commandsMap, commands, command);
62
71
  commandsMap.set(command.name.split(" "), command);
63
72
  }
64
73
  return commandsMap;
@@ -202,10 +211,12 @@ var __privateSet = (obj, member, value, setter) => {
202
211
  setter ? setter.call(obj, value) : member.set(obj, value);
203
212
  return value;
204
213
  };
205
- var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasSingleCommand;
214
+ var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasSingleCommandOrAlias, hasSingleCommandOrAlias_get, _hasSingleCommand, hasSingleCommand_get;
206
215
  const SingleCommand = Symbol("SingleCommand");
207
216
  const _Clerc = class {
208
- constructor() {
217
+ constructor(name, description, version) {
218
+ __privateAdd(this, _hasSingleCommandOrAlias);
219
+ __privateAdd(this, _hasSingleCommand);
209
220
  __privateAdd(this, _name, "");
210
221
  __privateAdd(this, _description, "");
211
222
  __privateAdd(this, _version, "");
@@ -213,7 +224,9 @@ const _Clerc = class {
213
224
  __privateAdd(this, _commands, {});
214
225
  __privateAdd(this, _commandEmitter, new LiteEmit());
215
226
  __privateAdd(this, _usedNames, []);
216
- __privateAdd(this, _hasSingleCommand, false);
227
+ __privateSet(this, _name, name || __privateGet(this, _name));
228
+ __privateSet(this, _description, description || __privateGet(this, _description));
229
+ __privateSet(this, _version, version || __privateGet(this, _version));
217
230
  }
218
231
  get _name() {
219
232
  return __privateGet(this, _name);
@@ -230,8 +243,8 @@ const _Clerc = class {
230
243
  get _commands() {
231
244
  return __privateGet(this, _commands);
232
245
  }
233
- static create() {
234
- return new _Clerc();
246
+ static create(name, description, version) {
247
+ return new _Clerc(name, description, version);
235
248
  }
236
249
  name(name) {
237
250
  __privateSet(this, _name, name);
@@ -263,9 +276,6 @@ const _Clerc = class {
263
276
  throw new CommandExistsError(name2);
264
277
  }
265
278
  }
266
- if (nameList.includes(SingleCommand)) {
267
- __privateSet(this, _hasSingleCommand, true);
268
- }
269
279
  __privateGet(this, _commands)[name] = commandToSave;
270
280
  __privateGet(this, _usedNames).push(commandToSave.name, ...toArray(commandToSave.alias) || []);
271
281
  isCommandObject && handler && this.on(nameOrCommand.name, handler);
@@ -331,8 +341,10 @@ const _Clerc = class {
331
341
  const mergedFlags = { ...flags, ...unknownFlags };
332
342
  const context = {
333
343
  name: command == null ? void 0 : command.name,
344
+ called: name.length === 0 ? SingleCommand : stringName,
334
345
  resolved: isCommandResolved,
335
- hasSingleCommand: __privateGet(this, _hasSingleCommand),
346
+ hasSingleCommandOrAlias: __privateGet(this, _hasSingleCommandOrAlias, hasSingleCommandOrAlias_get),
347
+ hasSingleCommand: __privateGet(this, _hasSingleCommand, hasSingleCommand_get),
336
348
  raw: { ...parsed, parameters, mergedFlags },
337
349
  parameters: mapping,
338
350
  flags,
@@ -374,7 +386,14 @@ _inspectors = new WeakMap();
374
386
  _commands = new WeakMap();
375
387
  _commandEmitter = new WeakMap();
376
388
  _usedNames = new WeakMap();
377
- _hasSingleCommand = new WeakMap();
389
+ _hasSingleCommandOrAlias = new WeakSet();
390
+ hasSingleCommandOrAlias_get = function() {
391
+ return __privateGet(this, _usedNames).includes(SingleCommand);
392
+ };
393
+ _hasSingleCommand = new WeakSet();
394
+ hasSingleCommand_get = function() {
395
+ return !!__privateGet(this, _commands)[SingleCommand];
396
+ };
378
397
 
379
398
  const definePlugin = (p) => p;
380
399
  const defineHandler = (_cli, _key, handler) => handler;
package/dist/index.mjs CHANGED
@@ -6,11 +6,13 @@ import { isNode, isDeno } from 'is-platform';
6
6
  class CommandExistsError extends Error {
7
7
  constructor(name) {
8
8
  super(`Command "${name}" exists.`);
9
+ this.name = name;
9
10
  }
10
11
  }
11
12
  class NoSuchCommandError extends Error {
12
13
  constructor(name) {
13
14
  super(`No such command: ${name}`);
15
+ this.name = name;
14
16
  }
15
17
  }
16
18
  class NoCommandGivenError extends Error {
@@ -21,6 +23,8 @@ class NoCommandGivenError extends Error {
21
23
  class CommandNameConflictError extends Error {
22
24
  constructor(n1, n2) {
23
25
  super(`Command name ${n1} conflicts with ${n2}. Maybe caused by alias.`);
26
+ this.n1 = n1;
27
+ this.n2 = n2;
24
28
  }
25
29
  }
26
30
  class NameNotSetError extends Error {
@@ -41,24 +45,29 @@ class VersionNotSetError extends Error {
41
45
  class InvalidCommandNameError extends Error {
42
46
  constructor(name) {
43
47
  super(`Bad name format: ${name}`);
48
+ this.name = name;
44
49
  }
45
50
  }
46
51
 
52
+ function setCommand(commandsMap, commands, command) {
53
+ if (command.alias) {
54
+ const aliases = toArray(command.alias);
55
+ for (const alias of aliases) {
56
+ if (alias in commands) {
57
+ throw new CommandNameConflictError(commands[alias].name, command.name);
58
+ }
59
+ commandsMap.set(typeof alias === "symbol" ? alias : alias.split(" "), { ...command, __isAlias: true });
60
+ }
61
+ }
62
+ }
47
63
  function resolveFlattenCommands(commands) {
48
64
  const commandsMap = /* @__PURE__ */ new Map();
49
65
  if (commands[SingleCommand]) {
50
66
  commandsMap.set(SingleCommand, commands[SingleCommand]);
67
+ setCommand(commandsMap, commands, commands[SingleCommand]);
51
68
  }
52
69
  for (const command of Object.values(commands)) {
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(alias.split(" "), { ...command, __isAlias: true });
60
- }
61
- }
70
+ setCommand(commandsMap, commands, command);
62
71
  commandsMap.set(command.name.split(" "), command);
63
72
  }
64
73
  return commandsMap;
@@ -202,10 +211,12 @@ var __privateSet = (obj, member, value, setter) => {
202
211
  setter ? setter.call(obj, value) : member.set(obj, value);
203
212
  return value;
204
213
  };
205
- var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasSingleCommand;
214
+ var _name, _description, _version, _inspectors, _commands, _commandEmitter, _usedNames, _hasSingleCommandOrAlias, hasSingleCommandOrAlias_get, _hasSingleCommand, hasSingleCommand_get;
206
215
  const SingleCommand = Symbol("SingleCommand");
207
216
  const _Clerc = class {
208
- constructor() {
217
+ constructor(name, description, version) {
218
+ __privateAdd(this, _hasSingleCommandOrAlias);
219
+ __privateAdd(this, _hasSingleCommand);
209
220
  __privateAdd(this, _name, "");
210
221
  __privateAdd(this, _description, "");
211
222
  __privateAdd(this, _version, "");
@@ -213,7 +224,9 @@ const _Clerc = class {
213
224
  __privateAdd(this, _commands, {});
214
225
  __privateAdd(this, _commandEmitter, new LiteEmit());
215
226
  __privateAdd(this, _usedNames, []);
216
- __privateAdd(this, _hasSingleCommand, false);
227
+ __privateSet(this, _name, name || __privateGet(this, _name));
228
+ __privateSet(this, _description, description || __privateGet(this, _description));
229
+ __privateSet(this, _version, version || __privateGet(this, _version));
217
230
  }
218
231
  get _name() {
219
232
  return __privateGet(this, _name);
@@ -230,8 +243,8 @@ const _Clerc = class {
230
243
  get _commands() {
231
244
  return __privateGet(this, _commands);
232
245
  }
233
- static create() {
234
- return new _Clerc();
246
+ static create(name, description, version) {
247
+ return new _Clerc(name, description, version);
235
248
  }
236
249
  name(name) {
237
250
  __privateSet(this, _name, name);
@@ -263,9 +276,6 @@ const _Clerc = class {
263
276
  throw new CommandExistsError(name2);
264
277
  }
265
278
  }
266
- if (nameList.includes(SingleCommand)) {
267
- __privateSet(this, _hasSingleCommand, true);
268
- }
269
279
  __privateGet(this, _commands)[name] = commandToSave;
270
280
  __privateGet(this, _usedNames).push(commandToSave.name, ...toArray(commandToSave.alias) || []);
271
281
  isCommandObject && handler && this.on(nameOrCommand.name, handler);
@@ -331,8 +341,10 @@ const _Clerc = class {
331
341
  const mergedFlags = { ...flags, ...unknownFlags };
332
342
  const context = {
333
343
  name: command == null ? void 0 : command.name,
344
+ called: name.length === 0 ? SingleCommand : stringName,
334
345
  resolved: isCommandResolved,
335
- hasSingleCommand: __privateGet(this, _hasSingleCommand),
346
+ hasSingleCommandOrAlias: __privateGet(this, _hasSingleCommandOrAlias, hasSingleCommandOrAlias_get),
347
+ hasSingleCommand: __privateGet(this, _hasSingleCommand, hasSingleCommand_get),
336
348
  raw: { ...parsed, parameters, mergedFlags },
337
349
  parameters: mapping,
338
350
  flags,
@@ -374,7 +386,14 @@ _inspectors = new WeakMap();
374
386
  _commands = new WeakMap();
375
387
  _commandEmitter = new WeakMap();
376
388
  _usedNames = new WeakMap();
377
- _hasSingleCommand = new WeakMap();
389
+ _hasSingleCommandOrAlias = new WeakSet();
390
+ hasSingleCommandOrAlias_get = function() {
391
+ return __privateGet(this, _usedNames).includes(SingleCommand);
392
+ };
393
+ _hasSingleCommand = new WeakSet();
394
+ hasSingleCommand_get = function() {
395
+ return !!__privateGet(this, _commands)[SingleCommand];
396
+ };
378
397
 
379
398
  const definePlugin = (p) => p;
380
399
  const defineHandler = (_cli, _key, handler) => handler;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/core",
3
- "version": "0.20.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.20.0"
53
+ "@clerc/utils": "0.21.0"
54
54
  },
55
55
  "scripts": {
56
56
  "build": "puild",