@clerc/core 1.0.0-beta.14 → 1.0.0-beta.16
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/chunk-BAz01cYq.js +18 -0
- package/dist/index.d.ts +97 -24
- package/dist/index.js +99 -13
- package/package.json +3 -3
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (all, symbols) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) {
|
|
6
|
+
__defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
if (symbols) {
|
|
12
|
+
__defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
}
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { __export as t };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { ErrorHandler as ErrorHandler$1 } from "lite-emit";
|
|
2
2
|
|
|
3
|
-
//#region rolldown:runtime
|
|
4
|
-
//#endregion
|
|
5
3
|
//#region ../parser/src/errors.d.ts
|
|
6
4
|
declare class InvalidSchemaError extends Error {
|
|
7
5
|
constructor(message: string);
|
|
8
6
|
}
|
|
7
|
+
declare class MissingRequiredFlagError extends Error {
|
|
8
|
+
constructor(name: string);
|
|
9
|
+
}
|
|
9
10
|
//#endregion
|
|
10
11
|
//#region ../parser/src/flag-types.d.ts
|
|
11
12
|
/**
|
|
12
|
-
* Creates a
|
|
13
|
+
* Creates a Enum type function that validates the input against allowed values.
|
|
13
14
|
* The display name will be formatted as "value1 | value2 | ..." for help output.
|
|
14
15
|
*
|
|
15
16
|
* @param values - Array of allowed string values
|
|
@@ -18,11 +19,11 @@ declare class InvalidSchemaError extends Error {
|
|
|
18
19
|
*
|
|
19
20
|
* @example
|
|
20
21
|
* ```typescript
|
|
21
|
-
* const format =
|
|
22
|
+
* const format = Enum(['json', 'yaml', 'xml']);
|
|
22
23
|
* // Help output will show: json | yaml | xml
|
|
23
24
|
* ```
|
|
24
25
|
*/
|
|
25
|
-
declare function
|
|
26
|
+
declare function Enum<T extends string>(...values: T[]): FlagTypeFunction<T>;
|
|
26
27
|
//#endregion
|
|
27
28
|
//#region ../utils/src/types/type-fest.d.ts
|
|
28
29
|
type LiteralUnion<LiteralType, BaseType> = LiteralType | (BaseType & Record<never, never>);
|
|
@@ -43,21 +44,24 @@ type UnionToIntersection<U$1> = (U$1 extends any ? (k: U$1) => void : never) ext
|
|
|
43
44
|
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;
|
|
44
45
|
//#endregion
|
|
45
46
|
//#region ../parser/src/types.d.ts
|
|
46
|
-
|
|
47
|
+
interface FlagDefaultValueFunction<T> {
|
|
48
|
+
(): T;
|
|
47
49
|
display?: string;
|
|
48
|
-
}
|
|
50
|
+
}
|
|
51
|
+
type FlagDefaultValue<T = unknown> = T | FlagDefaultValueFunction<T>;
|
|
49
52
|
/**
|
|
50
53
|
* Defines how a string input is converted to the target type T.
|
|
51
54
|
*
|
|
52
55
|
* @template T The target type.
|
|
53
56
|
*/
|
|
54
|
-
|
|
57
|
+
interface FlagTypeFunction<T = unknown> {
|
|
58
|
+
(value: string): T;
|
|
55
59
|
/**
|
|
56
60
|
* Optional display name for the type, useful in help output.
|
|
57
61
|
* If provided, this will be shown instead of the function name.
|
|
58
62
|
*/
|
|
59
63
|
display?: string;
|
|
60
|
-
}
|
|
64
|
+
}
|
|
61
65
|
/**
|
|
62
66
|
* A callback function to conditionally stop parsing.
|
|
63
67
|
* When it returns true, parsing stops and remaining arguments are preserved in `ignored`.
|
|
@@ -79,6 +83,8 @@ interface BaseFlagOptions<T extends FlagType = FlagType> {
|
|
|
79
83
|
alias?: MaybeArray<string>;
|
|
80
84
|
/** The default value of the flag. */
|
|
81
85
|
default?: unknown;
|
|
86
|
+
/** Whether the flag is required. */
|
|
87
|
+
required?: boolean;
|
|
82
88
|
}
|
|
83
89
|
type FlagOptions = (BaseFlagOptions<BooleanConstructor> & {
|
|
84
90
|
/**
|
|
@@ -149,13 +155,15 @@ type IsTypeAny<T extends FlagDefinitionValue> = IsAny<T> extends true ? true : T
|
|
|
149
155
|
} ? IsAny<Type> extends true ? true : false : false;
|
|
150
156
|
type _InferFlags<T extends FlagsDefinition> = { [K in keyof T]: IsTypeAny<T[K]> extends true ? any : T[K] extends readonly [BooleanConstructor] | {
|
|
151
157
|
type: readonly [BooleanConstructor];
|
|
152
|
-
} ? number : T[K] extends ObjectConstructor | {
|
|
158
|
+
} ? number | InferFlagDefault<T[K], never> : T[K] extends ObjectConstructor | {
|
|
153
159
|
type: ObjectConstructor;
|
|
154
|
-
} ? ObjectInputType : T[K] extends readonly [FlagType<infer U>] | {
|
|
160
|
+
} ? ObjectInputType | InferFlagDefault<T[K], never> : T[K] extends readonly [FlagType<infer U>] | {
|
|
155
161
|
type: readonly [FlagType<infer U>];
|
|
156
162
|
} ? U[] | InferFlagDefault<T[K], never> : T[K] extends FlagType<infer U> | {
|
|
157
163
|
type: FlagType<infer U>;
|
|
158
|
-
} ? U | InferFlagDefault<T[K], [U] extends [boolean] ? never :
|
|
164
|
+
} ? U | InferFlagDefault<T[K], [U] extends [boolean] ? never : T[K] extends {
|
|
165
|
+
required: true;
|
|
166
|
+
} ? never : undefined> : never };
|
|
159
167
|
/**
|
|
160
168
|
* An advanced utility type that infers the exact type of the `flags` object in the parsed result,
|
|
161
169
|
* based on the provided `flags` configuration object T.
|
|
@@ -177,7 +185,7 @@ declare function createParser<T extends FlagsDefinition>(options?: ParserOptions
|
|
|
177
185
|
};
|
|
178
186
|
declare const parse: <T extends FlagsDefinition>(args: string[], options?: ParserOptions<T>) => ParsedResult<InferFlags<T>>;
|
|
179
187
|
declare namespace index_d_exports {
|
|
180
|
-
export { BaseFlagOptions,
|
|
188
|
+
export { BaseFlagOptions, DOUBLE_DASH, Enum, FlagDefaultValue, FlagDefaultValueFunction, FlagDefinitionValue, FlagOptions, FlagType, FlagTypeFunction, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, RawInputType, UNKNOWN_FLAG, createParser, parse };
|
|
181
189
|
}
|
|
182
190
|
//#endregion
|
|
183
191
|
//#region src/types/clerc.d.ts
|
|
@@ -188,17 +196,36 @@ interface FlagCustomOptions {}
|
|
|
188
196
|
type ClercFlagOptions = FlagOptions & FlagCustomOptions;
|
|
189
197
|
type ClercGlobalFlagDefinitionValue = ClercFlagOptions | FlagType;
|
|
190
198
|
type ClercFlagOptionsWithDescription = ClercFlagOptions & {
|
|
191
|
-
description
|
|
199
|
+
description?: string;
|
|
192
200
|
};
|
|
193
201
|
type ClercFlagsDefinition = Record<string, ClercFlagOptionsWithDescription>;
|
|
194
202
|
//#endregion
|
|
203
|
+
//#region src/types/constraint.d.ts
|
|
204
|
+
/**
|
|
205
|
+
* Creates an enum constraint function that validates the input against a set of allowed values.
|
|
206
|
+
*
|
|
207
|
+
* @template T - The union type of allowed string values. Just a marker type to help with inference.
|
|
208
|
+
*/
|
|
209
|
+
interface ConstraintFunction<T = string> {
|
|
210
|
+
(value: string): void;
|
|
211
|
+
display?: string;
|
|
212
|
+
}
|
|
213
|
+
//#endregion
|
|
195
214
|
//#region src/types/parameters.d.ts
|
|
196
|
-
type
|
|
197
|
-
type
|
|
215
|
+
type InferStringParameter<T extends string, Type$1 = string> = T extends `<${infer Name extends string}...>` | `[${infer Name extends string}...]` ? Record<CamelCase<Name>, Type$1[]> : T extends `<${infer Name extends string}>` ? Record<CamelCase<Name>, Type$1> : T extends `[${infer Name extends string}]` ? Record<CamelCase<Name>, Type$1 | undefined> : never;
|
|
216
|
+
type InferParameter<T extends Parameter> = T extends string ? InferStringParameter<T> : T extends ParameterDefinition ? T["constraint"] extends ConstraintFunction<infer U> ? InferStringParameter<T["key"], U> : InferStringParameter<T["key"]> : never;
|
|
217
|
+
type InferParameters<T extends readonly Parameter[]> = T extends readonly (infer U extends Parameter)[] ? Prettify<UnionToIntersection<InferParameter<U>>> : never;
|
|
218
|
+
interface ParameterDefinition {
|
|
219
|
+
key: string;
|
|
220
|
+
description?: string;
|
|
221
|
+
constraint?: ConstraintFunction;
|
|
222
|
+
}
|
|
223
|
+
type Parameter = string | ParameterDefinition;
|
|
198
224
|
//#endregion
|
|
199
225
|
//#region src/types/context.d.ts
|
|
200
226
|
type AddStringIndex<T> = T & Record<string, any>;
|
|
201
227
|
type InferFlagsWithGlobal<C extends Command, GF extends ClercFlagsDefinition> = AddStringIndex<InferFlags<NonNullable<C["flags"]> & Omit<GF, keyof NonNullable<C["flags"]>>>>;
|
|
228
|
+
interface ContextStore {}
|
|
202
229
|
interface BaseContext<C extends Command = Command, GF extends ClercFlagsDefinition = {}> {
|
|
203
230
|
resolved: boolean;
|
|
204
231
|
command?: C;
|
|
@@ -208,11 +235,12 @@ interface BaseContext<C extends Command = Command, GF extends ClercFlagsDefiniti
|
|
|
208
235
|
ignored: string[];
|
|
209
236
|
rawParsed: ParsedResult<InferFlagsWithGlobal<C, GF>>;
|
|
210
237
|
missingParameters: boolean;
|
|
238
|
+
store: ContextStore;
|
|
211
239
|
}
|
|
212
240
|
//#endregion
|
|
213
241
|
//#region src/types/command.d.ts
|
|
214
242
|
interface CommandCustomOptions {}
|
|
215
|
-
interface CommandOptions<Parameters extends readonly
|
|
243
|
+
interface CommandOptions<Parameters extends readonly Parameter[] = readonly Parameter[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandCustomOptions {
|
|
216
244
|
alias?: MaybeArray<string>;
|
|
217
245
|
parameters?: Parameters;
|
|
218
246
|
flags?: Flags;
|
|
@@ -221,11 +249,11 @@ interface CommandOptions<Parameters extends readonly string[] = readonly string[
|
|
|
221
249
|
*/
|
|
222
250
|
ignore?: IgnoreFunction;
|
|
223
251
|
}
|
|
224
|
-
interface Command<Name$1 extends string = string, Parameters extends readonly
|
|
252
|
+
interface Command<Name$1 extends string = string, Parameters extends readonly Parameter[] = readonly Parameter[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> extends CommandOptions<Parameters, Flags> {
|
|
225
253
|
name: Name$1;
|
|
226
|
-
description
|
|
254
|
+
description?: string;
|
|
227
255
|
}
|
|
228
|
-
type CommandWithHandler<Name$1 extends string = string, Parameters extends readonly
|
|
256
|
+
type CommandWithHandler<Name$1 extends string = string, Parameters extends readonly Parameter[] = readonly Parameter[], Flags extends ClercFlagsDefinition = ClercFlagsDefinition> = Command<Name$1, Parameters, Flags> & {
|
|
229
257
|
handler?: CommandHandler<Command<Name$1, Parameters, Flags>>;
|
|
230
258
|
};
|
|
231
259
|
type CommandsRecord = Record<string, Command>;
|
|
@@ -275,6 +303,7 @@ declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends Cl
|
|
|
275
303
|
get _version(): string;
|
|
276
304
|
get _commands(): CommandsMap;
|
|
277
305
|
get _globalFlags(): GlobalFlags;
|
|
306
|
+
get store(): ContextStore;
|
|
278
307
|
static create(options?: CreateOptions): Clerc;
|
|
279
308
|
name(name: string): this;
|
|
280
309
|
scriptName(scriptName: string): this;
|
|
@@ -282,9 +311,11 @@ declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends Cl
|
|
|
282
311
|
version(version: string): this;
|
|
283
312
|
use(plugin: Plugin): this;
|
|
284
313
|
errorHandler(handler: ErrorHandler$1): this;
|
|
285
|
-
command<Name$1 extends string, const Parameters extends readonly
|
|
286
|
-
command<Name$1 extends string, const Parameters extends readonly
|
|
314
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[] = readonly [], Flags extends ClercFlagsDefinition = {}>(command: CommandWithHandler<Name$1, Parameters, Flags>): Clerc<Commands & Record<string, CommandWithHandler<Name$1, Parameters, Flags>>, GlobalFlags>;
|
|
315
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[] = readonly [], Flags extends ClercFlagsDefinition = {}>(name: Name$1 extends keyof Commands ? ["COMMAND ALREADY EXISTS"] : Name$1, options?: CommandOptions<Parameters, Flags>): Clerc<Commands & Record<Name$1, Command<Name$1, Parameters, Flags>>, GlobalFlags>;
|
|
316
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[] = 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>;
|
|
287
317
|
globalFlag<Name$1 extends string, Flag extends ClercGlobalFlagDefinitionValue>(name: Name$1, description: string, options: Flag): Clerc<Commands, GlobalFlags & Record<Name$1, Flag>>;
|
|
318
|
+
globalFlag<Name$1 extends string, Flag extends ClercGlobalFlagDefinitionValue>(name: Name$1, options: Flag): Clerc<Commands, GlobalFlags & Record<Name$1, Flag>>;
|
|
288
319
|
interceptor(interceptor: Interceptor<Command, GlobalFlags>): this;
|
|
289
320
|
on<Name$1 extends LiteralUnion<keyof Commands, string>>(name: Name$1, handler: CommandHandler<Commands[Name$1], GlobalFlags>): this;
|
|
290
321
|
run(): Promise<void>;
|
|
@@ -293,6 +324,45 @@ declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends Cl
|
|
|
293
324
|
//#endregion
|
|
294
325
|
//#region src/commands.d.ts
|
|
295
326
|
declare function resolveCommand(commandsMap: CommandsMap, parameters: string[]): [Command, string] | [undefined, undefined];
|
|
327
|
+
declare namespace constraint_d_exports {
|
|
328
|
+
export { Custom, Enum$1 as Enum, Range, Regex };
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Creates an enum constraint function that validates the input against a set of allowed values.
|
|
332
|
+
*
|
|
333
|
+
* @param values - Array of allowed string values
|
|
334
|
+
* @returns A ConstraintFunction that validates the input value
|
|
335
|
+
* @throws {Error} If the value is not in the allowed values list
|
|
336
|
+
*/
|
|
337
|
+
declare function Enum$1<T extends string>(...values: T[]): ConstraintFunction<T>;
|
|
338
|
+
/**
|
|
339
|
+
* Creates a range constraint function that validates the input is a number within the specified range.
|
|
340
|
+
*
|
|
341
|
+
* @param min - The minimum acceptable value (inclusive)
|
|
342
|
+
* @param max - The maximum acceptable value (inclusive)
|
|
343
|
+
* @returns A ConstraintFunction that validates the input value
|
|
344
|
+
* @throws {Error} If the value is not a number or is outside the specified range
|
|
345
|
+
*/
|
|
346
|
+
declare function Range(min: number, max: number): ConstraintFunction;
|
|
347
|
+
/**
|
|
348
|
+
* Creates a regex constraint function that validates the input against the provided pattern.
|
|
349
|
+
*
|
|
350
|
+
* @param pattern - The regular expression pattern to validate against
|
|
351
|
+
* @param description - Optional description for display purposes
|
|
352
|
+
* @returns A ConstraintFunction that validates the input value
|
|
353
|
+
* @throws {Error} If the value does not match the regex pattern
|
|
354
|
+
*/
|
|
355
|
+
declare function Regex(pattern: RegExp, description?: string): ConstraintFunction;
|
|
356
|
+
/**
|
|
357
|
+
* Just an utility to create custom constraints, helps you set the display name.
|
|
358
|
+
*
|
|
359
|
+
* @param validator - A function that validates the input value. Should return true if valid, false or throw an error if invalid.
|
|
360
|
+
* @param display - Optional display name for the constraint, useful in help output.
|
|
361
|
+
* @param errorMessage - Optional function to generate a custom error message when validation fails.
|
|
362
|
+
* @returns A ConstraintFunction that applies the custom validation logic.
|
|
363
|
+
* @throws {Error} If the validator returns false or throws an error.
|
|
364
|
+
*/
|
|
365
|
+
declare function Custom(validator: (value: string) => boolean | void, display?: string, errorMessage?: (value: string) => string): ConstraintFunction;
|
|
296
366
|
//#endregion
|
|
297
367
|
//#region src/errors.d.ts
|
|
298
368
|
declare class NoSuchCommandError extends Error {
|
|
@@ -311,9 +381,12 @@ declare class MissingRequiredMetadataError extends Error {
|
|
|
311
381
|
declare class InvalidParametersError extends Error {
|
|
312
382
|
constructor(message: string);
|
|
313
383
|
}
|
|
384
|
+
declare namespace flag_types_d_exports {
|
|
385
|
+
export { Enum };
|
|
386
|
+
}
|
|
314
387
|
//#endregion
|
|
315
388
|
//#region src/helpers.d.ts
|
|
316
|
-
declare const defineCommand: <Name$1 extends string, const Parameters extends readonly
|
|
389
|
+
declare const defineCommand: <Name$1 extends string, const Parameters extends readonly Parameter[] = readonly [], Flags extends ClercFlagsDefinition = {}>(command: Command<Name$1, Parameters, Flags>, handler?: NoInfer<CommandHandler<Command<Name$1, Parameters, Flags>>>) => CommandWithHandler<Name$1, Parameters, Flags>;
|
|
317
390
|
//#endregion
|
|
318
391
|
//#region src/ignore.d.ts
|
|
319
392
|
declare function createStopAtFirstParameter(): IgnoreFunction;
|
|
@@ -321,4 +394,4 @@ declare function createStopAtFirstParameter(): IgnoreFunction;
|
|
|
321
394
|
//#region src/plugin.d.ts
|
|
322
395
|
declare const definePlugin: (plugin: Plugin) => Plugin;
|
|
323
396
|
//#endregion
|
|
324
|
-
export { BaseContext,
|
|
397
|
+
export { BaseContext, Clerc, ClercFlagOptions, ClercFlagOptionsWithDescription, ClercFlagsDefinition, ClercGlobalFlagDefinitionValue, Command, CommandCustomOptions, CommandHandler, CommandHandlerContext, CommandOptions, CommandWithHandler, CommandsMap, CommandsRecord, ConstraintFunction, constraint_d_exports as Constraints, ContextStore, type CreateOptions, DOUBLE_DASH, ErrorHandler, FlagCustomOptions, InferParameters, Interceptor, InterceptorContext, InterceptorHandler, InterceptorNext, InterceptorObject, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MakeEmitterEvents, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, Parameter, ParameterDefinition, type ParseOptions, type index_d_exports as Parser, Plugin, flag_types_d_exports as Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, resolveCommand };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __export } from "./chunk-BAz01cYq.js";
|
|
2
|
+
import { DOUBLE_DASH, DOUBLE_DASH as DOUBLE_DASH$1, Enum, InvalidSchemaError, KNOWN_FLAG, PARAMETER, PARAMETER as PARAMETER$1, UNKNOWN_FLAG, parse } from "@clerc/parser";
|
|
2
3
|
import { LiteEmit } from "lite-emit";
|
|
3
4
|
|
|
4
5
|
//#region ../utils/src/index.ts
|
|
@@ -111,11 +112,12 @@ function _parseParameters(definitions, parameters) {
|
|
|
111
112
|
const result = {};
|
|
112
113
|
let hasOptional = false;
|
|
113
114
|
for (const [i, definition] of definitions.entries()) {
|
|
114
|
-
const
|
|
115
|
-
|
|
115
|
+
const definitionKey = typeof definition === "string" ? definition : definition.key;
|
|
116
|
+
const match = definitionKey.match(PARAMETER_REGEX);
|
|
117
|
+
if (!match || !isParameterDefinitionBracketsValid(definitionKey)) throw new InvalidParametersError(`Invalid parameter definition: ${definitionKey}`);
|
|
116
118
|
const name = camelCase(match[2]);
|
|
117
119
|
const isVariadic = !!match[3];
|
|
118
|
-
const isRequired =
|
|
120
|
+
const isRequired = definitionKey.startsWith("<");
|
|
119
121
|
if (name in result) throw new InvalidParametersError(`Duplicate parameter name: ${name}`);
|
|
120
122
|
if (isVariadic && i !== definitions.length - 1) throw new InvalidParametersError("Variadic parameter must be the last parameter in the definition.");
|
|
121
123
|
if (isRequired) {
|
|
@@ -123,6 +125,10 @@ function _parseParameters(definitions, parameters) {
|
|
|
123
125
|
} else hasOptional = true;
|
|
124
126
|
const value = isVariadic ? parameters.slice(i) : parameters[i];
|
|
125
127
|
if (isRequired && (isVariadic ? value.length === 0 : value === void 0)) throw new InvalidParametersError(`Missing required ${isVariadic ? "variadic " : ""}parameter: ${name}`);
|
|
128
|
+
if (typeof definition !== "string" && definition.constraint) {
|
|
129
|
+
if (isVariadic) for (const v of value) definition.constraint(v);
|
|
130
|
+
else if (value !== void 0) definition.constraint(value);
|
|
131
|
+
}
|
|
126
132
|
result[name] = value;
|
|
127
133
|
}
|
|
128
134
|
return result;
|
|
@@ -157,6 +163,7 @@ var Clerc = class Clerc {
|
|
|
157
163
|
#commands = /* @__PURE__ */ new Map();
|
|
158
164
|
#emitter = new LiteEmit({ errorHandler: (error) => this.#handleError(error) });
|
|
159
165
|
#globalFlags = {};
|
|
166
|
+
#store = {};
|
|
160
167
|
#interceptors = [];
|
|
161
168
|
#errorHandlers = [];
|
|
162
169
|
#name = "";
|
|
@@ -187,6 +194,9 @@ var Clerc = class Clerc {
|
|
|
187
194
|
get _globalFlags() {
|
|
188
195
|
return this.#globalFlags;
|
|
189
196
|
}
|
|
197
|
+
get store() {
|
|
198
|
+
return this.#store;
|
|
199
|
+
}
|
|
190
200
|
static create(options) {
|
|
191
201
|
return new Clerc(options);
|
|
192
202
|
}
|
|
@@ -234,11 +244,12 @@ var Clerc = class Clerc {
|
|
|
234
244
|
if (this.#commands.has(name)) throw new InvalidCommandError(`Command with name "${name}" already exists.`);
|
|
235
245
|
for (const alias of aliases) if (this.#commands.has(alias)) throw new InvalidCommandError(`Command with name "${alias}" already exists.`);
|
|
236
246
|
}
|
|
237
|
-
command(nameOrCommandObject,
|
|
247
|
+
command(nameOrCommandObject, descriptionOrOptions, options) {
|
|
248
|
+
const isDescription = typeof descriptionOrOptions === "string";
|
|
238
249
|
const command = typeof nameOrCommandObject === "string" ? {
|
|
239
250
|
name: nameOrCommandObject,
|
|
240
|
-
description,
|
|
241
|
-
...options
|
|
251
|
+
description: isDescription ? descriptionOrOptions : void 0,
|
|
252
|
+
...isDescription ? options : descriptionOrOptions
|
|
242
253
|
} : nameOrCommandObject;
|
|
243
254
|
const aliases = toArray(command?.alias ?? []);
|
|
244
255
|
this.#callWithErrorHandler(() => this.#validateCommandNameAndAlias(command.name, aliases));
|
|
@@ -250,10 +261,11 @@ var Clerc = class Clerc {
|
|
|
250
261
|
if (command.handler) this.on(command.name, command.handler);
|
|
251
262
|
return this;
|
|
252
263
|
}
|
|
253
|
-
globalFlag(name,
|
|
264
|
+
globalFlag(name, descriptionOrOptions, options) {
|
|
265
|
+
const isDescription = typeof descriptionOrOptions === "string";
|
|
254
266
|
this.#globalFlags[name] = {
|
|
255
|
-
description,
|
|
256
|
-
...options
|
|
267
|
+
description: isDescription ? descriptionOrOptions : void 0,
|
|
268
|
+
...isDescription ? options : descriptionOrOptions
|
|
257
269
|
};
|
|
258
270
|
return this;
|
|
259
271
|
}
|
|
@@ -267,7 +279,6 @@ var Clerc = class Clerc {
|
|
|
267
279
|
}
|
|
268
280
|
#validate() {
|
|
269
281
|
if (!this.#scriptName) throw new MissingRequiredMetadataError("script name");
|
|
270
|
-
if (!this.#description) throw new MissingRequiredMetadataError("description");
|
|
271
282
|
if (!this.#version) throw new MissingRequiredMetadataError("version");
|
|
272
283
|
}
|
|
273
284
|
#parseArgv(argv, command) {
|
|
@@ -300,7 +311,8 @@ var Clerc = class Clerc {
|
|
|
300
311
|
flags: parsed.flags,
|
|
301
312
|
ignored: parsed.ignored,
|
|
302
313
|
rawParsed: parsed,
|
|
303
|
-
missingParameters: !!parametersError
|
|
314
|
+
missingParameters: !!parametersError,
|
|
315
|
+
store: { ...this.#store }
|
|
304
316
|
};
|
|
305
317
|
const emitInterceptor = {
|
|
306
318
|
enforce: "post",
|
|
@@ -323,6 +335,80 @@ var Clerc = class Clerc {
|
|
|
323
335
|
}
|
|
324
336
|
};
|
|
325
337
|
|
|
338
|
+
//#endregion
|
|
339
|
+
//#region src/constraint.ts
|
|
340
|
+
var constraint_exports = /* @__PURE__ */ __export({
|
|
341
|
+
Custom: () => Custom,
|
|
342
|
+
Enum: () => Enum$1,
|
|
343
|
+
Range: () => Range,
|
|
344
|
+
Regex: () => Regex
|
|
345
|
+
});
|
|
346
|
+
/**
|
|
347
|
+
* Creates an enum constraint function that validates the input against a set of allowed values.
|
|
348
|
+
*
|
|
349
|
+
* @param values - Array of allowed string values
|
|
350
|
+
* @returns A ConstraintFunction that validates the input value
|
|
351
|
+
* @throws {Error} If the value is not in the allowed values list
|
|
352
|
+
*/
|
|
353
|
+
function Enum$1(...values) {
|
|
354
|
+
function fn(value) {
|
|
355
|
+
if (!values.includes(value)) throw new Error(`Invalid value: ${value}. Must be one of: ${values.join(", ")}`);
|
|
356
|
+
}
|
|
357
|
+
fn.display = values.join(" | ");
|
|
358
|
+
return fn;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Creates a range constraint function that validates the input is a number within the specified range.
|
|
362
|
+
*
|
|
363
|
+
* @param min - The minimum acceptable value (inclusive)
|
|
364
|
+
* @param max - The maximum acceptable value (inclusive)
|
|
365
|
+
* @returns A ConstraintFunction that validates the input value
|
|
366
|
+
* @throws {Error} If the value is not a number or is outside the specified range
|
|
367
|
+
*/
|
|
368
|
+
function Range(min, max) {
|
|
369
|
+
function fn(value) {
|
|
370
|
+
const num = Number(value);
|
|
371
|
+
if (Number.isNaN(num) || num < min || num > max) throw new Error(`Invalid value: ${value}. Must be a number between ${min} and ${max}`);
|
|
372
|
+
}
|
|
373
|
+
fn.display = `${min}-${max}`;
|
|
374
|
+
return fn;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Creates a regex constraint function that validates the input against the provided pattern.
|
|
378
|
+
*
|
|
379
|
+
* @param pattern - The regular expression pattern to validate against
|
|
380
|
+
* @param description - Optional description for display purposes
|
|
381
|
+
* @returns A ConstraintFunction that validates the input value
|
|
382
|
+
* @throws {Error} If the value does not match the regex pattern
|
|
383
|
+
*/
|
|
384
|
+
function Regex(pattern, description) {
|
|
385
|
+
function fn(value) {
|
|
386
|
+
if (!pattern.test(value)) throw new Error(`Invalid value: ${value}. Must match pattern: ${pattern}`);
|
|
387
|
+
}
|
|
388
|
+
fn.display = description ?? pattern.toString();
|
|
389
|
+
return fn;
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Just an utility to create custom constraints, helps you set the display name.
|
|
393
|
+
*
|
|
394
|
+
* @param validator - A function that validates the input value. Should return true if valid, false or throw an error if invalid.
|
|
395
|
+
* @param display - Optional display name for the constraint, useful in help output.
|
|
396
|
+
* @param errorMessage - Optional function to generate a custom error message when validation fails.
|
|
397
|
+
* @returns A ConstraintFunction that applies the custom validation logic.
|
|
398
|
+
* @throws {Error} If the validator returns false or throws an error.
|
|
399
|
+
*/
|
|
400
|
+
function Custom(validator, display, errorMessage) {
|
|
401
|
+
function fn(value) {
|
|
402
|
+
if (validator(value) === false) throw new Error(errorMessage ? errorMessage(value) : `Invalid value: ${value}`);
|
|
403
|
+
}
|
|
404
|
+
fn.display = display;
|
|
405
|
+
return fn;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
//#endregion
|
|
409
|
+
//#region src/flag-types.ts
|
|
410
|
+
var flag_types_exports = /* @__PURE__ */ __export({ Enum: () => Enum });
|
|
411
|
+
|
|
326
412
|
//#endregion
|
|
327
413
|
//#region src/helpers.ts
|
|
328
414
|
const defineCommand = (command, handler) => ({
|
|
@@ -348,4 +434,4 @@ function createStopAtFirstParameter() {
|
|
|
348
434
|
const definePlugin = (plugin) => plugin;
|
|
349
435
|
|
|
350
436
|
//#endregion
|
|
351
|
-
export {
|
|
437
|
+
export { Clerc, constraint_exports as Constraints, DOUBLE_DASH, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, flag_types_exports as Types, 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.16",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc core",
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"lite-emit": "^3.1.0",
|
|
48
|
-
"@clerc/parser": "^1.0.0-beta.
|
|
48
|
+
"@clerc/parser": "^1.0.0-beta.16"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"is-platform": "^1.0.0",
|
|
52
|
-
"@clerc/utils": "1.0.0-beta.
|
|
52
|
+
"@clerc/utils": "1.0.0-beta.16"
|
|
53
53
|
}
|
|
54
54
|
}
|