@clerc/plugin-completions 0.10.0 → 0.10.1
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.cjs +3 -2
- package/dist/index.d.ts +259 -2
- package/dist/index.mjs +2 -1
- package/package.json +5 -3
package/dist/index.cjs
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var core = require('@clerc/core');
|
|
6
5
|
var utils = require('@clerc/utils');
|
|
7
6
|
|
|
7
|
+
const definePlugin = (p) => p;
|
|
8
|
+
|
|
8
9
|
const generateCommandCompletion = (name) => `
|
|
9
10
|
${name})
|
|
10
11
|
cmd+="__${name}"
|
|
@@ -94,7 +95,7 @@ const completionMap = {
|
|
|
94
95
|
bash: getBashCompletion,
|
|
95
96
|
pwsh: getPwshCompletion
|
|
96
97
|
};
|
|
97
|
-
const completionsPlugin = (options = {}) =>
|
|
98
|
+
const completionsPlugin = (options = {}) => definePlugin({
|
|
98
99
|
setup(cli) {
|
|
99
100
|
const { command = true } = options;
|
|
100
101
|
if (command) {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,265 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { MaybeArray, Dict, LiteralUnion, CamelCase } from '@clerc/utils';
|
|
2
|
+
|
|
3
|
+
declare const DOUBLE_DASH = "--";
|
|
4
|
+
type TypeFunction<ReturnType = any> = (value: any) => ReturnType;
|
|
5
|
+
type TypeFunctionArray<ReturnType> = readonly [TypeFunction<ReturnType>];
|
|
6
|
+
type FlagType<ReturnType = any> = TypeFunction<ReturnType> | TypeFunctionArray<ReturnType>;
|
|
7
|
+
type FlagSchemaBase<TF> = {
|
|
8
|
+
/**
|
|
9
|
+
Type of the flag as a function that parses the argv string and returns the parsed value.
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
type: String
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
@example Wrap in an array to accept multiple values.
|
|
17
|
+
```
|
|
18
|
+
type: [Boolean]
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
@example Custom function type that uses moment.js to parse string as date.
|
|
22
|
+
```
|
|
23
|
+
type: function CustomDate(value: string) {
|
|
24
|
+
return moment(value).toDate();
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
*/
|
|
28
|
+
type: TF;
|
|
29
|
+
/**
|
|
30
|
+
A single-character alias for the flag.
|
|
31
|
+
|
|
32
|
+
@example
|
|
33
|
+
```
|
|
34
|
+
alias: 's'
|
|
35
|
+
```
|
|
36
|
+
*/
|
|
37
|
+
alias?: string;
|
|
38
|
+
} & Record<PropertyKey, unknown>;
|
|
39
|
+
type FlagSchemaDefault<TF, DefaultType = any> = FlagSchemaBase<TF> & {
|
|
40
|
+
/**
|
|
41
|
+
Default value of the flag. Also accepts a function that returns the default value.
|
|
42
|
+
[Default: undefined]
|
|
43
|
+
|
|
44
|
+
@example
|
|
45
|
+
```
|
|
46
|
+
default: 'hello'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
@example
|
|
50
|
+
```
|
|
51
|
+
default: () => [1, 2, 3]
|
|
52
|
+
```
|
|
53
|
+
*/
|
|
54
|
+
default: DefaultType | (() => DefaultType);
|
|
55
|
+
};
|
|
56
|
+
type FlagSchema<TF = FlagType> = (FlagSchemaBase<TF> | FlagSchemaDefault<TF>);
|
|
57
|
+
type FlagTypeOrSchema<ExtraOptions = Record<string, unknown>> = FlagType | (FlagSchema & ExtraOptions);
|
|
58
|
+
type Flags<ExtraOptions = Record<string, unknown>> = Record<string, FlagTypeOrSchema<ExtraOptions>>;
|
|
59
|
+
type InferFlagType<Flag extends FlagTypeOrSchema> = (Flag extends (TypeFunctionArray<infer T> | FlagSchema<TypeFunctionArray<infer T>>) ? (Flag extends FlagSchemaDefault<TypeFunctionArray<T>, infer D> ? T[] | D : T[]) : (Flag extends TypeFunction<infer T> | FlagSchema<TypeFunction<infer T>> ? (Flag extends FlagSchemaDefault<TypeFunction<T>, infer D> ? T | D : T | undefined) : never));
|
|
60
|
+
interface ParsedFlags<Schemas = Record<string, unknown>> {
|
|
61
|
+
flags: Schemas;
|
|
62
|
+
unknownFlags: Record<string, (string | boolean)[]>;
|
|
63
|
+
_: string[] & {
|
|
64
|
+
[DOUBLE_DASH]: string[];
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
type TypeFlag<Schemas extends Flags> = ParsedFlags<{
|
|
68
|
+
[flag in keyof Schemas]: InferFlagType<Schemas[flag]>;
|
|
69
|
+
}>;
|
|
70
|
+
|
|
71
|
+
type FlagOptions = FlagSchema & {
|
|
72
|
+
description?: string;
|
|
73
|
+
};
|
|
74
|
+
declare interface CommandCustomProperties {
|
|
75
|
+
}
|
|
76
|
+
interface CommandOptions<P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, F extends Dict<FlagOptions> = Dict<FlagOptions>> extends CommandCustomProperties {
|
|
77
|
+
alias?: A;
|
|
78
|
+
parameters?: P;
|
|
79
|
+
flags?: F;
|
|
80
|
+
examples?: [string, string][];
|
|
81
|
+
notes?: string[];
|
|
82
|
+
}
|
|
83
|
+
type Command<N extends string | SingleCommandType = string, O extends CommandOptions = CommandOptions> = O & {
|
|
84
|
+
name: N;
|
|
85
|
+
description: string;
|
|
86
|
+
};
|
|
87
|
+
type CommandWithHandler<N extends string | SingleCommandType = string, O extends CommandOptions = CommandOptions> = Command<N, O> & {
|
|
88
|
+
handler?: HandlerInCommand<Record<N, Command<N, O>>, N>;
|
|
89
|
+
};
|
|
90
|
+
type StripBrackets<Parameter extends string> = (Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? (ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName) : never);
|
|
91
|
+
type ParameterType<Parameter extends string> = (Parameter extends `<${infer _ParameterName}...>` | `[${infer _ParameterName}...]` ? string[] : Parameter extends `<${infer _ParameterName}>` ? string : Parameter extends `[${infer _ParameterName}]` ? string | undefined : never);
|
|
92
|
+
type CommandRecord = Dict<Command> & {
|
|
93
|
+
[SingleCommand]?: Command;
|
|
94
|
+
};
|
|
95
|
+
type NonNullableParameters<T extends string[] | undefined> = T extends undefined ? [] : NonNullable<T>;
|
|
96
|
+
interface HandlerContext<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> {
|
|
97
|
+
name?: N;
|
|
98
|
+
resolved: boolean;
|
|
99
|
+
isSingleCommand: boolean;
|
|
100
|
+
raw: TypeFlag<NonNullable<C[N]["flags"]>> & {
|
|
101
|
+
parameters: string[];
|
|
102
|
+
};
|
|
103
|
+
parameters: {
|
|
104
|
+
[Parameter in [...NonNullableParameters<C[N]["parameters"]>][number] as CamelCase<StripBrackets<Parameter>>]: ParameterType<Parameter>;
|
|
105
|
+
};
|
|
106
|
+
unknownFlags: ParsedFlags["unknownFlags"];
|
|
107
|
+
flags: TypeFlag<NonNullable<C[N]["flags"]>>["flags"];
|
|
108
|
+
cli: Clerc<C>;
|
|
109
|
+
}
|
|
110
|
+
type Handler<C extends CommandRecord = CommandRecord, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K>) => void;
|
|
111
|
+
type HandlerInCommand<C extends CommandRecord = CommandRecord, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K> & {
|
|
112
|
+
name: K;
|
|
113
|
+
}) => void;
|
|
114
|
+
type FallbackType<T, U> = {} extends T ? U : T;
|
|
115
|
+
type InspectorContext<C extends CommandRecord = CommandRecord> = HandlerContext<C> & {
|
|
116
|
+
flags: FallbackType<TypeFlag<NonNullable<C[keyof C]["flags"]>>["flags"], Dict<any>>;
|
|
117
|
+
};
|
|
118
|
+
type Inspector<C extends CommandRecord = CommandRecord> = (ctx: InspectorContext<C>, next: () => void) => void;
|
|
119
|
+
interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
|
|
120
|
+
setup: (cli: T) => U;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
declare const SingleCommand: unique symbol;
|
|
124
|
+
type SingleCommandType = typeof SingleCommand;
|
|
125
|
+
declare class Clerc<C extends CommandRecord = {}> {
|
|
126
|
+
#private;
|
|
127
|
+
private constructor();
|
|
128
|
+
get _name(): string;
|
|
129
|
+
get _description(): string;
|
|
130
|
+
get _version(): string;
|
|
131
|
+
get _inspectors(): Inspector<CommandRecord>[];
|
|
132
|
+
get _commands(): C;
|
|
133
|
+
/**
|
|
134
|
+
* Create a new cli
|
|
135
|
+
* @returns
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const cli = Clerc.create()
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
static create(): Clerc<{}>;
|
|
142
|
+
/**
|
|
143
|
+
* Set the name of the cli
|
|
144
|
+
* @param name
|
|
145
|
+
* @returns
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* Clerc.create()
|
|
149
|
+
* .name("test")
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
name(name: string): this;
|
|
153
|
+
/**
|
|
154
|
+
* Set the description of the cli
|
|
155
|
+
* @param description
|
|
156
|
+
* @returns
|
|
157
|
+
* @example
|
|
158
|
+
* ```ts
|
|
159
|
+
* Clerc.create()
|
|
160
|
+
* .description("test cli")
|
|
161
|
+
*/
|
|
162
|
+
description(description: string): this;
|
|
163
|
+
/**
|
|
164
|
+
* Set the version of the cli
|
|
165
|
+
* @param version
|
|
166
|
+
* @returns
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* Clerc.create()
|
|
170
|
+
* .version("1.0.0")
|
|
171
|
+
*/
|
|
172
|
+
version(version: string): this;
|
|
173
|
+
/**
|
|
174
|
+
* Register a command
|
|
175
|
+
* @param name
|
|
176
|
+
* @param description
|
|
177
|
+
* @param options
|
|
178
|
+
* @returns
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* Clerc.create()
|
|
182
|
+
* .command("test", "test command", {
|
|
183
|
+
* alias: "t",
|
|
184
|
+
* flags: {
|
|
185
|
+
* foo: {
|
|
186
|
+
* alias: "f",
|
|
187
|
+
* description: "foo flag",
|
|
188
|
+
* }
|
|
189
|
+
* }
|
|
190
|
+
* })
|
|
191
|
+
* ```
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* Clerc.create()
|
|
195
|
+
* .command("", "single command", {
|
|
196
|
+
* flags: {
|
|
197
|
+
* foo: {
|
|
198
|
+
* alias: "f",
|
|
199
|
+
* description: "foo flag",
|
|
200
|
+
* }
|
|
201
|
+
* }
|
|
202
|
+
* })
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
command<N extends string | SingleCommandType, O extends CommandOptions<[...P], A, F>, P extends string[] = string[], A extends MaybeArray<string> = MaybeArray<string>, F extends Dict<FlagOptions> = Dict<FlagOptions>>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>>;
|
|
206
|
+
command<N extends string | SingleCommandType, P extends string[], O extends CommandOptions<[...P]>>(name: N, description: string, options?: O & CommandOptions<[...P]>): this & Clerc<C & Record<N, Command<N, O>>>;
|
|
207
|
+
/**
|
|
208
|
+
* Register a handler
|
|
209
|
+
* @param name
|
|
210
|
+
* @param handler
|
|
211
|
+
* @returns
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* Clerc.create()
|
|
215
|
+
* .command("test", "test command")
|
|
216
|
+
* .on("test", (ctx) => {
|
|
217
|
+
* console.log(ctx);
|
|
218
|
+
* })
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
on<K extends keyof CM, CM extends this["_commands"] = this["_commands"]>(name: LiteralUnion<K, string>, handler: Handler<CM, K>): this;
|
|
222
|
+
/**
|
|
223
|
+
* Use a plugin
|
|
224
|
+
* @param plugin
|
|
225
|
+
* @returns
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* Clerc.create()
|
|
229
|
+
* .use(plugin)
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
use<T extends Clerc, U extends Clerc>(plugin: Plugin<T, U>): U;
|
|
233
|
+
/**
|
|
234
|
+
* Register a inspector
|
|
235
|
+
* @param inspector
|
|
236
|
+
* @returns
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* Clerc.create()
|
|
240
|
+
* .inspector((ctx, next) => {
|
|
241
|
+
* console.log(ctx);
|
|
242
|
+
* next();
|
|
243
|
+
* })
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
inspector(inspector: Inspector): this;
|
|
247
|
+
/**
|
|
248
|
+
* Parse the command line arguments
|
|
249
|
+
* @param args
|
|
250
|
+
* @returns
|
|
251
|
+
* @example
|
|
252
|
+
* ```ts
|
|
253
|
+
* Clerc.create()
|
|
254
|
+
* .parse(process.argv.slice(2)) // Optional
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
parse(argv?: string[]): this;
|
|
258
|
+
}
|
|
2
259
|
|
|
3
260
|
interface Options {
|
|
4
261
|
command?: boolean;
|
|
5
262
|
}
|
|
6
|
-
declare const completionsPlugin: (options?: Options) =>
|
|
263
|
+
declare const completionsPlugin: (options?: Options) => Plugin<Clerc<{}>, Clerc<{}>>;
|
|
7
264
|
|
|
8
265
|
export { Options, completionsPlugin };
|
package/dist/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/plugin-completions",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
|
|
5
5
|
"description": "Clerc plugin completions",
|
|
6
6
|
"keywords": [
|
|
@@ -43,8 +43,10 @@
|
|
|
43
43
|
"clerc": "*"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@clerc/
|
|
47
|
-
|
|
46
|
+
"@clerc/utils": "0.10.1"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@clerc/core": "0.10.1"
|
|
48
50
|
},
|
|
49
51
|
"scripts": {
|
|
50
52
|
"build": "puild",
|