@clerc/core 1.0.3 → 1.1.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/README.md CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  [![NPM version](https://img.shields.io/npm/v/@clerc/core?color=a1b858&label=)](https://www.npmjs.com/package/@clerc/core)
4
4
 
5
- See [Clerc](https://github.com/clercjs/clerc) for documentation.
5
+ ## Documenation
6
+
7
+ Read the [documentation](https://clerc.so1ve.dev) for more details.
6
8
 
7
9
  ## 📝 License
8
10
 
@@ -0,0 +1,18 @@
1
+ //#region rolldown:runtime
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (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 { __exportAll as t };
@@ -1,8 +1,51 @@
1
- import * as Types from "@clerc/advanced-types";
2
1
  import * as Parser from "@clerc/parser";
3
2
  import { DOUBLE_DASH, FlagOptions, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, PARAMETER, ParsedResult, TypeFunction, TypeValue, UNKNOWN_FLAG } from "@clerc/parser";
4
3
  import { CamelCase, DeepPrettify, LiteralUnion, MaybeArray, PartialRequired, Prettify, UnionToIntersection } from "@clerc/utils";
5
4
 
5
+ //#region ../advanced-types/src/errors.d.ts
6
+ declare class FlagValidationError extends Error {}
7
+ declare namespace index_d_exports {
8
+ export { Enum, FlagValidationError, Range, Regex };
9
+ }
10
+ /**
11
+ * Creates a Enum type function that validates the input against allowed values.
12
+ * The display name will be formatted as "value1 | value2 | ..." for help
13
+ * output.
14
+ *
15
+ * @example
16
+ *
17
+ * ```typescript
18
+ * const format = Enum(["json", "yaml", "xml"]);
19
+ * // Help output will show: json | yaml | xml
20
+ * ```
21
+ *
22
+ * @param values - Array of allowed string values
23
+ * @returns A TypeFunction that validates and returns the input value
24
+ * @throws {Error} If the value is not in the allowed values list
25
+ */
26
+ declare function Enum<T extends string>(...values: T[]): TypeFunction<T>;
27
+ /**
28
+ * Creates a range type function that validates the input is a number within the
29
+ * specified range.
30
+ *
31
+ * @param min - The minimum acceptable value (inclusive)
32
+ * @param max - The maximum acceptable value (inclusive)
33
+ * @returns A TypeFunction that validates the input value
34
+ * @throws {Error} If the value is not a number or is outside the specified
35
+ * range
36
+ */
37
+ declare function Range(min: number, max: number): TypeFunction<number>;
38
+ /**
39
+ * Creates a regex type function that validates the input against the provided
40
+ * pattern.
41
+ *
42
+ * @param pattern - The regular expression pattern to validate against
43
+ * @param description - Optional description for display purposes
44
+ * @returns A TypeFunction that validates the input value
45
+ * @throws {Error} If the value does not match the regex pattern
46
+ */
47
+ declare function Regex(pattern: RegExp, description?: string): TypeFunction<string>;
48
+ //#endregion
6
49
  //#region src/types/clerc.d.ts
7
50
  type ErrorHandler = (error: unknown) => void;
8
51
  //#endregion
@@ -48,7 +91,8 @@ interface CommandOptions<Parameters extends readonly ParameterDefinitionValue[]
48
91
  parameters?: Parameters;
49
92
  flags?: Flags;
50
93
  /**
51
- * A callback function to conditionally stop parsing. When it returns true, parsing stops and remaining arguments are preserved in ignored.
94
+ * A callback function to conditionally stop parsing. When it returns true,
95
+ * parsing stops and remaining arguments are preserved in ignored.
52
96
  */
53
97
  ignore?: IgnoreFunction;
54
98
  }
@@ -70,8 +114,7 @@ type CommandHandler<C extends Command = Command, GF extends ClercFlagsDefinition
70
114
  //#region src/types/interceptor.d.ts
71
115
  type InterceptorContext<C extends Command = Command, GF extends ClercFlagsDefinition = {}> = DeepPrettify<BaseContext<C, GF>>;
72
116
  /**
73
- * Function to call the next interceptor in the chain.
74
- * **MUST** be awaited.
117
+ * Function to call the next interceptor in the chain. **MUST** be awaited.
75
118
  */
76
119
  type InterceptorNext = () => void | Promise<void>;
77
120
  type InterceptorHandler<C extends Command = Command, GF extends ClercFlagsDefinition = {}> = (context: InterceptorContext<C, GF>, next: InterceptorNext) => void | Promise<void>;
@@ -167,4 +210,4 @@ declare const definePlugin: (plugin: Plugin) => Plugin;
167
210
  declare const normalizeFlagValue: (flag: ClercFlagDefinitionValue) => ClercFlagOptions;
168
211
  declare const normalizeParameterValue: (parameter: ParameterDefinitionValue) => ParameterOptions;
169
212
  //#endregion
170
- 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, ParameterCustomOptions, ParameterDefinitionValue, ParameterOptions, type ParseOptions, type Parser, Plugin, Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, extractParameterInfo, normalizeFlagValue, normalizeParameterValue, resolveCommand };
213
+ 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, ParameterCustomOptions, ParameterDefinitionValue, ParameterOptions, type ParseOptions, type Parser, Plugin, index_d_exports as Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, extractParameterInfo, normalizeFlagValue, normalizeParameterValue, resolveCommand };
@@ -1,8 +1,81 @@
1
- import * as Types from "@clerc/advanced-types";
1
+ import { t as __exportAll } from "./chunk-15K8U1wQ.mjs";
2
2
  import { DOUBLE_DASH, DOUBLE_DASH as DOUBLE_DASH$1, InvalidSchemaError, KNOWN_FLAG, PARAMETER, PARAMETER as PARAMETER$1, UNKNOWN_FLAG, parse } from "@clerc/parser";
3
3
  import { camelCase, looseIsArray, toArray } from "@clerc/utils";
4
4
  import { LiteEmit } from "lite-emit";
5
5
 
6
+ //#region ../advanced-types/src/errors.ts
7
+ var FlagValidationError = class extends Error {};
8
+
9
+ //#endregion
10
+ //#region ../advanced-types/src/index.ts
11
+ var src_exports = /* @__PURE__ */ __exportAll({
12
+ Enum: () => Enum,
13
+ FlagValidationError: () => FlagValidationError,
14
+ Range: () => Range,
15
+ Regex: () => Regex
16
+ });
17
+ /**
18
+ * Creates a Enum type function that validates the input against allowed values.
19
+ * The display name will be formatted as "value1 | value2 | ..." for help
20
+ * output.
21
+ *
22
+ * @example
23
+ *
24
+ * ```typescript
25
+ * const format = Enum(["json", "yaml", "xml"]);
26
+ * // Help output will show: json | yaml | xml
27
+ * ```
28
+ *
29
+ * @param values - Array of allowed string values
30
+ * @returns A TypeFunction that validates and returns the input value
31
+ * @throws {Error} If the value is not in the allowed values list
32
+ */
33
+ function Enum(...values) {
34
+ const fn = ((value) => {
35
+ if (!values.includes(value)) throw new FlagValidationError(`Invalid value: ${value}. Must be one of: ${values.join(", ")}`);
36
+ return value;
37
+ });
38
+ fn.display = values.join(" | ");
39
+ return fn;
40
+ }
41
+ /**
42
+ * Creates a range type function that validates the input is a number within the
43
+ * specified range.
44
+ *
45
+ * @param min - The minimum acceptable value (inclusive)
46
+ * @param max - The maximum acceptable value (inclusive)
47
+ * @returns A TypeFunction that validates the input value
48
+ * @throws {Error} If the value is not a number or is outside the specified
49
+ * range
50
+ */
51
+ function Range(min, max) {
52
+ const fn = ((value) => {
53
+ const num = Number(value);
54
+ if (Number.isNaN(num) || num < min || num > max) throw new FlagValidationError(`Invalid value: ${value}. Must be a number between ${min} and ${max}`);
55
+ return num;
56
+ });
57
+ fn.display = `${min} - ${max}`;
58
+ return fn;
59
+ }
60
+ /**
61
+ * Creates a regex type function that validates the input against the provided
62
+ * pattern.
63
+ *
64
+ * @param pattern - The regular expression pattern to validate against
65
+ * @param description - Optional description for display purposes
66
+ * @returns A TypeFunction that validates the input value
67
+ * @throws {Error} If the value does not match the regex pattern
68
+ */
69
+ function Regex(pattern, description) {
70
+ const fn = ((value) => {
71
+ if (!pattern.test(value)) throw new FlagValidationError(`Invalid value: ${value}. Must match pattern: ${pattern}`);
72
+ return value;
73
+ });
74
+ fn.display = description ?? `Regex: ${pattern.toString()}`;
75
+ return fn;
76
+ }
77
+
78
+ //#endregion
6
79
  //#region src/command.ts
7
80
  function resolveCommand(commandsMap, parameters) {
8
81
  for (let i = parameters.length; i >= 0; i--) {
@@ -347,4 +420,4 @@ function createStopAtFirstParameter() {
347
420
  const definePlugin = (plugin) => plugin;
348
421
 
349
422
  //#endregion
350
- export { Clerc, DOUBLE_DASH, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, extractParameterInfo, normalizeFlagValue, normalizeParameterValue, resolveCommand };
423
+ export { Clerc, DOUBLE_DASH, InvalidCommandError, InvalidParametersError, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, MissingRequiredMetadataError, NoCommandSpecifiedError, NoSuchCommandError, PARAMETER, src_exports as Types, UNKNOWN_FLAG, createStopAtFirstParameter, defineCommand, definePlugin, extractParameterInfo, normalizeFlagValue, normalizeParameterValue, resolveCommand };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/core",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "author": "Ray <i@mk1.io> (https://github.com/so1ve)",
5
5
  "type": "module",
6
6
  "description": "Clerc core",
@@ -24,19 +24,12 @@
24
24
  "license": "MIT",
25
25
  "sideEffects": false,
26
26
  "exports": {
27
- ".": "./dist/index.js"
28
- },
29
- "main": "./dist/index.js",
30
- "module": "./dist/index.js",
31
- "types": "dist/index.d.ts",
32
- "typesVersions": {
33
- "*": {
34
- "*": [
35
- "./dist/*",
36
- "./dist/index.d.ts"
37
- ]
38
- }
27
+ ".": "./dist/index.mjs",
28
+ "./package.json": "./package.json"
39
29
  },
30
+ "main": "./dist/index.mjs",
31
+ "module": "./dist/index.mjs",
32
+ "types": "./dist/index.d.mts",
40
33
  "files": [
41
34
  "dist"
42
35
  ],
@@ -45,11 +38,11 @@
45
38
  },
46
39
  "dependencies": {
47
40
  "lite-emit": "^4.0.0",
48
- "@clerc/advanced-types": "1.0.3",
49
- "@clerc/parser": "1.0.3",
50
- "@clerc/utils": "1.0.3"
41
+ "@clerc/parser": "1.1.0",
42
+ "@clerc/utils": "1.1.0"
51
43
  },
52
44
  "devDependencies": {
53
- "is-platform": "^1.0.0"
45
+ "is-platform": "^1.0.0",
46
+ "@clerc/advanced-types": "1.1.0"
54
47
  }
55
48
  }