@clerc/core 1.0.2 → 1.0.3
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 +7 -215
- package/dist/index.js +1 -25
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,216 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
//#endregion
|
|
7
|
-
//#region ../utils/src/types/type-fest.d.ts
|
|
8
|
-
type LiteralUnion<LiteralType, BaseType> = LiteralType | (BaseType & Record<never, never>);
|
|
9
|
-
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
10
|
-
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
|
|
11
|
-
type BuiltIns = Primitive | void | Date | RegExp;
|
|
12
|
-
type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown) | Promise<unknown>;
|
|
13
|
-
type UnknownArray = readonly unknown[];
|
|
14
|
-
type MapsSetsOrArrays = ReadonlyMap<unknown, unknown> | WeakMap<WeakKey, unknown> | ReadonlySet<unknown> | WeakSet<WeakKey> | UnknownArray;
|
|
15
|
-
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;
|
|
16
|
-
type DeepPrettify<T, E = never> = ConditionalDeepPrettify<T, E | NonRecursiveType | MapsSetsOrArrays, object>;
|
|
17
|
-
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
18
|
-
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>);
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region ../utils/src/types/index.d.ts
|
|
21
|
-
type MaybeArray<T> = T | T[];
|
|
22
|
-
type PartialRequired<T, K$1 extends keyof T> = T & { [P in K$1]-?: T[P] };
|
|
23
|
-
type UnionToIntersection<U$1> = (U$1 extends any ? (k: U$1) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
24
|
-
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;
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region ../parser/src/types.d.ts
|
|
27
|
-
interface FlagDefaultValueFunction<T> {
|
|
28
|
-
(): T;
|
|
29
|
-
display?: string;
|
|
30
|
-
}
|
|
31
|
-
type FlagDefaultValue<T = unknown> = T | FlagDefaultValueFunction<T>;
|
|
32
|
-
/**
|
|
33
|
-
* Defines how a string input is converted to the target type T.
|
|
34
|
-
*
|
|
35
|
-
* @template T The target type.
|
|
36
|
-
*/
|
|
37
|
-
interface TypeFunction<T = unknown> {
|
|
38
|
-
(value: string): T;
|
|
39
|
-
/**
|
|
40
|
-
* Optional display name for the type, useful in help output.
|
|
41
|
-
* If provided, this will be shown instead of the function name.
|
|
42
|
-
*/
|
|
43
|
-
display?: string;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* A callback function to conditionally stop parsing.
|
|
47
|
-
* When it returns true, parsing stops and remaining arguments are preserved in `ignored`.
|
|
48
|
-
*
|
|
49
|
-
* @param type - The type of the current argument: 'known-flag' or 'unknown-flag' for flags, 'parameter' for positional arguments
|
|
50
|
-
* @param arg - The current argument being processed
|
|
51
|
-
* @returns true to stop parsing, false to continue
|
|
52
|
-
*/
|
|
53
|
-
type IgnoreFunction = (type: typeof KNOWN_FLAG | typeof UNKNOWN_FLAG | typeof PARAMETER, arg: string) => boolean;
|
|
54
|
-
type TypeValue<T = unknown> = TypeFunction<T> | readonly [TypeFunction<T>];
|
|
55
|
-
type FlagRequiredOrDefault = RequireExactlyOneOrNone<{
|
|
56
|
-
/** The default value of the flag. */
|
|
57
|
-
default?: unknown;
|
|
58
|
-
/** Whether the flag is required. */
|
|
59
|
-
required?: boolean;
|
|
60
|
-
}, "default" | "required">;
|
|
61
|
-
type BaseFlagOptions<T extends TypeValue = TypeValue> = FlagRequiredOrDefault & {
|
|
62
|
-
/**
|
|
63
|
-
* The type constructor or a function to convert the string value.
|
|
64
|
-
* To support multiple occurrences of a flag (e.g., --file a --file b), wrap the type in an array: [String], [Number].
|
|
65
|
-
* e.g., String, Number, [String], (val) => val.split(',')
|
|
66
|
-
*/
|
|
67
|
-
type: T;
|
|
68
|
-
/** Short flag alias (single character). */
|
|
69
|
-
short?: string;
|
|
70
|
-
};
|
|
71
|
-
type FlagOptions = (BaseFlagOptions<BooleanConstructor> & {
|
|
72
|
-
/**
|
|
73
|
-
* Whether to enable the `--no-<flag>` syntax to set the value to false.
|
|
74
|
-
* Only useful for boolean flags.
|
|
75
|
-
* When set on a non-boolean flag, a type error will be shown.
|
|
76
|
-
*
|
|
77
|
-
* @default true
|
|
78
|
-
*/
|
|
79
|
-
negatable?: boolean;
|
|
80
|
-
}) | (BaseFlagOptions & {
|
|
81
|
-
negatable?: never;
|
|
82
|
-
});
|
|
83
|
-
type FlagDefinitionValue = FlagOptions | TypeValue;
|
|
84
|
-
type FlagsDefinition = Record<string, FlagDefinitionValue>;
|
|
85
|
-
/**
|
|
86
|
-
* Configuration options for the parser.
|
|
87
|
-
*/
|
|
88
|
-
interface ParserOptions<T extends FlagsDefinition = {}> {
|
|
89
|
-
/**
|
|
90
|
-
* Detailed configuration for flags.
|
|
91
|
-
* Supports the full object syntax or a type constructor as a shorthand.
|
|
92
|
-
* The key is the flag name (e.g., "file" for "--file").
|
|
93
|
-
*/
|
|
94
|
-
flags?: T;
|
|
95
|
-
/**
|
|
96
|
-
* Delimiters to split flag names and values.
|
|
97
|
-
*
|
|
98
|
-
* @default ['=', ':']
|
|
99
|
-
*/
|
|
100
|
-
delimiters?: string[];
|
|
101
|
-
/**
|
|
102
|
-
* A callback function to conditionally stop parsing.
|
|
103
|
-
* When it returns true, parsing stops and remaining arguments are preserved in `ignored`.
|
|
104
|
-
*/
|
|
105
|
-
ignore?: IgnoreFunction;
|
|
106
|
-
}
|
|
107
|
-
type RawInputType = string | boolean;
|
|
108
|
-
interface ObjectInputType {
|
|
109
|
-
[key: string]: RawInputType | ObjectInputType;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* The parsed result.
|
|
113
|
-
* @template TFlags The specific flags type inferred from ParserOptions.
|
|
114
|
-
*/
|
|
115
|
-
interface ParsedResult<TFlags extends Record<string, any>> {
|
|
116
|
-
/** Positional arguments or commands. */
|
|
117
|
-
parameters: string[];
|
|
118
|
-
/** Arguments after the `--` delimiter. */
|
|
119
|
-
doubleDash: string[];
|
|
120
|
-
/**
|
|
121
|
-
* The parsed flags.
|
|
122
|
-
* This is a strongly-typed object whose structure is inferred from the `flags` configuration in ParserOptions.
|
|
123
|
-
*/
|
|
124
|
-
flags: TFlags;
|
|
125
|
-
/** The raw command-line arguments. */
|
|
126
|
-
raw: string[];
|
|
127
|
-
/** Unknown flags encountered during parsing. */
|
|
128
|
-
unknown: Record<string, RawInputType>;
|
|
129
|
-
/** Arguments that were not parsed due to ignore callback. */
|
|
130
|
-
ignored: string[];
|
|
131
|
-
/** List of required flags that were not provided. */
|
|
132
|
-
missingRequiredFlags: string[];
|
|
133
|
-
}
|
|
134
|
-
type InferFlagDefault<T extends FlagDefinitionValue, Fallback> = T extends {
|
|
135
|
-
default: FlagDefaultValue<infer DefaultType>;
|
|
136
|
-
} ? DefaultType : Fallback;
|
|
137
|
-
type IsTypeAny<T extends FlagDefinitionValue> = IsAny<T> extends true ? true : T extends {
|
|
138
|
-
type: infer Type;
|
|
139
|
-
} ? IsAny<Type> extends true ? true : false : false;
|
|
140
|
-
type _InferFlags<T extends FlagsDefinition> = { [K in keyof T]: IsTypeAny<T[K]> extends true ? any : T[K] extends readonly [BooleanConstructor] | {
|
|
141
|
-
type: readonly [BooleanConstructor];
|
|
142
|
-
} ? number | InferFlagDefault<T[K], never> : T[K] extends ObjectConstructor | {
|
|
143
|
-
type: ObjectConstructor;
|
|
144
|
-
} ? ObjectInputType | InferFlagDefault<T[K], never> : T[K] extends readonly [TypeValue<infer U>] | {
|
|
145
|
-
type: readonly [TypeValue<infer U>];
|
|
146
|
-
} ? U[] | InferFlagDefault<T[K], never> : T[K] extends TypeValue<infer U> | {
|
|
147
|
-
type: TypeValue<infer U>;
|
|
148
|
-
} ? U | InferFlagDefault<T[K], [U] extends [boolean] ? never : T[K] extends {
|
|
149
|
-
required: true;
|
|
150
|
-
} ? never : undefined> : never };
|
|
151
|
-
/**
|
|
152
|
-
* An advanced utility type that infers the exact type of the `flags` object in the parsed result,
|
|
153
|
-
* based on the provided `flags` configuration object T.
|
|
154
|
-
*
|
|
155
|
-
* @template T The type of the flags configuration object.
|
|
156
|
-
*/
|
|
157
|
-
type InferFlags<T extends FlagsDefinition> = Prettify<_InferFlags<T>>;
|
|
158
|
-
//#endregion
|
|
159
|
-
//#region ../parser/src/iterator.d.ts
|
|
160
|
-
declare const KNOWN_FLAG = "known-flag";
|
|
161
|
-
declare const UNKNOWN_FLAG = "unknown-flag";
|
|
162
|
-
declare const PARAMETER = "parameter";
|
|
163
|
-
//#endregion
|
|
164
|
-
//#region ../parser/src/parse.d.ts
|
|
165
|
-
declare const DOUBLE_DASH = "--";
|
|
166
|
-
type ParseFunction<T extends FlagsDefinition> = (args: string[]) => ParsedResult<InferFlags<T>>;
|
|
167
|
-
declare function createParser<T extends FlagsDefinition>(options?: ParserOptions<T>): {
|
|
168
|
-
parse: ParseFunction<T>;
|
|
169
|
-
};
|
|
170
|
-
declare const parse: <T extends FlagsDefinition>(args: string[], options?: ParserOptions<T>) => ParsedResult<InferFlags<T>>;
|
|
171
|
-
declare namespace index_d_exports {
|
|
172
|
-
export { BaseFlagOptions, DOUBLE_DASH, FlagDefaultValue, FlagDefaultValueFunction, FlagDefinitionValue, FlagOptions, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, RawInputType, TypeFunction, TypeValue, UNKNOWN_FLAG, createParser, parse };
|
|
173
|
-
}
|
|
174
|
-
//#endregion
|
|
175
|
-
//#region ../advanced-types/src/errors.d.ts
|
|
176
|
-
declare class FlagValidationError extends Error {}
|
|
177
|
-
declare namespace index_d_exports$1 {
|
|
178
|
-
export { Enum, FlagValidationError, Range, Regex };
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Creates a Enum type function that validates the input against allowed values.
|
|
182
|
-
* The display name will be formatted as "value1 | value2 | ..." for help output.
|
|
183
|
-
*
|
|
184
|
-
* @param values - Array of allowed string values
|
|
185
|
-
* @returns A TypeFunction that validates and returns the input value
|
|
186
|
-
* @throws {Error} If the value is not in the allowed values list
|
|
187
|
-
*
|
|
188
|
-
* @example
|
|
189
|
-
* ```typescript
|
|
190
|
-
* const format = Enum(['json', 'yaml', 'xml']);
|
|
191
|
-
* // Help output will show: json | yaml | xml
|
|
192
|
-
* ```
|
|
193
|
-
*/
|
|
194
|
-
declare function Enum<T extends string>(...values: T[]): TypeFunction<T>;
|
|
195
|
-
/**
|
|
196
|
-
* Creates a range type function that validates the input is a number within the specified range.
|
|
197
|
-
*
|
|
198
|
-
* @param min - The minimum acceptable value (inclusive)
|
|
199
|
-
* @param max - The maximum acceptable value (inclusive)
|
|
200
|
-
* @returns A TypeFunction that validates the input value
|
|
201
|
-
* @throws {Error} If the value is not a number or is outside the specified range
|
|
202
|
-
*/
|
|
203
|
-
declare function Range(min: number, max: number): TypeFunction<number>;
|
|
204
|
-
/**
|
|
205
|
-
* Creates a regex type function that validates the input against the provided pattern.
|
|
206
|
-
*
|
|
207
|
-
* @param pattern - The regular expression pattern to validate against
|
|
208
|
-
* @param description - Optional description for display purposes
|
|
209
|
-
* @returns A TypeFunction that validates the input value
|
|
210
|
-
* @throws {Error} If the value does not match the regex pattern
|
|
211
|
-
*/
|
|
212
|
-
declare function Regex(pattern: RegExp, description?: string): TypeFunction<string>;
|
|
213
|
-
//#endregion
|
|
1
|
+
import * as Types from "@clerc/advanced-types";
|
|
2
|
+
import * as Parser from "@clerc/parser";
|
|
3
|
+
import { DOUBLE_DASH, FlagOptions, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, PARAMETER, ParsedResult, TypeFunction, TypeValue, UNKNOWN_FLAG } from "@clerc/parser";
|
|
4
|
+
import { CamelCase, DeepPrettify, LiteralUnion, MaybeArray, PartialRequired, Prettify, UnionToIntersection } from "@clerc/utils";
|
|
5
|
+
|
|
214
6
|
//#region src/types/clerc.d.ts
|
|
215
7
|
type ErrorHandler = (error: unknown) => void;
|
|
216
8
|
//#endregion
|
|
@@ -224,7 +16,7 @@ type ClercFlagsDefinition = Record<string, ClercFlagDefinitionValue>;
|
|
|
224
16
|
//#endregion
|
|
225
17
|
//#region src/types/parameter.d.ts
|
|
226
18
|
interface ParameterCustomOptions {}
|
|
227
|
-
type InferStringParameter<T extends string, Type
|
|
19
|
+
type InferStringParameter<T extends string, Type = string> = T extends `<${infer Name extends string}...>` | `[${infer Name extends string}...]` ? Record<CamelCase<Name>, Type[]> : T extends `<${infer Name extends string}>` ? Record<CamelCase<Name>, Type> : T extends `[${infer Name extends string}]` ? Record<CamelCase<Name>, Type | undefined> : never;
|
|
228
20
|
type InferParameter<T extends ParameterDefinitionValue> = T extends string ? InferStringParameter<T> : T extends ParameterOptions ? T["type"] extends TypeFunction<infer U> ? InferStringParameter<T["key"], U> : InferStringParameter<T["key"]> : never;
|
|
229
21
|
type InferParameters<T extends readonly ParameterDefinitionValue[]> = T extends readonly (infer U extends ParameterDefinitionValue)[] ? Prettify<UnionToIntersection<InferParameter<U>>> : never;
|
|
230
22
|
type ParameterOptions = {
|
|
@@ -375,4 +167,4 @@ declare const definePlugin: (plugin: Plugin) => Plugin;
|
|
|
375
167
|
declare const normalizeFlagValue: (flag: ClercFlagDefinitionValue) => ClercFlagOptions;
|
|
376
168
|
declare const normalizeParameterValue: (parameter: ParameterDefinitionValue) => ParameterOptions;
|
|
377
169
|
//#endregion
|
|
378
|
-
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
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,32 +1,8 @@
|
|
|
1
1
|
import * as Types from "@clerc/advanced-types";
|
|
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
|
+
import { camelCase, looseIsArray, toArray } from "@clerc/utils";
|
|
3
4
|
import { LiteEmit } from "lite-emit";
|
|
4
5
|
|
|
5
|
-
//#region ../utils/src/index.ts
|
|
6
|
-
const looseIsArray = (arr) => Array.isArray(arr);
|
|
7
|
-
const toArray = (a) => Array.isArray(a) ? a : [a];
|
|
8
|
-
/**
|
|
9
|
-
* Converts a dash- or space-separated string to camelCase.
|
|
10
|
-
* Not using regexp for better performance, because this function is used in parser.
|
|
11
|
-
*/
|
|
12
|
-
function camelCase(str) {
|
|
13
|
-
const firstIdx = Math.min(str.includes("-") ? str.indexOf("-") : Infinity, str.includes(" ") ? str.indexOf(" ") : Infinity);
|
|
14
|
-
if (firstIdx === Infinity) return str;
|
|
15
|
-
let result = str.slice(0, firstIdx);
|
|
16
|
-
for (let i = firstIdx; i < str.length; i++) if ((str[i] === "-" || str[i] === " ") && i + 1 < str.length) {
|
|
17
|
-
const nextChar = str.charCodeAt(i + 1);
|
|
18
|
-
if (nextChar >= 97 && nextChar <= 122) {
|
|
19
|
-
result += String.fromCharCode(nextChar - 32);
|
|
20
|
-
i++;
|
|
21
|
-
} else {
|
|
22
|
-
result += str[i + 1];
|
|
23
|
-
i++;
|
|
24
|
-
}
|
|
25
|
-
} else if (str[i] !== "-" && str[i] !== " ") result += str[i];
|
|
26
|
-
return result;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
//#endregion
|
|
30
6
|
//#region src/command.ts
|
|
31
7
|
function resolveCommand(commandsMap, parameters) {
|
|
32
8
|
for (let i = parameters.length; i >= 0; i--) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc core",
|
|
@@ -45,11 +45,11 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"lite-emit": "^4.0.0",
|
|
48
|
-
"@clerc/advanced-types": "1.0.
|
|
49
|
-
"@clerc/parser": "1.0.
|
|
48
|
+
"@clerc/advanced-types": "1.0.3",
|
|
49
|
+
"@clerc/parser": "1.0.3",
|
|
50
|
+
"@clerc/utils": "1.0.3"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
|
-
"is-platform": "^1.0.0"
|
|
53
|
-
"@clerc/utils": "1.0.2"
|
|
53
|
+
"is-platform": "^1.0.0"
|
|
54
54
|
}
|
|
55
55
|
}
|