@gunshi/plugin-completion 0.26.3 â 0.27.0-alpha.10
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 +235 -2
- package/lib/index.d.ts +90 -4
- package/lib/index.js +1285 -5
- package/package.json +13 -6
package/README.md
CHANGED
|
@@ -1,6 +1,239 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @gunshi/plugin-completion
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> shell completion plugin for gunshi.
|
|
4
|
+
|
|
5
|
+
This plugin provides tab completion functionality for your CLI applications, allowing users to auto-complete commands, options, and arguments in their shell. It generates shell-specific completion scripts and handles runtime completion suggestions.
|
|
6
|
+
|
|
7
|
+
<!-- eslint-disable markdown/no-missing-label-refs -->
|
|
8
|
+
|
|
9
|
+
> [!WARNING]
|
|
10
|
+
> This package support Node.js runtime only. Deno and Bun support are coming soon.
|
|
11
|
+
|
|
12
|
+
<!-- eslint-enable markdown/no-missing-label-refs -->
|
|
13
|
+
|
|
14
|
+
## đŋ Installation
|
|
15
|
+
|
|
16
|
+
```sh
|
|
17
|
+
# npm
|
|
18
|
+
npm install --save @gunshi/plugin-completion
|
|
19
|
+
|
|
20
|
+
# pnpm
|
|
21
|
+
pnpm add @gunshi/plugin-completion
|
|
22
|
+
|
|
23
|
+
# yarn
|
|
24
|
+
yarn add @gunshi/plugin-completion
|
|
25
|
+
|
|
26
|
+
# deno
|
|
27
|
+
deno add jsr:@gunshi/plugin-completion
|
|
28
|
+
|
|
29
|
+
# bun
|
|
30
|
+
bun add @gunshi/plugin-completion
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## đ Usage
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { cli } from 'gunshi'
|
|
37
|
+
import completion from '@gunshi/plugin-completion'
|
|
38
|
+
|
|
39
|
+
const command = {
|
|
40
|
+
name: 'deploy',
|
|
41
|
+
args: {
|
|
42
|
+
environment: {
|
|
43
|
+
type: 'string',
|
|
44
|
+
short: 'e',
|
|
45
|
+
description: 'Target environment'
|
|
46
|
+
},
|
|
47
|
+
config: {
|
|
48
|
+
type: 'string',
|
|
49
|
+
short: 'c',
|
|
50
|
+
description: 'Config file path'
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
run: ctx => {
|
|
54
|
+
console.log(`Deploying to ${ctx.values.environment}`)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
await cli(process.argv.slice(2), command, {
|
|
59
|
+
name: 'my-cli',
|
|
60
|
+
version: '1.0.0',
|
|
61
|
+
plugins: [
|
|
62
|
+
completion({
|
|
63
|
+
config: {
|
|
64
|
+
entry: {
|
|
65
|
+
args: {
|
|
66
|
+
config: {
|
|
67
|
+
handler: () => [
|
|
68
|
+
{ value: 'prod.json', description: 'Production config' },
|
|
69
|
+
{ value: 'dev.json', description: 'Development config' },
|
|
70
|
+
{ value: 'test.json', description: 'Test config' }
|
|
71
|
+
]
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
]
|
|
78
|
+
})
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## ⨠Features
|
|
82
|
+
|
|
83
|
+
### Automatic Complete Command
|
|
84
|
+
|
|
85
|
+
The plugin automatically adds a `complete` subcommand to your CLI:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
# Generate shell completion script
|
|
89
|
+
my-cli complete bash > ~/.my-cli-completion.bash
|
|
90
|
+
source ~/.my-cli-completion.bash
|
|
91
|
+
|
|
92
|
+
# Now tab completion works!
|
|
93
|
+
my-cli dep<TAB> # Completes to: my-cli deploy
|
|
94
|
+
my-cli deploy --env<TAB> # Completes to: my-cli deploy --environment
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Shell Support
|
|
98
|
+
|
|
99
|
+
The `complete` command accepts the following shell types:
|
|
100
|
+
|
|
101
|
+
- `bash` - Bash shell completion
|
|
102
|
+
- `zsh` - Zsh shell completion
|
|
103
|
+
- `fish` - Fish shell completion
|
|
104
|
+
|
|
105
|
+
### Custom Completion Handlers
|
|
106
|
+
|
|
107
|
+
You can provide custom completion handlers for specific arguments:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
completion({
|
|
111
|
+
config: {
|
|
112
|
+
entry: {
|
|
113
|
+
args: {
|
|
114
|
+
environment: {
|
|
115
|
+
handler: ({ locale }) => [
|
|
116
|
+
{ value: 'production', description: 'Production environment' },
|
|
117
|
+
{ value: 'staging', description: 'Staging environment' },
|
|
118
|
+
{ value: 'development', description: 'Development environment' }
|
|
119
|
+
]
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
subCommands: {
|
|
124
|
+
deploy: {
|
|
125
|
+
args: {
|
|
126
|
+
region: {
|
|
127
|
+
handler: ({ previousArgs }) => {
|
|
128
|
+
// Dynamic completions based on previous arguments
|
|
129
|
+
const env = previousArgs.find(arg => arg.startsWith('--environment='))
|
|
130
|
+
if (env?.includes('production')) {
|
|
131
|
+
return [
|
|
132
|
+
{ value: 'us-east-1', description: 'US East (N. Virginia)' },
|
|
133
|
+
{ value: 'eu-west-1', description: 'EU (Ireland)' }
|
|
134
|
+
]
|
|
135
|
+
}
|
|
136
|
+
return [{ value: 'local', description: 'Local development' }]
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Internationalization Support
|
|
147
|
+
|
|
148
|
+
When used with `@gunshi/plugin-i18n`, completion descriptions are automatically localized:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import completion from '@gunshi/plugin-completion'
|
|
152
|
+
import i18n from '@gunshi/plugin-i18n'
|
|
153
|
+
|
|
154
|
+
await cli(args, command, {
|
|
155
|
+
plugins: [
|
|
156
|
+
i18n({ locale: 'ja-JP' }),
|
|
157
|
+
completion() // Descriptions will be shown in Japanese
|
|
158
|
+
]
|
|
159
|
+
})
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## âī¸ Plugin Options
|
|
163
|
+
|
|
164
|
+
### `config`
|
|
165
|
+
|
|
166
|
+
- Type: `{ entry?: CompletionConfig, subCommands?: Record<string, CompletionConfig> }`
|
|
167
|
+
- Default: `{}`
|
|
168
|
+
- Description: Configuration for completion handlers
|
|
169
|
+
|
|
170
|
+
#### CompletionConfig
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
interface CompletionConfig {
|
|
174
|
+
handler?: CompletionHandler // Handler for command-level completions
|
|
175
|
+
args?: Record<
|
|
176
|
+
string,
|
|
177
|
+
{
|
|
178
|
+
// Handlers for specific arguments
|
|
179
|
+
handler: CompletionHandler
|
|
180
|
+
}
|
|
181
|
+
>
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
#### CompletionHandler
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
type CompletionHandler = (params: {
|
|
189
|
+
previousArgs: string[] // Previously entered arguments
|
|
190
|
+
toComplete: string // Current string being completed
|
|
191
|
+
endWithSpace: boolean // Whether input ends with space
|
|
192
|
+
locale?: Intl.Locale // Current locale (if i18n is enabled)
|
|
193
|
+
}) => CompletionItem[]
|
|
194
|
+
|
|
195
|
+
interface CompletionItem {
|
|
196
|
+
value: string // The completion value
|
|
197
|
+
description?: string // Optional description
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## đ Plugin Dependencies
|
|
202
|
+
|
|
203
|
+
The completion plugin has an optional dependency on the i18n plugin:
|
|
204
|
+
|
|
205
|
+
- **Plugin ID**: `g:i18n` (optional)
|
|
206
|
+
- **Purpose**: Provides localized descriptions for completions
|
|
207
|
+
- **Effect**: When the i18n plugin is present, all command and argument descriptions are automatically translated to the current locale
|
|
208
|
+
|
|
209
|
+
## đ§Š Context Extensions
|
|
210
|
+
|
|
211
|
+
When using the completion plugin, your command context is extended via `ctx.extensions['g:completion']`.
|
|
212
|
+
|
|
213
|
+
<!-- eslint-disable markdown/no-missing-label-refs -->
|
|
214
|
+
|
|
215
|
+
> [!IMPORTANT]
|
|
216
|
+
> This plugin extension is namespaced in `CommandContext.extensions` using this plugin ID `g:completion` by the gunshi plugin system.
|
|
217
|
+
|
|
218
|
+
<!-- eslint-enable markdown/no-missing-label-refs -->
|
|
219
|
+
|
|
220
|
+
Currently, the completion plugin does not provide any context extensions for use within commands. The plugin ID can be imported for type-safe access:
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
import completion, { pluginId } from '@gunshi/plugin-completion'
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## đ API References
|
|
227
|
+
|
|
228
|
+
See the [API References](./docs/index.md)
|
|
229
|
+
|
|
230
|
+
## đ Credits
|
|
231
|
+
|
|
232
|
+
This project uses and depends on:
|
|
233
|
+
|
|
234
|
+
- [`@bombsh/tab`](https://github.com/bombshell-dev/tab), created by [Bombshell](https://github.com/bombshell-dev) - Shell completion library
|
|
235
|
+
|
|
236
|
+
Thank you!
|
|
4
237
|
|
|
5
238
|
## ÂŠī¸ License
|
|
6
239
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,12 +1,98 @@
|
|
|
1
|
+
/*! license ISC
|
|
2
|
+
* @author Bombshell team and Bombshell contributors
|
|
3
|
+
* Bombshell related codes are forked from @bombsh/tab
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import { PluginWithoutExtension } from "@gunshi/plugin";
|
|
2
7
|
|
|
3
|
-
//#region src/
|
|
8
|
+
//#region ../shared/src/constants.d.ts
|
|
9
|
+
/**
|
|
10
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
11
|
+
* @license MIT
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
15
|
+
* @license MIT
|
|
16
|
+
*/
|
|
17
|
+
declare const BUILT_IN_PREFIX = "_";
|
|
18
|
+
declare const PLUGIN_PREFIX = "g";
|
|
19
|
+
declare const BUILT_IN_KEY_SEPARATOR = ":";
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region ../shared/src/types.d.ts
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Generate a namespaced key.
|
|
25
|
+
*/
|
|
26
|
+
type GenerateNamespacedKey<Key extends string, Prefixed extends string = typeof BUILT_IN_PREFIX> = `${Prefixed}${typeof BUILT_IN_KEY_SEPARATOR}${Key}`;
|
|
27
|
+
/**
|
|
28
|
+
* Command i18n built-in arguments keys.
|
|
29
|
+
*/
|
|
4
30
|
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/bombshell/index.d.ts
|
|
33
|
+
|
|
34
|
+
type Item = {
|
|
35
|
+
description: string;
|
|
36
|
+
value: string;
|
|
37
|
+
};
|
|
38
|
+
type Handler = (previousArgs: string[], toComplete: string, endsWithSpace: boolean) => Item[] | Promise<Item[]>;
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/types.d.ts
|
|
41
|
+
/**
|
|
42
|
+
* The unique identifier for the completion plugin.
|
|
43
|
+
*/
|
|
44
|
+
declare const pluginId: GenerateNamespacedKey<'completion', typeof PLUGIN_PREFIX>;
|
|
45
|
+
/**
|
|
46
|
+
* Type representing the unique identifier for the completion plugin.
|
|
47
|
+
*/
|
|
48
|
+
type PluginId = typeof pluginId;
|
|
49
|
+
/**
|
|
50
|
+
* Parameters for {@link CompletionHandler | the completion handler}.
|
|
51
|
+
*/
|
|
52
|
+
interface CompletionParams {
|
|
53
|
+
previousArgs: Parameters<Handler>[0];
|
|
54
|
+
toComplete: Parameters<Handler>[1];
|
|
55
|
+
endWithSpace: Parameters<Handler>[2];
|
|
56
|
+
locale?: Intl.Locale;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The handler for completion.
|
|
60
|
+
*/
|
|
61
|
+
type CompletionHandler = (params: CompletionParams) => ReturnType<Handler>;
|
|
62
|
+
/**
|
|
63
|
+
* Extended command context which provides utilities via completion plugin.
|
|
64
|
+
* These utilities are available via `CommandContext.extensions['g:completion']`.
|
|
65
|
+
*/
|
|
5
66
|
interface CompletionCommandContext {}
|
|
67
|
+
/**
|
|
68
|
+
* Completion configuration, which structure is similar `bombsh/tab`'s `CompletionConfig`.
|
|
69
|
+
*/
|
|
70
|
+
interface CompletionConfig {
|
|
71
|
+
handler?: CompletionHandler;
|
|
72
|
+
args?: Record<string, {
|
|
73
|
+
handler: CompletionHandler;
|
|
74
|
+
}>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Completion plugin options.
|
|
78
|
+
*/
|
|
79
|
+
interface CompletionOptions {
|
|
80
|
+
config?: {
|
|
81
|
+
/**
|
|
82
|
+
* The entry point completion configuration.
|
|
83
|
+
*/
|
|
84
|
+
entry?: CompletionConfig;
|
|
85
|
+
/**
|
|
86
|
+
* The handlers for subcommands.
|
|
87
|
+
*/
|
|
88
|
+
subCommands?: Record<string, CompletionConfig>;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/index.d.ts
|
|
6
93
|
/**
|
|
7
94
|
* completion plugin for gunshi
|
|
8
95
|
*/
|
|
9
|
-
declare function completion(): PluginWithoutExtension
|
|
10
|
-
|
|
96
|
+
declare function completion(options?: CompletionOptions): PluginWithoutExtension;
|
|
11
97
|
//#endregion
|
|
12
|
-
export { CompletionCommandContext, completion as default };
|
|
98
|
+
export { CompletionCommandContext, CompletionConfig, CompletionHandler, CompletionOptions, CompletionParams, PluginId, completion as default, pluginId };
|