@clerc/plugin-completions 0.10.3 → 0.10.5

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
@@ -1,274 +1,8 @@
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
- type TransformParameters<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> = {
97
- [Parameter in [...NonNullableParameters<C[N]["parameters"]>][number] as CamelCase<StripBrackets<Parameter>>]: ParameterType<Parameter>;
98
- };
99
- type TransformFlags<F extends Record<string, FlagSchema>> = {
100
- [K in keyof F]: F[K]["type"] extends any[] ? F[K]["default"] extends never[] ? F[K] & {
101
- default: any[];
102
- } : F[K] : F[K];
103
- };
104
- type TypeFlagWithDefault<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> = TypeFlag<TransformFlags<NonNullable<C[N]["flags"]>>>;
105
- type Raw<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> = TypeFlagWithDefault<C, N> & {
106
- parameters: string[];
107
- mergedFlags: TypeFlagWithDefault<C, N>["flags"] & TypeFlagWithDefault<C, N>["unknownFlags"];
108
- };
109
- interface HandlerContext<C extends CommandRecord = CommandRecord, N extends keyof C = keyof C> {
110
- name?: N;
111
- resolved: boolean;
112
- isSingleCommand: boolean;
113
- raw: Raw<C, N>;
114
- parameters: TransformParameters<C, N>;
115
- unknownFlags: ParsedFlags["unknownFlags"];
116
- flags: TypeFlagWithDefault<C, N>["flags"];
117
- cli: Clerc<C>;
118
- }
119
- type Handler<C extends CommandRecord = CommandRecord, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K>) => void;
120
- type HandlerInCommand<C extends CommandRecord = CommandRecord, K extends keyof C = keyof C> = (ctx: HandlerContext<C, K> & {
121
- name: K;
122
- }) => void;
123
- type FallbackType<T, U> = {} extends T ? U : T;
124
- type InspectorContext<C extends CommandRecord = CommandRecord> = HandlerContext<C> & {
125
- flags: FallbackType<TypeFlag<NonNullable<C[keyof C]["flags"]>>["flags"], Dict<any>>;
126
- };
127
- type Inspector<C extends CommandRecord = CommandRecord> = (ctx: InspectorContext<C>, next: () => void) => void;
128
- interface Plugin<T extends Clerc = Clerc, U extends Clerc = Clerc> {
129
- setup: (cli: T) => U;
130
- }
131
-
132
- declare const SingleCommand: unique symbol;
133
- type SingleCommandType = typeof SingleCommand;
134
- declare class Clerc<C extends CommandRecord = {}> {
135
- #private;
136
- private constructor();
137
- get _name(): string;
138
- get _description(): string;
139
- get _version(): string;
140
- get _inspectors(): Inspector<CommandRecord>[];
141
- get _commands(): C;
142
- /**
143
- * Create a new cli
144
- * @returns
145
- * @example
146
- * ```ts
147
- * const cli = Clerc.create()
148
- * ```
149
- */
150
- static create(): Clerc<{}>;
151
- /**
152
- * Set the name of the cli
153
- * @param name
154
- * @returns
155
- * @example
156
- * ```ts
157
- * Clerc.create()
158
- * .name("test")
159
- * ```
160
- */
161
- name(name: string): this;
162
- /**
163
- * Set the description of the cli
164
- * @param description
165
- * @returns
166
- * @example
167
- * ```ts
168
- * Clerc.create()
169
- * .description("test cli")
170
- */
171
- description(description: string): this;
172
- /**
173
- * Set the version of the cli
174
- * @param version
175
- * @returns
176
- * @example
177
- * ```ts
178
- * Clerc.create()
179
- * .version("1.0.0")
180
- */
181
- version(version: string): this;
182
- /**
183
- * Register a command
184
- * @param name
185
- * @param description
186
- * @param options
187
- * @returns
188
- * @example
189
- * ```ts
190
- * Clerc.create()
191
- * .command("test", "test command", {
192
- * alias: "t",
193
- * flags: {
194
- * foo: {
195
- * alias: "f",
196
- * description: "foo flag",
197
- * }
198
- * }
199
- * })
200
- * ```
201
- * @example
202
- * ```ts
203
- * Clerc.create()
204
- * .command("", "single command", {
205
- * flags: {
206
- * foo: {
207
- * alias: "f",
208
- * description: "foo flag",
209
- * }
210
- * }
211
- * })
212
- * ```
213
- */
214
- 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> = {}>(c: CommandWithHandler<N, O & CommandOptions<[...P], A, F>>): this & Clerc<C & Record<N, Command<N, O>>>;
215
- 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> = {}>(name: N, description: string, options?: O & CommandOptions<[...P], A, F>): this & Clerc<C & Record<N, Command<N, O>>>;
216
- /**
217
- * Register a handler
218
- * @param name
219
- * @param handler
220
- * @returns
221
- * @example
222
- * ```ts
223
- * Clerc.create()
224
- * .command("test", "test command")
225
- * .on("test", (ctx) => {
226
- * console.log(ctx);
227
- * })
228
- * ```
229
- */
230
- on<K extends keyof CM, CM extends this["_commands"] = this["_commands"]>(name: LiteralUnion<K, string>, handler: Handler<CM, K>): this;
231
- /**
232
- * Use a plugin
233
- * @param plugin
234
- * @returns
235
- * @example
236
- * ```ts
237
- * Clerc.create()
238
- * .use(plugin)
239
- * ```
240
- */
241
- use<T extends Clerc, U extends Clerc>(plugin: Plugin<T, U>): U;
242
- /**
243
- * Register a inspector
244
- * @param inspector
245
- * @returns
246
- * @example
247
- * ```ts
248
- * Clerc.create()
249
- * .inspector((ctx, next) => {
250
- * console.log(ctx);
251
- * next();
252
- * })
253
- * ```
254
- */
255
- inspector(inspector: Inspector): this;
256
- /**
257
- * Parse the command line arguments
258
- * @param args
259
- * @returns
260
- * @example
261
- * ```ts
262
- * Clerc.create()
263
- * .parse(process.argv.slice(2)) // Optional
264
- * ```
265
- */
266
- parse(argv?: string[]): this;
267
- }
1
+ import * as _clerc_core from '@clerc/core';
268
2
 
