@gunshi/plugin-completion 0.27.0-alpha.9 → 0.27.0-beta.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.
Files changed (4) hide show
  1. package/README.md +182 -5
  2. package/lib/index.d.ts +27 -22
  3. package/lib/index.js +169 -1055
  4. package/package.json +13 -9
package/README.md CHANGED
@@ -1,9 +1,15 @@
1
1
  # @gunshi/plugin-completion
2
2
 
3
+ [![Version][npm-version-src]][npm-version-href]
4
+ [![InstallSize][install-size-src]][install-size-src]
5
+ [![JSR][jsr-src]][jsr-href]
6
+
3
7
  > shell completion plugin for gunshi.
4
8
 
5
9
  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
10
 
11
+ This completion plugin is powered by [`@bomb.sh/tab`](https://github.com/bombshell-dev/tab)
12
+
7
13
  <!-- eslint-disable markdown/no-missing-label-refs -->
8
14
 
9
15
  > [!WARNING]
@@ -84,7 +90,7 @@ await cli(process.argv.slice(2), command, {
84
90
 
85
91
  The plugin automatically adds a `complete` subcommand to your CLI:
86
92
 
87
- ```bash
93
+ ```sh
88
94
  # Generate shell completion script
89
95
  my-cli complete bash > ~/.my-cli-completion.bash
90
96
  source ~/.my-cli-completion.bash
@@ -101,6 +107,172 @@ The `complete` command accepts the following shell types:
101
107
  - `bash` - Bash shell completion
102
108
  - `zsh` - Zsh shell completion
103
109
  - `fish` - Fish shell completion
110
+ - `powershell` - PowerShell completion
111
+
112
+ <!-- eslint-disable markdown/no-missing-label-refs -->
113
+
114
+ > [!NOTE]
115
+ > Supported shells comply with [`@bomb.sh/tab`](https://github.com/bombshell-dev/tab)
116
+
117
+ <!-- eslint-enable markdown/no-missing-label-refs -->
118
+
119
+ ## Shell Completion Setup
120
+
121
+ This section provides detailed instructions for setting up shell completions in different shells. The setup is a one-time process that enables tab completion for your CLI.
122
+
123
+ ### Prerequisites
124
+
125
+ Shell completion requires Node.js runtime. Ensure your CLI is running with Node.js (not Deno or Bun).
126
+
127
+ <!-- eslint-disable markdown/no-missing-label-refs -->
128
+
129
+ > [!WARNING]
130
+ > This package support Node.js runtime only. Deno and Bun support are coming soon.
131
+
132
+ <!-- eslint-enable markdown/no-missing-label-refs -->
133
+
134
+ ### Setup by Shell
135
+
136
+ #### Bash
137
+
138
+ Bash users have multiple options for installing completion scripts. Choose the approach that best fits your system:
139
+
140
+ **Option 1: User-specific completion directory (Recommended)**
141
+
142
+ ```sh
143
+ # Create the completion directory if it doesn't exist
144
+ mkdir -p ~/.local/share/bash-completion/completions
145
+
146
+ # Generate and install the completion script
147
+ your-cli complete bash > ~/.local/share/bash-completion/completions/your-cli
148
+
149
+ # Reload your shell configuration
150
+ source ~/.bashrc
151
+ ```
152
+
153
+ **Option 2: Alternative user directory**
154
+
155
+ ```sh
156
+ # Create the completion directory if it doesn't exist
157
+ mkdir -p ~/.bash_completion.d
158
+
159
+ # Generate and install the completion script
160
+ your-cli complete bash > ~/.bash_completion.d/your-cli
161
+
162
+ # Add this line to your ~/.bashrc (only needed once)
163
+ echo 'for f in ~/.bash_completion.d/*; do source "$f"; done' >> ~/.bashrc
164
+
165
+ # Reload your shell configuration
166
+ source ~/.bashrc
167
+ ```
168
+
169
+ #### Zsh
170
+
171
+ Zsh requires adding the completion directory to your `fpath`:
172
+
173
+ ```sh
174
+ # Create the completion directory if it doesn't exist
175
+ mkdir -p ~/.zsh/completions
176
+
177
+ # Add the completion directory to fpath in ~/.zshrc (only needed once)
178
+ echo 'fpath=(~/.zsh/completions $fpath)' >> ~/.zshrc
179
+ echo 'autoload -U compinit && compinit' >> ~/.zshrc
180
+
181
+ # Generate and install the completion script (note the underscore prefix)
182
+ your-cli complete zsh > ~/.zsh/completions/_your-cli
183
+
184
+ # Reload your shell configuration
185
+ source ~/.zshrc
186
+ # OR restart the shell
187
+ exec zsh
188
+ ```
189
+
190
+ <!-- eslint-disable markdown/no-missing-label-refs -->
191
+
192
+ > [!NOTE]
193
+ > Zsh completion files must start with an underscore (`_`) followed by the command name.
194
+
195
+ <!-- eslint-enable markdown/no-missing-label-refs -->
196
+
197
+ #### Fish
198
+
199
+ Fish shell has the simplest setup process:
200
+
201
+ ```sh
202
+ # Create the completion directory if it doesn't exist
203
+ mkdir -p ~/.config/fish/completions
204
+
205
+ # Generate and install the completion script
206
+ your-cli complete fish > ~/.config/fish/completions/your-cli.fish
207
+
208
+ # Completions are loaded automatically - no reload needed
209
+ # Optionally, restart the shell for immediate effect
210
+ exec fish
211
+ ```
212
+
213
+ #### PowerShell
214
+
215
+ PowerShell requires adding the completion script to your profile:
216
+
217
+ ```powershell
218
+ # Create the profile directory if it doesn't exist
219
+ New-Item -ItemType Directory -Force -Path (Split-Path $PROFILE)
220
+
221
+ # Generate the completion script and test it
222
+ your-cli complete powershell | Out-String | Invoke-Expression
223
+
224
+ # To make it permanent, add it to your PowerShell profile
225
+ your-cli complete powershell >> $PROFILE
226
+
227
+ # Reload your profile
228
+ . $PROFILE
229
+ # OR restart PowerShell
230
+ ```
231
+
232
+ ### Troubleshooting
233
+
234
+ If completions don't work after setup:
235
+
236
+ 1. **Check script generation**: Ensure `your-cli complete <shell>` outputs a valid script
237
+ 2. **Verify file location**: Confirm the completion script is in the correct directory
238
+ 3. **Reload shell**: Try opening a new terminal session
239
+ 4. **Check permissions**: Ensure the completion file is readable (`chmod +r <file>`)
240
+ 5. **Debug mode**: Some shells offer debug options:
241
+ - Bash: `set -x` before sourcing
242
+ - Zsh: `setopt xtrace` before sourcing
243
+ - Fish: `fish --debug=complete`
244
+ - Powershell: `Set-PSDebug -Trace 1`
245
+
246
+ ### System-wide Installation
247
+
248
+ <!-- eslint-disable markdown/no-missing-label-refs -->
249
+
250
+ > [!WARNING]
251
+ > System-wide installation requires root/administrator permissions and is not recommended for most users. User-specific installation is preferred as it doesn't require elevated privileges and is easier to manage.
252
+
253
+ <!-- eslint-enable markdown/no-missing-label-refs -->
254
+
255
+ If you need system-wide completions:
256
+
257
+ - **Bash**: `/etc/bash_completion.d/` or `/usr/share/bash-completion/completions/`
258
+ - **Zsh**: `/usr/share/zsh/site-functions/` or `/usr/local/share/zsh/site-functions/`
259
+ - **Fish**: `/usr/share/fish/vendor_completions.d/`
260
+ - **PowerShell**: for Moduele, `/usr/local/share/powershell/Modules/`, for system profiles, `$PSHOME\Profile.ps1`
261
+
262
+ ### Updating Completions
263
+
264
+ When your CLI updates with new commands or options:
265
+
266
+ 1. Regenerate the completion script: `your-cli complete <shell> > <path-to-completion-file>`
267
+ 2. Reload your shell or start a new session
268
+
269
+ ### Uninstalling Completions
270
+
271
+ To remove completion support:
272
+
273
+ 1. Delete the completion script file
274
+ 2. Remove any lines added to your shell configuration files (`.bashrc`, `.zshrc`, etc.)
275
+ 3. Reload your shell or start a new session
104
276
 
105
277
  ### Custom Completion Handlers
106
278
 
@@ -186,9 +358,6 @@ interface CompletionConfig {
186
358
 
187
359
  ```ts
188
360
  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
361
  locale?: Intl.Locale // Current locale (if i18n is enabled)
193
362
  }) => CompletionItem[]
194
363
 
@@ -231,10 +400,18 @@ See the [API References](./docs/index.md)
231
400
 
232
401
  This project uses and depends on:
233
402
 
234
- - [`@bombsh/tab`](https://github.com/bombshell-dev/tab), created by [Bombshell](https://github.com/bombshell-dev) - Shell completion library
403
+ - [`@bomb.sh/tab`](https://github.com/bombshell-dev/tab), created by [Bombshell](https://github.com/bombshell-dev) - Shell completion library
235
404
 
236
405
  Thank you!
237
406
 
238
407
  ## ©️ License
239
408
 
240
409
  [MIT](http://opensource.org/licenses/MIT)
410
+
411
+ <!-- Badges -->
412
+
413
+ [npm-version-src]: https://img.shields.io/npm/v/@gunshi/plugin-completion?style=flat
414
+ [npm-version-href]: https://npmjs.com/package/@gunshi/plugin-completion@alpha
415
+ [jsr-src]: https://jsr.io/badges/@gunshi/plugin-completion
416
+ [jsr-href]: https://jsr.io/@gunshi/plugin-completion
417
+ [install-size-src]: https://pkg-size.dev/badge/install/30875
package/lib/index.d.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  * Bombshell related codes are forked from @bombsh/tab
4
4
  */
5
5
 
6
+ import { Completion } from "@bomb.sh/tab";
6
7
  import { PluginWithoutExtension } from "@gunshi/plugin";
7
8
 
8
9
  //#region ../shared/src/constants.d.ts
@@ -24,18 +25,6 @@ declare const BUILT_IN_KEY_SEPARATOR = ":";
24
25
  * Generate a namespaced key.
25
26
  */
26
27
  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
- */
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
28
  //#endregion
40
29
  //#region src/types.d.ts
41
30
  /**
@@ -47,28 +36,37 @@ declare const pluginId: GenerateNamespacedKey<'completion', typeof PLUGIN_PREFIX
47
36
  */
48
37
  type PluginId = typeof pluginId;
49
38
  /**
50
- * Parameters for {@link CompletionHandler | the completion handler}.
39
+ * Parameters for {@linkcode CompletionHandler | the completion handler}.
51
40
  */
52
41
  interface CompletionParams {
53
- previousArgs: Parameters<Handler>[0];
54
- toComplete: Parameters<Handler>[1];
55
- endWithSpace: Parameters<Handler>[2];
42
+ /**
43
+ * The locale to use for i18n.
44
+ */
56
45
  locale?: Intl.Locale;
57
46
  }
58
47
  /**
59
48
  * The handler for completion.
49
+ *
50
+ * @param params - The {@linkcode CompletionParams | parameters} for the completion handler.
51
+ * @returns An array of {@linkcode Completion | completions}.
60
52
  */
61
- type CompletionHandler = (params: CompletionParams) => ReturnType<Handler>;
53
+ type CompletionHandler = (params: CompletionParams) => Completion[];
62
54
  /**
63
55
  * Extended command context which provides utilities via completion plugin.
64
56
  * These utilities are available via `CommandContext.extensions['g:completion']`.
65
57
  */
66
- interface CompletionCommandContext {}
58
+ interface CompletionExtension {}
67
59
  /**
68
60
  * Completion configuration, which structure is similar `bombsh/tab`'s `CompletionConfig`.
69
61
  */
70
62
  interface CompletionConfig {
63
+ /**
64
+ * The {@linkcode CompletionHandler | handler} for the completion.
65
+ */
71
66
  handler?: CompletionHandler;
67
+ /**
68
+ * The command arguments for the completion.
69
+ */
72
70
  args?: Record<string, {
73
71
  handler: CompletionHandler;
74
72
  }>;
@@ -77,22 +75,29 @@ interface CompletionConfig {
77
75
  * Completion plugin options.
78
76
  */
79
77
  interface CompletionOptions {
78
+ /**
79
+ * The completion configuration
80
+ */
80
81
  config?: {
81
82
  /**
82
- * The entry point completion configuration.
83
+ * The entry point {@linkcode CompletionConfig | completion configuration}.
83
84
  */
84
85
  entry?: CompletionConfig;
85
86
  /**
86
- * The handlers for subcommands.
87
+ * The handlers for sub-commands.
87
88
  */
88
89
  subCommands?: Record<string, CompletionConfig>;
89
90
  };
90
91
  }
91
92
  //#endregion
92
93
  //#region src/index.d.ts
94
+
93
95
  /**
94
- * completion plugin for gunshi
96
+ * completion plugin
97
+ *
98
+ * @param options - A {@linkcode CompletionOptions | Completion options}
99
+ * @returns A defined plugin as completion
95
100
  */
96
101
  declare function completion(options?: CompletionOptions): PluginWithoutExtension;
97
102
  //#endregion
98
- export { CompletionCommandContext, CompletionConfig, CompletionHandler, CompletionOptions, CompletionParams, PluginId, completion as default, pluginId };
103
+ export { CompletionConfig, CompletionExtension, CompletionHandler, CompletionOptions, CompletionParams, PluginId, completion as default, pluginId };