@clerc/core 1.0.0-beta.1 → 1.0.0-beta.11
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 +94 -24
- package/dist/index.js +49 -21
- package/package.json +4 -8
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
import { ErrorHandler as ErrorHandler$1 } from "lite-emit";
|
|
2
2
|
|
|
3
|
+
//#region rolldown:runtime
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region ../parser/src/errors.d.ts
|
|
6
|
+
declare class InvalidSchemaError extends Error {
|
|
7
|
+
constructor(message: string);
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region ../parser/src/flag-types.d.ts
|
|
11
|
+
/**
|
|
12
|
+
* Creates a Choices type function that validates the input against allowed values.
|
|
13
|
+
* The display name will be formatted as "value1 | value2 | ..." for help output.
|
|
14
|
+
*
|
|
15
|
+
* @param values - Array of allowed string values
|
|
16
|
+
* @returns A FlagTypeFunction that validates and returns the input value
|
|
17
|
+
* @throws {Error} If the value is not in the allowed values list
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const format = Choices(['json', 'yaml', 'xml']);
|
|
22
|
+
* // Help output will show: json | yaml | xml
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function Choices<T extends string>(...values: T[]): FlagTypeFunction<T>;
|
|
26
|
+
//#endregion
|
|
3
27
|
//#region ../utils/src/types/type-fest.d.ts
|
|
4
28
|
type LiteralUnion<LiteralType, BaseType> = LiteralType | (BaseType & Record<never, never>);
|
|
5
29
|
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
@@ -10,11 +34,13 @@ type UnknownArray = readonly unknown[];
|
|
|
10
34
|
type MapsSetsOrArrays = ReadonlyMap<unknown, unknown> | WeakMap<WeakKey, unknown> | ReadonlySet<unknown> | WeakSet<WeakKey> | UnknownArray;
|
|
11
35
|
type ConditionalDeepPrettify<T, E = never, I$1 = unknown> = T extends E ? T : T extends I$1 ? { [TypeKey in keyof T]: ConditionalDeepPrettify<T[TypeKey], E, I$1> } : T;
|
|
12
36
|
type DeepPrettify<T, E = never> = ConditionalDeepPrettify<T, E | NonRecursiveType | MapsSetsOrArrays, object>;
|
|
37
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
13
38
|
//#endregion
|
|
14
39
|
//#region ../utils/src/types/index.d.ts
|
|
15
40
|
type MaybeArray<T> = T | T[];
|
|
16
41
|
type PartialRequired<T, K$1 extends keyof T> = T & { [P in K$1]-?: T[P] };
|
|
17
42
|
type UnionToIntersection<U$1> = (U$1 extends any ? (k: U$1) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
43
|
+
type CamelCase<S extends string> = S extends `${infer Head} ${infer Tail}` ? `${Head}${Capitalize<CamelCase<Tail>>}` : S extends `${infer Head}-${infer Tail}` ? `${Head}${Capitalize<CamelCase<Tail>>}` : S;
|
|
18
44
|
//#endregion
|
|
19
45
|
//#region ../parser/src/types.d.ts
|
|
20
46
|
type FlagDefaultValue<T = unknown> = T | (() => T);
|
|
@@ -23,7 +49,13 @@ type FlagDefaultValue<T = unknown> = T | (() => T);
|
|
|
23
49
|
*
|
|
24
50
|
* @template T The target type.
|
|
25
51
|
*/
|
|
26
|
-
type FlagTypeFunction<T = unknown> = (value: string) => T
|
|
52
|
+
type FlagTypeFunction<T = unknown> = ((value: string) => T) & {
|
|
53
|
+
/**
|
|
54
|
+
* Optional display name for the type, useful in help output.
|
|
55
|
+
* If provided, this will be shown instead of the function name.
|
|
56
|
+
*/
|
|
57
|
+
displayName?: string;
|
|
58
|
+
};
|
|
27
59
|
/**
|
|
28
60
|
* A callback function to conditionally stop parsing.
|
|
29
61
|
* When it returns true, parsing stops and remaining arguments are preserved in `ignored`.
|
|
@@ -60,6 +92,28 @@ type FlagOptions = (BaseFlagOptions<BooleanConstructor> & {
|
|
|
60
92
|
});
|
|
61
93
|
type FlagDefinitionValue = FlagOptions | FlagType;
|
|
62
94
|
type FlagsDefinition = Record<string, FlagDefinitionValue>;
|
|
95
|
+
/**
|
|
96
|
+
* Configuration options for the parser.
|
|
97
|
+
*/
|
|
98
|
+
interface ParserOptions<T extends FlagsDefinition = {}> {
|
|
99
|
+
/**
|
|
100
|
+
* Detailed configuration for flags.
|
|
101
|
+
* Supports the full object syntax or a type constructor as a shorthand.
|
|
102
|
+
* The key is the flag name (e.g., "file" for "--file").
|
|
103
|
+
*/
|
|
104
|
+
flags?: T;
|
|
105
|
+
/**
|
|
106
|
+
* Delimiters to split flag names and values.
|
|
107
|
+
*
|
|
108
|
+
* @default ['=', ':']
|
|
109
|
+
*/
|
|
110
|
+
delimiters?: string[];
|
|
111
|
+
/**
|
|
112
|
+
* A callback function to conditionally stop parsing.
|
|
113
|
+
* When it returns true, parsing stops and remaining arguments are preserved in `ignored`.
|
|
114
|
+
*/
|
|
115
|
+
ignore?: IgnoreFunction;
|
|
116
|
+
}
|
|
63
117
|
type RawInputType = string | boolean;
|
|
64
118
|
interface ObjectInputType {
|
|
65
119
|
[key: string]: RawInputType | ObjectInputType;
|
|
@@ -88,7 +142,10 @@ interface ParsedResult<TFlags extends Record<string, any>> {
|
|
|
88
142
|
type InferFlagDefault<T extends FlagDefinitionValue, Fallback> = T extends {
|
|
89
143
|
default: FlagDefaultValue<infer DefaultType>;
|
|
90
144
|
} ? DefaultType : Fallback;
|
|
91
|
-
type
|
|
145
|
+
type IsTypeAny<T extends FlagDefinitionValue> = IsAny<T> extends true ? true : T extends {
|
|
146
|
+
type: infer Type;
|
|
147
|
+
} ? IsAny<Type> extends true ? true : false : false;
|
|
148
|
+
type _InferFlags<T extends FlagsDefinition> = { [K in keyof T]: IsTypeAny<T[K]> extends true ? any : T[K] extends readonly [BooleanConstructor] | {
|
|
92
149
|
type: readonly [BooleanConstructor];
|
|
93
150
|
} ? number : T[K] extends ObjectConstructor | {
|
|
94
151
|
type: ObjectConstructor;
|
|
@@ -110,18 +167,32 @@ declare const KNOWN_FLAG = "known-flag";
|
|
|
110
167
|
declare const UNKNOWN_FLAG = "unknown-flag";
|
|
111
168
|
declare const PARAMETER = "parameter";
|
|
112
169
|
//#endregion
|
|
170
|
+
//#region ../parser/src/parse.d.ts
|
|
171
|
+
declare const DOUBLE_DASH = "--";
|
|
172
|
+
type ParseFunction<T extends FlagsDefinition> = (args: string[]) => ParsedResult<InferFlags<T>>;
|
|
173
|
+
declare function createParser<T extends FlagsDefinition>(options?: ParserOptions<T>): {
|
|
174
|
+
parse: ParseFunction<T>;
|
|
175
|
+
};
|
|
176
|
+
declare const parse: <T extends FlagsDefinition>(args: string[], options?: ParserOptions<T>) => ParsedResult<InferFlags<T>>;
|
|
177
|
+
declare namespace index_d_exports {
|
|
178
|
+
export { BaseFlagOptions, Choices, DOUBLE_DASH, FlagDefaultValue, FlagDefinitionValue, FlagOptions, FlagType, FlagTypeFunction, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, RawInputType, UNKNOWN_FLAG, createParser, parse };
|
|
179
|
+
}
|
|
180
|
+
//#endregion
|
|
113
181
|
//#region src/types/clerc.d.ts
|
|
114
182
|
type ErrorHandler = (error: unknown) => void;
|
|
115
183
|
//#endregion
|
|
116
184
|
//#region src/types/flag.d.ts
|
|
117
|
-
|
|
185
|
+
interface FlagCustomOptions {}
|
|
186
|
+
type ClercFlagOptions = FlagOptions & FlagCustomOptions;
|
|
187
|
+
type ClercGlobalFlagDefinitionValue = ClercFlagOptions | FlagType;
|
|
188
|
+
type ClercFlagOptionsWithDescription = ClercFlagOptions & {
|
|
118
189
|
description: string;
|
|
119
190
|
};
|
|
120
|
-
type ClercFlagsDefinition = Record<string,
|
|
191
|
+
type ClercFlagsDefinition = Record<string, ClercFlagOptionsWithDescription>;
|
|
121
192
|
//#endregion
|
|
122
193
|
//#region src/types/parameters.d.ts
|
|
123
|
-
type InferParameter<T extends string> = T extends `<${infer Name extends string}...>` | `[${infer Name extends string}...]` ? Record<Name
|
|
124
|
-
type InferParameters<T extends string[]> = T extends (infer U extends string)[] ? Prettify<UnionToIntersection<InferParameter<U>>> : never;
|
|
194
|
+
type InferParameter<T extends string> = T extends `<${infer Name extends string}...>` | `[${infer Name extends string}...]` ? Record<CamelCase<Name>, string[]> : T extends `<${infer Name extends string}>` ? Record<CamelCase<Name>, string> : T extends `[${infer Name extends string}]` ? Record<CamelCase<Name>, string | undefined> : never;
|
|
195
|
+
type InferParameters<T extends readonly string[]> = T extends readonly (infer U extends string)[] ? Prettify<UnionToIntersection<InferParameter<U>>> : never;
|
|
125
196
|
//#endregion
|
|
126
197
|
//#region src/types/context.d.ts
|
|
127
198
|
type AddStringIndex<T> = T & Record<string, any>;
|
|
@@ -134,12 +205,12 @@ interface BaseContext<C extends Command = Command, GF extends ClercFlagsDefiniti
|
|
|
134
205
|
flags: InferFlagsWithGlobal<C, GF>;
|
|
135
206
|
ignored: string[];
|
|
136
207
|
rawParsed: ParsedResult<InferFlagsWithGlobal<C, GF>>;
|
|
137
|
-
|
|
208
|
+
missingParameters: boolean;
|
|
138
209
|
}
|
|
139
210
|
//#endregion
|
|
140
211
|
//#region src/types/command.d.ts
|
|
141
212
|
interface CommandCustomOptions {}
|
|
142
|
-
interface CommandOptions<Parameters extends string[] = string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandCustomOptions {
|
|
213
|
+
interface CommandOptions<Parameters extends readonly string[] = readonly string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandCustomOptions {
|
|
143
214
|
alias?: MaybeArray<string>;
|
|
144
215
|
parameters?: Parameters;
|
|
145
216
|
flags?: Flags;
|
|
@@ -148,11 +219,11 @@ interface CommandOptions<Parameters extends string[] = string[], Flags extends C
|
|
|
148
219
|
*/
|
|
149
220
|
ignore?: IgnoreFunction;
|
|
150
221
|
}
|
|
151
|
-
interface Command<Name$1 extends string = string, Parameters extends string[] = string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandOptions<Parameters, Flags> {
|
|
222
|
+
interface Command<Name$1 extends string = string, Parameters extends readonly string[] = readonly string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandOptions<Parameters, Flags> {
|
|
152
223
|
name: Name$1;
|
|
153
224
|
description: string;
|
|
154
225
|
}
|
|
155
|
-
type CommandWithHandler<Name$1 extends string = string, Parameters extends string[] = string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> = Command<Name$1, Parameters, Flags> & {
|
|
226
|
+
type CommandWithHandler<Name$1 extends string = string, Parameters extends readonly string[] = readonly string[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> = Command<Name$1, Parameters, Flags> & {
|
|
156
227
|
handler?: CommandHandler<Command<Name$1, Parameters, Flags>>;
|
|
157
228
|
};
|
|
158
229
|
type CommandsRecord = Record<string, Command>;
|
|
@@ -189,8 +260,9 @@ interface CreateOptions {
|
|
|
189
260
|
description?: string;
|
|
190
261
|
version?: string;
|
|
191
262
|
}
|
|
192
|
-
interface ParseOptions {
|
|
193
|
-
|
|
263
|
+
interface ParseOptions<Run extends boolean = true> {
|
|
264
|
+
argv?: string[];
|
|
265
|
+
run?: Run;
|
|
194
266
|
}
|
|
195
267
|
declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends ClercFlagsDefinition = {}> {
|
|
196
268
|
#private;
|
|
@@ -208,15 +280,13 @@ declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends Cl
|
|
|
208
280
|
version(version: string): this;
|
|
209
281
|
use(plugin: Plugin): this;
|
|
210
282
|
errorHandler(handler: ErrorHandler$1): this;
|
|
211
|
-
command<Name$1 extends string, Parameters extends string[] = [], Flags extends ClercFlagsDefinition = {}>(command: CommandWithHandler<Name$1,
|
|
212
|
-
command<Name$1 extends string, Parameters extends string[] = [], Flags extends ClercFlagsDefinition = {}>(name: Name$1 extends keyof Commands ? ["COMMAND ALREADY EXISTS"] : Name$1, description: string, options?: CommandOptions<
|
|
213
|
-
globalFlag<Name$1 extends string, Flag extends
|
|
283
|
+
command<Name$1 extends string, const Parameters extends readonly string[] = readonly [], Flags extends ClercFlagsDefinition = {}>(command: CommandWithHandler<Name$1, Parameters, Flags>): Clerc<Commands & Record<string, CommandWithHandler<Name$1, Parameters, Flags>>, GlobalFlags>;
|
|
284
|
+
command<Name$1 extends string, const Parameters extends readonly string[] = readonly [], Flags extends ClercFlagsDefinition = {}>(name: Name$1 extends keyof Commands ? ["COMMAND ALREADY EXISTS"] : Name$1, description: string, options?: CommandOptions<Parameters, Flags>): Clerc<Commands & Record<Name$1, Command<Name$1, Parameters, Flags>>, GlobalFlags>;
|
|
285
|
+
globalFlag<Name$1 extends string, Flag extends ClercGlobalFlagDefinitionValue>(name: Name$1, description: string, options: Flag): Clerc<Commands, GlobalFlags & Record<Name$1, Flag>>;
|
|
214
286
|
interceptor(interceptor: Interceptor<Command, GlobalFlags>): this;
|
|
215
287
|
on<Name$1 extends LiteralUnion<keyof Commands, string>>(name: Name$1, handler: CommandHandler<Commands[Name$1], GlobalFlags>): this;
|
|
216
|
-
run(): void
|
|
217
|
-
parse(
|
|
218
|
-
run
|
|
219
|
-
}?: ParseOptions): this;
|
|
288
|
+
run(): Promise<void>;
|
|
289
|
+
parse<Run extends boolean = true>(argvOrOptions?: string[] | ParseOptions<Run>): Run extends true ? Promise<void> : this;
|
|
220
290
|
}
|
|
221
291
|
//#endregion
|
|
222
292
|
//#region src/commands.d.ts
|
|
@@ -225,10 +295,10 @@ declare function resolveCommand(commandsMap: CommandsMap, parameters: string[]):
|
|
|
225
295
|
//#region src/errors.d.ts
|
|
226
296
|
declare class NoSuchCommandError extends Error {
|
|
227
297
|
commandName: string;
|
|
228
|
-
constructor(commandName: string);
|
|
298
|
+
constructor(commandName: string, text?: string);
|
|
229
299
|
}
|
|
230
|
-
declare class
|
|
231
|
-
constructor();
|
|
300
|
+
declare class NoCommandSpecifiedError extends Error {
|
|
301
|
+
constructor(text?: string);
|
|
232
302
|
}
|
|
233
303
|
declare class InvalidCommandError extends Error {
|
|
234
304
|
constructor(message: string);
|
|
@@ -241,7 +311,7 @@ declare class InvalidParametersError extends Error {
|
|
|
241
311
|
}
|
|
242
312
|
//#endregion
|
|
243
313
|
//#region src/helpers.d.ts
|
|
244
|
-
declare const defineCommand: <Name$1 extends string, Parameters extends string[] = [], Flags extends ClercFlagsDefinition = {}>(command:
|
|
314
|
+
declare const defineCommand: <Name$1 extends string, const Parameters extends readonly string[] = readonly [], Flags extends ClercFlagsDefinition = {}>(command: Command<Name$1, Parameters, Flags>, handler?: NoInfer<CommandHandler<Command<Name$1, Parameters, Flags>>>) => CommandWithHandler<Name$1, Parameters, Flags>;
|
|
245
315
|
//#endregion
|
|
246
316
|
//#region src/ignore.d.ts
|
|
247
317
|
declare function createStopAtFirstParameter(): IgnoreFunction;
|
|
@@ -249,4 +319,4 @@ declare function createStopAtFirstParameter(): IgnoreFunction;
|
|
|
249
319
|
//#region src/plugin.d.ts
|
|
250
320
|
declare const definePlugin: (plugin: Plugin) => Plugin;
|
|
251
321
|
//#endregion
|
|
252
|
-
export { BaseContext, Clerc, ClercFlagOptions, ClercFlagsDefinition, Command, CommandCustomOptions, CommandHandler, CommandHandlerContext, CommandOptions, CommandWithHandler, CommandsMap, CommandsRecord, ErrorHandler, InferParameters, Interceptor, InterceptorContext, InterceptorHandler, InterceptorNext, InterceptorObject, InvalidCommandError, InvalidParametersError, MakeEmitterEvents, MissingRequiredMetadataError,
|
|
322
|
+
export { BaseContext, Choices, Clerc, ClercFlagOptions, ClercFlagOptionsWithDescription, ClercFlagsDefinition, ClercGlobalFlagDefinitionValue, Command, CommandCustomOptions, CommandHandler, CommandHandlerContext, CommandOptions, CommandWithHandler, CommandsMap, CommandsRecord, type CreateOptions, DOUBLE_DASH, ErrorHandler, FlagCustomOptions, InferParameters, Interceptor, InterceptorContext, InterceptorHandler, InterceptorNext, InterceptorObject, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MakeEmitterEvents, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, type ParseOptions, type index_d_exports as Parser, Plugin, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, resolveCommand };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,30 @@
|
|
|
1
|
-
import { DOUBLE_DASH, PARAMETER, parse } from "@clerc/parser";
|
|
2
|
-
import { toArray } from "@clerc/utils";
|
|
1
|
+
import { Choices, DOUBLE_DASH, DOUBLE_DASH as DOUBLE_DASH$1, InvalidSchemaError, KNOWN_FLAG, PARAMETER, PARAMETER as PARAMETER$1, UNKNOWN_FLAG, parse } from "@clerc/parser";
|
|
3
2
|
import { LiteEmit } from "lite-emit";
|
|
4
3
|
|
|
4
|
+
//#region ../utils/src/index.ts
|
|
5
|
+
const toArray = (a) => Array.isArray(a) ? a : [a];
|
|
6
|
+
/**
|
|
7
|
+
* Converts a dash- or space-separated string to camelCase.
|
|
8
|
+
* Not using regexp for better performance, because this function is used in parser.
|
|
9
|
+
*/
|
|
10
|
+
function camelCase(str) {
|
|
11
|
+
const firstIdx = Math.min(str.includes("-") ? str.indexOf("-") : Infinity, str.includes(" ") ? str.indexOf(" ") : Infinity);
|
|
12
|
+
if (firstIdx === Infinity) return str;
|
|
13
|
+
let result = str.slice(0, firstIdx);
|
|
14
|
+
for (let i = firstIdx; i < str.length; i++) if ((str[i] === "-" || str[i] === " ") && i + 1 < str.length) {
|
|
15
|
+
const nextChar = str.charCodeAt(i + 1);
|
|
16
|
+
if (nextChar >= 97 && nextChar <= 122) {
|
|
17
|
+
result += String.fromCharCode(nextChar - 32);
|
|
18
|
+
i++;
|
|
19
|
+
} else {
|
|
20
|
+
result += str[i + 1];
|
|
21
|
+
i++;
|
|
22
|
+
}
|
|
23
|
+
} else if (str[i] !== "-" && str[i] !== " ") result += str[i];
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
5
28
|
//#region src/commands.ts
|
|
6
29
|
function resolveCommand(commandsMap, parameters) {
|
|
7
30
|
for (let i = parameters.length; i >= 0; i--) {
|
|
@@ -14,14 +37,14 @@ function resolveCommand(commandsMap, parameters) {
|
|
|
14
37
|
//#endregion
|
|
15
38
|
//#region src/errors.ts
|
|
16
39
|
var NoSuchCommandError = class extends Error {
|
|
17
|
-
constructor(commandName) {
|
|
18
|
-
super(
|
|
40
|
+
constructor(commandName, text = `No such command: "${commandName}".`) {
|
|
41
|
+
super(text);
|
|
19
42
|
this.commandName = commandName;
|
|
20
43
|
}
|
|
21
44
|
};
|
|
22
|
-
var
|
|
23
|
-
constructor() {
|
|
24
|
-
super(
|
|
45
|
+
var NoCommandSpecifiedError = class extends Error {
|
|
46
|
+
constructor(text = "No command specified.") {
|
|
47
|
+
super(text);
|
|
25
48
|
}
|
|
26
49
|
};
|
|
27
50
|
var InvalidCommandError = class extends Error {
|
|
@@ -82,7 +105,7 @@ function getParametersToResolve(argv) {
|
|
|
82
105
|
}
|
|
83
106
|
return parameters;
|
|
84
107
|
}
|
|
85
|
-
const PARAMETER_REGEX = /^(<|\[)(\w+)(\.\.\.)?(\]|>)$/;
|
|
108
|
+
const PARAMETER_REGEX = /^(<|\[)([\w ]+)(\.\.\.)?(\]|>)$/;
|
|
86
109
|
const isParameterDefinitionBracketsValid = (definition) => definition.startsWith("<") && definition.endsWith(">") || definition.startsWith("[") && definition.endsWith("]");
|
|
87
110
|
function _parseParameters(definitions, parameters) {
|
|
88
111
|
const result = {};
|
|
@@ -90,7 +113,7 @@ function _parseParameters(definitions, parameters) {
|
|
|
90
113
|
for (const [i, definition] of definitions.entries()) {
|
|
91
114
|
const match = definition.match(PARAMETER_REGEX);
|
|
92
115
|
if (!match || !isParameterDefinitionBracketsValid(definition)) throw new InvalidParametersError(`Invalid parameter definition: ${definition}`);
|
|
93
|
-
const name = match[2];
|
|
116
|
+
const name = camelCase(match[2]);
|
|
94
117
|
const isVariadic = !!match[3];
|
|
95
118
|
const isRequired = definition.startsWith("<");
|
|
96
119
|
if (name in result) throw new InvalidParametersError(`Duplicate parameter name: ${name}`);
|
|
@@ -105,7 +128,7 @@ function _parseParameters(definitions, parameters) {
|
|
|
105
128
|
return result;
|
|
106
129
|
}
|
|
107
130
|
function parseParameters(definitions, parameters, doubleDashParameters) {
|
|
108
|
-
const doubleDashIndex = definitions.indexOf(DOUBLE_DASH);
|
|
131
|
+
const doubleDashIndex = definitions.indexOf(DOUBLE_DASH$1);
|
|
109
132
|
if (doubleDashIndex === -1) return _parseParameters(definitions, parameters);
|
|
110
133
|
else {
|
|
111
134
|
const definitionBeforeDoubleDash = definitions.slice(0, doubleDashIndex);
|
|
@@ -198,7 +221,7 @@ var Clerc = class Clerc {
|
|
|
198
221
|
#callWithErrorHandler(fn) {
|
|
199
222
|
try {
|
|
200
223
|
const result = fn();
|
|
201
|
-
if (result instanceof Promise) result.catch((error) => {
|
|
224
|
+
if (result instanceof Promise) return result.catch((error) => {
|
|
202
225
|
this.#handleError(error);
|
|
203
226
|
});
|
|
204
227
|
return result;
|
|
@@ -217,7 +240,7 @@ var Clerc = class Clerc {
|
|
|
217
240
|
description,
|
|
218
241
|
...options
|
|
219
242
|
} : nameOrCommandObject;
|
|
220
|
-
const aliases = toArray(
|
|
243
|
+
const aliases = toArray(command?.alias ?? []);
|
|
221
244
|
this.#callWithErrorHandler(() => this.#validateCommandNameAndAlias(command.name, aliases));
|
|
222
245
|
this.#commands.set(command.name, command);
|
|
223
246
|
for (const alias of aliases) this.#commands.set(alias, {
|
|
@@ -257,7 +280,7 @@ var Clerc = class Clerc {
|
|
|
257
280
|
ignore
|
|
258
281
|
}));
|
|
259
282
|
}
|
|
260
|
-
run() {
|
|
283
|
+
async run() {
|
|
261
284
|
const parametersToResolve = getParametersToResolve(this.#argv);
|
|
262
285
|
const [command, calledAs] = resolveCommand(this.#commands, parametersToResolve);
|
|
263
286
|
const argvToPass = command && calledAs.length > 0 ? this.#argv.slice(calledAs.split(" ").length) : this.#argv;
|
|
@@ -277,37 +300,42 @@ var Clerc = class Clerc {
|
|
|
277
300
|
flags: parsed.flags,
|
|
278
301
|
ignored: parsed.ignored,
|
|
279
302
|
rawParsed: parsed,
|
|
280
|
-
|
|
303
|
+
missingParameters: !!parametersError
|
|
281
304
|
};
|
|
282
305
|
const emitInterceptor = {
|
|
283
306
|
enforce: "post",
|
|
284
307
|
handler: (ctx) => {
|
|
285
308
|
if (parametersError) throw parametersError;
|
|
286
309
|
if (command) this.#emitter.emit(command.name, ctx);
|
|
287
|
-
else throw parametersToResolve.length > 0 ? new NoSuchCommandError(parametersToResolve.join(" ")) : new
|
|
310
|
+
else throw parametersToResolve.length > 0 ? new NoSuchCommandError(parametersToResolve.join(" ")) : new NoCommandSpecifiedError();
|
|
288
311
|
}
|
|
289
312
|
};
|
|
290
313
|
const composedInterceptor = compose([...this.#interceptors, emitInterceptor]);
|
|
291
|
-
this.#callWithErrorHandler(() => composedInterceptor(context));
|
|
314
|
+
return this.#callWithErrorHandler(() => composedInterceptor(context));
|
|
292
315
|
}
|
|
293
|
-
parse(
|
|
316
|
+
parse(argvOrOptions = platformArgv) {
|
|
294
317
|
this.#callWithErrorHandler(() => this.#validate());
|
|
318
|
+
if (Array.isArray(argvOrOptions)) argvOrOptions = { argv: argvOrOptions };
|
|
319
|
+
const { argv = platformArgv, run = true } = argvOrOptions;
|
|
295
320
|
this.#argv = argv;
|
|
296
|
-
if (run) this.run();
|
|
321
|
+
if (run) return this.run();
|
|
297
322
|
return this;
|
|
298
323
|
}
|
|
299
324
|
};
|
|
300
325
|
|
|
301
326
|
//#endregion
|
|
302
327
|
//#region src/helpers.ts
|
|
303
|
-
const defineCommand = (command) =>
|
|
328
|
+
const defineCommand = (command, handler) => ({
|
|
329
|
+
...command,
|
|
330
|
+
handler
|
|
331
|
+
});
|
|
304
332
|
|
|
305
333
|
//#endregion
|
|
306
334
|
//#region src/ignore.ts
|
|
307
335
|
function createStopAtFirstParameter() {
|
|
308
336
|
let encounteredParameter = false;
|
|
309
337
|
return (type) => {
|
|
310
|
-
if (type === PARAMETER && !encounteredParameter) {
|
|
338
|
+
if (type === PARAMETER$1 && !encounteredParameter) {
|
|
311
339
|
encounteredParameter = true;
|
|
312
340
|
return false;
|
|
313
341
|
}
|
|
@@ -320,4 +348,4 @@ function createStopAtFirstParameter() {
|
|
|
320
348
|
const definePlugin = (plugin) => plugin;
|
|
321
349
|
|
|
322
350
|
//#endregion
|
|
323
|
-
export { Clerc, InvalidCommandError, InvalidParametersError, MissingRequiredMetadataError,
|
|
351
|
+
export { Choices, Clerc, DOUBLE_DASH, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, resolveCommand };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.11",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc core",
|
|
@@ -45,14 +45,10 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"lite-emit": "^3.1.0",
|
|
48
|
-
"@clerc/parser": "^1.0.0-beta.
|
|
49
|
-
"@clerc/utils": "1.0.0-beta.1"
|
|
48
|
+
"@clerc/parser": "^1.0.0-beta.11"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
52
|
-
"is-platform": "^1.0.0"
|
|
53
|
-
|
|
54
|
-
"scripts": {
|
|
55
|
-
"build": "tsdown",
|
|
56
|
-
"watch": "tsdown --watch"
|
|
51
|
+
"is-platform": "^1.0.0",
|
|
52
|
+
"@clerc/utils": "1.0.0-beta.11"
|
|
57
53
|
}
|
|
58
54
|
}
|