@clerc/core 1.0.0-beta.24 → 1.0.0-beta.26

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
@@ -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,20 +59,22 @@ 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
- interface BaseFlagOptions<T extends TypeValue = TypeValue> {
62
+ type Foo = 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> = Foo & {
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].
68
72
  * e.g., String, Number, [String], (val) => val.split(',')
69
73
  */
70
74
  type: T;
71
- /** Aliases for the flag. */
72
- alias?: MaybeArray<string>;
73
- /** The default value of the flag. */
74
- default?: unknown;
75
- /** Whether the flag is required. */
76
- required?: boolean;
77
- }
75
+ /** Short flag alias (single character). */
76
+ short?: string;
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, MissingRequiredFlagError, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, Range, RawInputType, Regex, TypeFunction, TypeValue, UNKNOWN_FLAG, createParser, parse };
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
@@ -234,14 +236,12 @@ type AddStringIndex<T> = T & Record<string, any>;
234
236
  type InferFlagsWithGlobal<C extends Command, GF extends ClercFlagsDefinition> = AddStringIndex<InferFlags<NonNullable<C["flags"]> & Omit<GF, keyof NonNullable<C["flags"]>>>>;
235
237
  interface ContextStore {}
236
238
  interface BaseContext<C extends Command = Command, GF extends ClercFlagsDefinition = {}> {
237
- resolved: boolean;
238
239
  command?: C;
239
240
  calledAs?: string;
240
241
  parameters: InferParameters<NonNullable<C["parameters"]>>;
241
242
  flags: InferFlagsWithGlobal<C, GF>;
242
243
  ignored: string[];
243
244
  rawParsed: ParsedResult<InferFlagsWithGlobal<C, GF>>;
244
- missingParameters: boolean;
245
245
  store: Partial<ContextStore>;
246
246
  }
247
247
  //#endregion
@@ -349,6 +349,9 @@ declare class MissingRequiredMetadataError extends Error {
349
349
  declare class InvalidParametersError extends Error {
350
350
  constructor(message: string);
351
351
  }
352
+ declare class MissingRequiredFlagError extends Error {
353
+ constructor(flags: string[]);
354
+ }
352
355
  declare namespace flag_types_d_exports {
353
356
  export { Enum, Range, Regex };
354
357
  }
@@ -378,14 +381,7 @@ declare module "@clerc/core" {
378
381
  };
379
382
  }
380
383
  }
381
- interface CompletionsPluginOptions {
382
- /**
383
- * Whether to register the `completions install` and `completions uninstall` commands.
384
- * @default true
385
- */
386
- managementCommands?: boolean;
387
- }
388
- declare const completionsPlugin: (options?: CompletionsPluginOptions) => Plugin$1;
384
+ declare const completionsPlugin: () => Plugin$1;
389
385
  //#endregion
390
386
  //#region ../plugin-friendly-error/src/index.d.ts
391
387
  interface FriendlyErrorPluginOptions {
@@ -550,15 +546,15 @@ declare const versionPlugin: ({
550
546
  flag
551
547
  }?: VersionPluginOptions) => Plugin$1;
552
548
  declare namespace re_exports_d_exports {
553
- export { CommandHelpOptions, CompletionsPluginOptions, FriendlyErrorPluginOptions, GroupDefinition, GroupsOptions, HelpOptions, HelpPluginOptions, completionsPlugin, defaultFormatters, friendlyErrorPlugin, helpPlugin, notFoundPlugin, strictFlagsPlugin, versionPlugin };
549
+ export { CommandHelpOptions, FriendlyErrorPluginOptions, GroupDefinition, GroupsOptions, HelpOptions, HelpPluginOptions, completionsPlugin, defaultFormatters, friendlyErrorPlugin, helpPlugin, notFoundPlugin, strictFlagsPlugin, versionPlugin };
554
550
  }
555
551
  import * as import___clerc_core from "@clerc/core";
556
552
  declare namespace index_d_exports$1 {
557
- export { Cli, CommandHelpOptions, CompletionsPluginOptions, FriendlyErrorPluginOptions, GroupDefinition, GroupsOptions, HelpOptions, HelpPluginOptions, completionsPlugin, defaultFormatters, friendlyErrorPlugin, helpPlugin, notFoundPlugin, strictFlagsPlugin, versionPlugin };
553
+ export { Cli, CommandHelpOptions, FriendlyErrorPluginOptions, GroupDefinition, GroupsOptions, HelpOptions, HelpPluginOptions, completionsPlugin, defaultFormatters, friendlyErrorPlugin, helpPlugin, notFoundPlugin, strictFlagsPlugin, versionPlugin };
558
554
  }
559
555
  declare const Cli: (options?: CreateOptions$1) => Clerc$1;
560
556
  //#endregion
561
557
  //#region src/utils.d.ts
562
558
  declare const normalizeFlagValue: (flag: index_d_exports$1.ClercFlagDefinitionValue) => index_d_exports$1.ClercFlagOptions;
563
559
  //#endregion
564
- 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 };
560
+ 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.24",
3
+ "version": "1.0.0-beta.26",
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.24"
48
+ "@clerc/parser": "^1.0.0-beta.26"
49
49
  },
50
50
  "devDependencies": {
51
51
  "is-platform": "^1.0.0",
52
- "@clerc/utils": "1.0.0-beta.24"
52
+ "@clerc/utils": "1.0.0-beta.26"
53
53
  }
54
54
  }