269
3
  interface Options {
270
4
  command?: boolean;
271
5
  }
272
- declare const completionsPlugin: (options?: Options) => Plugin<Clerc<{}>, Clerc<{}>>;
6
+ declare const completionsPlugin: (options?: Options) => _clerc_core.Plugin<_clerc_core.Clerc<{}>, _clerc_core.Clerc<{}>>;
273
7
 
274
8
  export { Options, completionsPlugin };
@@ -1,10 +1,5 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var utils = require('@clerc/utils');
6
-
7
- const definePlugin = (p) => p;
1
+ import { definePlugin } from '@clerc/core';
2
+ import { gracefulFlagName, mustArray } from '@clerc/utils';
8
3
 
9
4
  const generateCommandCompletion = (name) => `
10
5
  ${name})
@@ -42,11 +37,11 @@ const NO_DESCRIPTION = "(No Description)";
42
37
  const getCompletionValue = (command) => `[CompletionResult]::new('${command.name}', '${command.name}', [CompletionResultType]::ParameterValue, '${command.description}')`;
43
38
  const getCompletionFlag = (command) => {
44
39
  return Object.entries(command.flags || {}).map(([flagName, flag]) => {
45
- const gen = [`[CompletionResult]::new('${utils.gracefulFlagName(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`];
40
+ const gen = [`[CompletionResult]::new('${gracefulFlagName(flagName)}', '${flagName}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`];
46
41
  if (flag == null ? void 0 : flag.alias) {
47
- const arrayAlias = utils.mustArray(flag.alias);
42
+ const arrayAlias = mustArray(flag.alias);
48
43
  gen.push(
49
- ...arrayAlias.map((n) => `[CompletionResult]::new('${utils.gracefulFlagName(n)}', '${n}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`)
44
+ ...arrayAlias.map((n) => `[CompletionResult]::new('${gracefulFlagName(n)}', '${n}', [CompletionResultType]::ParameterName, '${command.flags[flagName].description || NO_DESCRIPTION}')`)
50
45
  );
51
46
  }
52
47
  return gen.join("\n ");
@@ -96,7 +91,7 @@ const completionMap = {
96
91
  pwsh: getPwshCompletion
97
92
  };
98
93
  const completionsPlugin = (options = {}) => definePlugin({
99
- setup(cli) {
94
+ setup: (cli) => {
100
95
  const { command = true } = options;
101
96
  if (command) {
102
97
  cli = cli.command("completions", "Print shell completions to stdout", {
@@ -129,4 +124,4 @@ const completionsPlugin = (options = {}) => definePlugin({
129
124
  }
130
125
  });
131
126
 
132
- exports.completionsPlugin = completionsPlugin;
127
+ export { completionsPlugin };
package/dist/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
+ import { definePlugin } from '@clerc/core';
1
2
  import { gracefulFlagName, mustArray } from '@clerc/utils';
2
3
 
3
- const definePlugin = (p) => p;
4
-
5
4
  const generateCommandCompletion = (name) => `
6
5
  ${name})
7
6
  cmd+="__${name}"
@@ -92,7 +91,7 @@ const completionMap = {
92
91
  pwsh: getPwshCompletion
93
92
  };
94
93
  const completionsPlugin = (options = {}) => definePlugin({
95
- setup(cli) {
94
+ setup: (cli) => {
96
95
  const { command = true } = options;
97
96
  if (command) {
98
97
  cli = cli.command("completions", "Print shell completions to stdout", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/plugin-completions",
3
- "version": "0.10.3",
3
+ "version": "0.10.5",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc plugin completions",
6
6
  "keywords": [
@@ -12,6 +12,7 @@
12
12
  "args",
13
13
  "terminal"
14
14
  ],
15
+ "type": "module",
15
16
  "homepage": "https://github.com/so1ve/clerc/tree/main/packages/plugin-completions#readme",
16
17
  "repository": {
17
18
  "type": "git",
@@ -26,13 +27,20 @@
26
27
  "exports": {
27
28
  ".": {
28
29
  "types": "./dist/index.d.ts",
29
- "require": "./dist/index.cjs",
30
30
  "import": "./dist/index.mjs"
31
31
  }
32
32
  },
33
- "main": "dist/index.cjs",
33
+ "main": "dist/index.js",
34
34
  "module": "dist/index.mjs",
35
35
  "types": "dist/index.d.ts",
36
+ "typesVersions": {
37
+ "*": {
38
+ "*": [
39
+ "./dist/*",
40
+ "./dist/index.d.ts"
41
+ ]
42
+ }
43
+ },
36
44
  "files": [
37
45
  "dist"
38
46
  ],
@@ -40,13 +48,13 @@
40
48
  "access": "public"
41
49
  },
42
50
  "peerDependencies": {
43
- "clerc": "*"
51
+ "@clerc/core": "*"
44
52
  },
45
53
  "dependencies": {
46
- "@clerc/utils": "0.10.3"
54
+ "@clerc/utils": "0.10.5"
47
55
  },
48
56
  "devDependencies": {
49
- "@clerc/core": "0.10.3"
57
+ "@clerc/core": "0.10.5"
50
58
  },
51
59
  "scripts": {
52
60
  "build": "puild",