@clerc/core 1.0.0-beta.25 → 1.0.0-beta.27
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 +22 -18
- package/dist/index.js +8 -3
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -5,9 +5,6 @@ import { Clerc as Clerc$1, CreateOptions as CreateOptions$1, Plugin as Plugin$1
|
|
|
5
5
|
declare class InvalidSchemaError extends Error {
|
|
6
6
|
constructor(message: string);
|
|
7
7
|
}
|
|
8
|
-
declare class MissingRequiredFlagError extends Error {
|
|
9
|
-
constructor(name: string);
|
|
10
|
-
}
|
|
11
8
|
//#endregion
|
|
12
9
|
//#region ../utils/src/types/type-fest.d.ts
|
|
13
10
|
type LiteralUnion<LiteralType, BaseType> = LiteralType | (BaseType & Record<never, never>);
|
|
@@ -20,6 +17,7 @@ type MapsSetsOrArrays = ReadonlyMap<unknown, unknown> | WeakMap<WeakKey, unknown
|
|
|
20
17
|
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;
|
|
21
18
|
type DeepPrettify<T, E = never> = ConditionalDeepPrettify<T, E | NonRecursiveType | MapsSetsOrArrays, object>;
|
|
22
19
|
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
20
|
+
type RequireExactlyOneOrNone<T, Keys extends keyof T = keyof T> = ({ [K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, never>> }[Keys] & Omit<T, Keys>) | (Partial<Record<Keys, never>> & Omit<T, Keys>);
|
|
23
21
|
//#endregion
|
|
24
22
|
//#region ../utils/src/types/index.d.ts
|
|
25
23
|
type MaybeArray<T> = T | T[];
|
|
@@ -61,7 +59,13 @@ interface TypeFunction<T = unknown> {
|
|
|
61
59
|
*/
|
|
62
60
|
type IgnoreFunction = (type: typeof KNOWN_FLAG | typeof UNKNOWN_FLAG | typeof PARAMETER, arg: string) => boolean;
|
|
63
61
|
type TypeValue<T = unknown> = TypeFunction<T> | readonly [TypeFunction<T>];
|
|
64
|
-
|
|
62
|
+
type FlagRequiredOrDefault = RequireExactlyOneOrNone<{
|
|
63
|
+
/** The default value of the flag. */
|
|
64
|
+
default?: unknown;
|
|
65
|
+
/** Whether the flag is required. */
|
|
66
|
+
required?: boolean;
|
|
67
|
+
}, "default" | "required">;
|
|
68
|
+
type BaseFlagOptions<T extends TypeValue = TypeValue> = FlagRequiredOrDefault & {
|
|
65
69
|
/**
|
|
66
70
|
* The type constructor or a function to convert the string value.
|
|
67
71
|
* To support multiple occurrences of a flag (e.g., --file a --file b), wrap the type in an array: [String], [Number].
|
|
@@ -70,11 +74,7 @@ interface BaseFlagOptions<T extends TypeValue = TypeValue> {
|
|
|
70
74
|
type: T;
|
|
71
75
|
/** Short flag alias (single character). */
|
|
72
76
|
short?: string;
|
|
73
|
-
|
|
74
|
-
default?: unknown;
|
|
75
|
-
/** Whether the flag is required. */
|
|
76
|
-
required?: boolean;
|
|
77
|
-
}
|
|
77
|
+
};
|
|
78
78
|
type FlagOptions = (BaseFlagOptions<BooleanConstructor> & {
|
|
79
79
|
/**
|
|
80
80
|
* Whether to enable the `--no-<flag>` syntax to set the value to false.
|
|
@@ -135,6 +135,8 @@ interface ParsedResult<TFlags extends Record<string, any>> {
|
|
|
135
135
|
unknown: Record<string, RawInputType>;
|
|
136
136
|
/** Arguments that were not parsed due to ignore callback. */
|
|
137
137
|
ignored: string[];
|
|
138
|
+
/** List of required flags that were not provided. */
|
|
139
|
+
missingRequiredFlags: string[];
|
|
138
140
|
}
|
|
139
141
|
type InferFlagDefault<T extends FlagDefinitionValue, Fallback> = T extends {
|
|
140
142
|
default: FlagDefaultValue<infer DefaultType>;
|
|
@@ -204,7 +206,7 @@ declare function createParser<T extends FlagsDefinition>(options?: ParserOptions
|
|
|
204
206
|
};
|
|
205
207
|
declare const parse: <T extends FlagsDefinition>(args: string[], options?: ParserOptions<T>) => ParsedResult<InferFlags<T>>;
|
|
206
208
|
declare namespace index_d_exports {
|
|
207
|
-
export { BaseFlagOptions, DOUBLE_DASH, Enum, FlagDefaultValue, FlagDefaultValueFunction, FlagDefinitionValue, FlagOptions, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG,
|
|
209
|
+
export { BaseFlagOptions, DOUBLE_DASH, Enum, FlagDefaultValue, FlagDefaultValueFunction, FlagDefinitionValue, FlagOptions, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, Range, RawInputType, Regex, TypeFunction, TypeValue, UNKNOWN_FLAG, createParser, parse };
|
|
208
210
|
}
|
|
209
211
|
//#endregion
|
|
210
212
|
//#region src/types/clerc.d.ts
|
|
@@ -231,17 +233,16 @@ type Parameter = string | ParameterDefinition;
|
|
|
231
233
|
//#endregion
|
|
232
234
|
//#region src/types/context.d.ts
|
|
233
235
|
type AddStringIndex<T> = T & Record<string, any>;
|
|
234
|
-
type
|
|
236
|
+
type KnownKeys<T> = string extends keyof T ? never : keyof T;
|
|
237
|
+
type InferFlagsWithGlobal<C extends Command, GF extends ClercFlagsDefinition> = AddStringIndex<InferFlags<NonNullable<C["flags"]> & Omit<GF, KnownKeys<NonNullable<C["flags"]>>>>>;
|
|
235
238
|
interface ContextStore {}
|
|
236
239
|
interface BaseContext<C extends Command = Command, GF extends ClercFlagsDefinition = {}> {
|
|
237
|
-
resolved: boolean;
|
|
238
240
|
command?: C;
|
|
239
241
|
calledAs?: string;
|
|
240
242
|
parameters: InferParameters<NonNullable<C["parameters"]>>;
|
|
241
243
|
flags: InferFlagsWithGlobal<C, GF>;
|
|
242
244
|
ignored: string[];
|
|
243
245
|
rawParsed: ParsedResult<InferFlagsWithGlobal<C, GF>>;
|
|
244
|
-
missingParameters: boolean;
|
|
245
246
|
store: Partial<ContextStore>;
|
|
246
247
|
}
|
|
247
248
|
//#endregion
|
|
@@ -318,9 +319,9 @@ declare class Clerc<Commands extends CommandsRecord = {}, GlobalFlags extends Cl
|
|
|
318
319
|
version(version: string): this;
|
|
319
320
|
use(plugin: Plugin): this;
|
|
320
321
|
errorHandler(handler: ErrorHandler): this;
|
|
321
|
-
command<Name$1 extends string, const Parameters extends readonly Parameter[], Flags extends ClercFlagsDefinition
|
|
322
|
-
command<Name$1 extends string, const Parameters extends readonly Parameter[], Flags extends ClercFlagsDefinition
|
|
323
|
-
command<Name$1 extends string, const Parameters extends readonly Parameter[], Flags extends ClercFlagsDefinition
|
|
322
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[], Flags extends ClercFlagsDefinition>(command: CommandWithHandler<Name$1, Parameters, Flags>): Clerc<Commands & Record<string, CommandWithHandler<Name$1, Parameters, Flags>>, GlobalFlags>;
|
|
323
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[], 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>;
|
|
324
|
+
command<Name$1 extends string, const Parameters extends readonly Parameter[], 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>;
|
|
324
325
|
globalFlag<Name$1 extends string, Flag extends ClercFlagDefinitionValue>(name: Name$1, description: string, options: Flag): Clerc<Commands, GlobalFlags & Record<Name$1, Flag>>;
|
|
325
326
|
globalFlag<Name$1 extends string, Flag extends ClercFlagDefinitionValue>(name: Name$1, options: Flag): Clerc<Commands, GlobalFlags & Record<Name$1, Flag>>;
|
|
326
327
|
interceptor(interceptor: Interceptor<Command, GlobalFlags>): this;
|
|
@@ -349,12 +350,15 @@ declare class MissingRequiredMetadataError extends Error {
|
|
|
349
350
|
declare class InvalidParametersError extends Error {
|
|
350
351
|
constructor(message: string);
|
|
351
352
|
}
|
|
353
|
+
declare class MissingRequiredFlagError extends Error {
|
|
354
|
+
constructor(flags: string[]);
|
|
355
|
+
}
|
|
352
356
|
declare namespace flag_types_d_exports {
|
|
353
357
|
export { Enum, Range, Regex };
|
|
354
358
|
}
|
|
355
359
|
//#endregion
|
|
356
360
|
//#region src/helpers.d.ts
|
|
357
|
-
declare const defineCommand: <Name$1 extends string, const Parameters extends readonly Parameter[], Flags extends ClercFlagsDefinition
|
|
361
|
+
declare const defineCommand: <Name$1 extends string, const Parameters extends readonly Parameter[], Flags extends ClercFlagsDefinition>(command: Command<Name$1, Parameters, Flags>, handler?: NoInfer<CommandHandler<Command<Name$1, Parameters, Flags>>>) => CommandWithHandler<Name$1, Parameters, Flags>;
|
|
358
362
|
//#endregion
|
|
359
363
|
//#region src/ignore.d.ts
|
|
360
364
|
declare function createStopAtFirstParameter(): IgnoreFunction;
|
|
@@ -554,4 +558,4 @@ declare const Cli: (options?: CreateOptions$1) => Clerc$1;
|
|
|
554
558
|
//#region src/utils.d.ts
|
|
555
559
|
declare const normalizeFlagValue: (flag: index_d_exports$1.ClercFlagDefinitionValue) => index_d_exports$1.ClercFlagOptions;
|
|
556
560
|
//#endregion
|
|
557
|
-
export { BaseContext, Clerc, ClercFlagDefinitionValue, ClercFlagOptions, ClercFlagsDefinition, Command, CommandCustomOptions, CommandHandler, CommandHandlerContext, CommandOptions, CommandWithHandler, CommandsMap, CommandsRecord, 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, normalizeFlagValue, resolveCommand };
|
|
561
|
+
export { BaseContext, Clerc, ClercFlagDefinitionValue, ClercFlagOptions, ClercFlagsDefinition, Command, CommandCustomOptions, CommandHandler, CommandHandlerContext, CommandOptions, CommandWithHandler, CommandsMap, CommandsRecord, ContextStore, type CreateOptions, DOUBLE_DASH, ErrorHandler, FlagCustomOptions, InferParameters, Interceptor, InterceptorContext, InterceptorHandler, InterceptorNext, InterceptorObject, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MakeEmitterEvents, MissingRequiredFlagError, 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, normalizeFlagValue, resolveCommand };
|
package/dist/index.js
CHANGED
|
@@ -64,6 +64,12 @@ var InvalidParametersError = class extends Error {
|
|
|
64
64
|
super(message);
|
|
65
65
|
}
|
|
66
66
|
};
|
|
67
|
+
var MissingRequiredFlagError = class extends Error {
|
|
68
|
+
constructor(flags) {
|
|
69
|
+
const s = flags.length > 1 ? "s" : "";
|
|
70
|
+
super(`Missing required flag${s}: ${flags.join(", ")}`);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
67
73
|
|
|
68
74
|
//#endregion
|
|
69
75
|
//#region src/interceptor.ts
|
|
@@ -304,19 +310,18 @@ var Clerc = class Clerc {
|
|
|
304
310
|
parametersError = e;
|
|
305
311
|
}
|
|
306
312
|
const context = {
|
|
307
|
-
resolved: !!command,
|
|
308
313
|
command,
|
|
309
314
|
calledAs,
|
|
310
315
|
parameters,
|
|
311
316
|
flags: parsed.flags,
|
|
312
317
|
ignored: parsed.ignored,
|
|
313
318
|
rawParsed: parsed,
|
|
314
|
-
missingParameters: !!parametersError,
|
|
315
319
|
store: { ...this.#store }
|
|
316
320
|
};
|
|
317
321
|
const emitInterceptor = {
|
|
318
322
|
enforce: "post",
|
|
319
323
|
handler: async (ctx) => {
|
|
324
|
+
if (parsed.missingRequiredFlags.length > 0) throw new MissingRequiredFlagError(parsed.missingRequiredFlags);
|
|
320
325
|
if (parametersError) throw parametersError;
|
|
321
326
|
if (command) await this.#emitter.emit(command.name, ctx);
|
|
322
327
|
else throw parametersToResolve.length > 0 ? new NoSuchCommandError(parametersToResolve.join(" ")) : new NoCommandSpecifiedError();
|
|
@@ -372,4 +377,4 @@ const definePlugin = (plugin) => plugin;
|
|
|
372
377
|
const normalizeFlagValue = (flag) => typeof flag === "function" || looseIsArray(flag) ? { type: flag } : flag;
|
|
373
378
|
|
|
374
379
|
//#endregion
|
|
375
|
-
export { Clerc, DOUBLE_DASH, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, flag_types_exports as Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, normalizeFlagValue, resolveCommand };
|
|
380
|
+
export { Clerc, DOUBLE_DASH, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, flag_types_exports as Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, normalizeFlagValue, 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.27",
|
|
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": "^4.0.0",
|
|
48
|
-
"@clerc/parser": "^1.0.0-beta.
|
|
48
|
+
"@clerc/parser": "^1.0.0-beta.27"
|
|
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.27"
|
|
53
53
|
}
|
|
54
54
|
}
|