blob: 91becac35c81e1e3be3499a1afdb8baf79ba8fef [file] [log] [blame]
Skyler Grey81248f72022-08-11 00:16:06 +01001_bun() {
2 zstyle ':completion:*:*:bun:*' group-name ''
3 zstyle ':completion:*:*:bun-grouped:*' group-name ''
4
5 zstyle ':completion:*:*:bun::descriptions' format '%F{green}-- %d --%f'
6 zstyle ':completion:*:*:bun-grouped:*' format '%F{green}-- %d --%f'
7
8 local program=bun
9 typeset -A opt_args
10 local curcontext="$curcontext" state line context
11
12 # ---- Command:
13 _arguments -s \
14 '1: :->cmd' \
15 '*: :->args' &&
16 ret=0
17
18 case $state in
19 cmd)
20 local -a scripts_list
21 IFS=$'\n' scripts_list=($(SHELL=zsh bun getcompletes i))
22 scripts="scripts:scripts:(($scripts_list))"
23
24 main_commands=('add\:"Add a dependency to package.json" bun\:"Generate a bundle" create\:"Create a new project" dev\:"Start a dev server" help\:"Show command help" install\:"Install packages from package.json" remove\:"Remove a dependency from package.json" run\:"Run a script or package bin" upgrade\:"Upgrade to the latest version of bun"')
25 main_commands=($main_commands)
26 _alternative "$scripts" "args:bun:(($main_commands))"
27 ;;
28 args)
29 case $line[1] in
30 add)
31
32 # ---- Command: add
33 _arguments -s -C \
34 '1: :->cmd1' \
35 '*: :->package' \
36 '--version[Show version and exit]' \
37 '-V[Show version and exit]' \
38 '--cwd[Change directory]:cwd' \
39 '--help[Show command help]' \
40 '-h[Show command help]' \
41 '--registry[Change default registry (default: \$BUN_CONFIG_REGISTRY || \$npm_config_registry)]:registry' \
42 '--token[Authentication token used for npm registry requests (default: \$npm_config_token)]:token' \
43 '-y[Write a yarn.lock file (yarn v1)]' \
44 '--yarn[Write a yarn.lock file (yarn v1)]' \
45 '-g[Add a package globally]' \
46 '--global[Add a package globally]' \
47 '--production[Don'"'"'t install devDependencies]' \
48 '--optional[Add dependency to optionalDependencies]' \
49 '--development[Add dependency to devDependencies]' \
50 '-d[Add dependency to devDependencies]' \
51 '-p[Don'"'"'t install devDependencies]' \
52 '--no-save[]' \
53 '--dry-run[Don'"'"'t install anything]' \
54 '--force[Always request the latest versions from the registry & reinstall all dependenices]' \
55 '--lockfile[Store & load a lockfile at a specific filepath]:lockfile' \
56 '--cache-dir[Store & load cached data from a specific directory path]:cache-dir' \
57 '--no-cache[Ignore manifest cache entirely]' \
58 '--silent[Don'"'"'t output anything]' \
59 '--verbose[Excessively verbose logging]' \
60 '--cwd[Set a specific cwd]:cwd' \
61 '--backend[Platform-specific optimizations for installing dependencies]:backend:("clonefile" "copyfile" "hardlink" "clonefile_each_dir")' \
62 '--link-native-bins[Link "bin" from a matching platform-specific dependency instead. Default: esbuild, turbo]:link-native-bins' &&
63 ret=0
64
65 case $state in
66 package)
67 _bun_add_param_package_completion
68 ;;
69 esac
70
71 ;;
72 bun)
73
74 # ---- Command: bun
75 _arguments -s -C \
76 '1: :->cmd' \
77 '*: :->file' \
78 '--version[Show version and exit]' \
79 '-V[Show version and exit]' \
80 '--cwd[Change directory]:cwd' \
81 '--help[Show command help]' \
82 '-h[Show command help]' \
83 '--use[Use a framework, e.g. "next"]:use' &&
84 ret=0
85
86 case $state in
87 file)
88 _files
89 ;;
90 esac
91
92 ;;
93 create)
94
95 # ---- Command: create
96 _arguments -s -C \
97 '1: :->cmd' \
98 '2: :->cmd2' \
99 '*: :->args' &&
100 ret=0
101
102 case $state in
103 cmd2)
104 _alternative 'args:create:((next\:"Next.js app" react\:"React app"))'
105 ;;
106
107 args)
108 case $line[2] in
109 next)
110
111 # ---- Command: create next
112 _arguments -s -C \
113 '1: :->cmd' \
114 '2: :->cmd2' \
115 '3: :->file' \
116 '--version[Show version and exit]' \
117 '-V[Show version and exit]' \
118 '--cwd[Change directory]:cwd' \
119 '--help[Show command help]' \
120 '-h[Show command help]' &&
121 ret=0
122
123 case $state in
124 file)
125 _files
126 ;;
127 esac
128
129 ;;
130 react)
131
132 # ---- Command: create react
133 _arguments -s -C \
134 '1: :->cmd' \
135 '2: :->cmd2' \
136 '3: :->file' \
137 '--version[Show version and exit]' \
138 '-V[Show version and exit]' \
139 '--cwd[Change directory]:cwd' \
140 '--help[Show command help]' \
141 '-h[Show command help]' &&
142 ret=0
143
144 case $state in
145 file)
146 _files
147 ;;
148 esac
149
150 ;;
151 esac
152
153 ;;
154
155 esac
156 ;;
157 dev)
158
159 # ---- Command: dev
160 _arguments -s -C \
161 '1: :->cmd' \
162 '--version[Show version and exit]' \
163 '-V[Show version and exit]' \
164 '--cwd[Change directory]:cwd' \
165 '--help[Show command help]' \
166 '-h[Show command help]' \
167 '--bunfile[Use a specific .bun file (default: node_modules.bun)]:bunfile' \
168 '--origin[Rewrite import paths to start from a different url. Default: http://localhost:3000]:origin' \
169 '-u[Rewrite import paths to start from a different url. Default: http://localhost:3000]:u' \
170 '--server-bunfile[Use a specific .bun file for SSR in bun dev (default: node_modules.server.bun)]:server-bunfile' \
171 '--extension-order[defaults to: .tsx,.ts,.jsx,.js,.json]:extension-order' \
172 '--jsx-runtime[JSX runtime to use. Defaults to "automatic"]:jsx-runtime:("automatic" "classic")' \
173 '--main-fields[Main fields to lookup in package.json. Defaults to --platform dependent]:main-fields' \
174 '--disable-react-fast-refresh[Disable React Fast Refresh]' \
175 '--disable-hmr[Disable Hot Module Reloading]' \
176 '--jsx-factory[Changes the function called when compiling JSX elements using the classic JSX runtime]:jsx-factory' \
177 '--jsx-fragment[Changes the function called when compiling JSX fragments]:jsx-fragment' \
178 '--jsx-import-source[Declares the module specifier to be used for importing the jsx and jsxs factory functions. Default: "react"]:jsx-import-source' \
179 '--port[Port number]:port' &&
180 ret=0
181
182 ;;
183
184 help)
185
186 # ---- Command: help
187 _arguments -s -C \
188 '1: :->cmd' \
189 '2: :->cmd2' \
190 '*: :->args' &&
191 ret=0
192
193 case $state in
194 cmd2)
195 _alternative 'args:cmd3:((bun create dev run upgrade))'
196 ;;
197
198 args)
199 case $line[2] in
200 bun)
201
202 # ---- Command: help bun
203 _arguments -s -C \
204 '1: :->cmd' \
205 '2: :->cmd2' \
206 '--version[Show version and exit]' \
207 '-V[Show version and exit]' \
208 '--cwd[Change directory]:cwd' \
209 '--help[Show command help]' \
210 '-h[Show command help]' \
211 '--all[]' &&
212 ret=0
213
214 ;;
215 install)
216
217 # ---- Command: help install
218 _arguments -s -C \
219 '1: :->cmd1' \
220 '2: :->cmd2' \
221 '--version[Show version and exit]' \
222 '-V[Show version and exit]' \
223 '--cwd[Change directory]:cwd' \
224 '--help[Show command help]' \
225 '-g[Add a package globally]' \
226 '--global[Add a package globally]' \
227 '-h[Show command help]' \
228 '--all[]' &&
229 ret=0
230
231 ;;
232
233 remove)
234
235 # ---- Command: help remove
236 _arguments -s -C \
237 '1: :->cmd1' \
238 '2: :->cmd2' \
239 '--version[Show version and exit]' \
240 '-V[Show version and exit]' \
241 '--cwd[Change directory]:cwd' \
242 '-g[Remove a package globally]' \
243 '--global[Remove a package globally]' \
244 '--help[Show command help]' \
245 '-h[Show command help]' \
246 '--all[]' &&
247 ret=0
248
249 ;;
250
251 create)
252
253 # ---- Command: help create
254 _arguments -s -C \
255 '1: :->cmd' \
256 '2: :->cmd2' \
257 '3: :->cmd3' \
258 '*: :->args' &&
259 ret=0
260
261 case $state in
262 cmd3)
263 _alternative 'args:create:((next react))'
264 ;;
265
266 args)
267 case $line[3] in
268 next)
269
270 # ---- Command: help create next
271 _arguments -s -C \
272 '1: :->cmd' \
273 '2: :->cmd2' \
274 '3: :->cmd3' \
275 '--version[Show version and exit]' \
276 '-V[Show version and exit]' \
277 '--cwd[Change directory]:cwd' \
278 '--help[Show command help]' \
279 '-h[Show command help]' \
280 '--all[]' &&
281 ret=0
282
283 ;;
284
285 react)
286
287 # ---- Command: help create react
288 _arguments -s -C \
289 '1: :->cmd' \
290 '2: :->cmd2' \
291 '3: :->cmd3' \
292 '--version[Show version and exit]' \
293 '-V[Show version and exit]' \
294 '--cwd[Change directory]:cwd' \
295 '--help[Show command help]' \
296 '-h[Show command help]' \
297 '--all[]' &&
298 ret=0
299
300 ;;
301
302 esac
303
304 ;;
305
306 esac
307 ;;
308 dev)
309
310 # ---- Command: help dev
311 _arguments -s -C \
312 '1: :->cmd' \
313 '2: :->cmd2' \
314 '--version[Show version and exit]' \
315 '-V[Show version and exit]' \
316 '--cwd[Change directory]:cwd' \
317 '--help[Show command help]' \
318 '-h[Show command help]' \
319 '--all[]' &&
320 ret=0
321
322 ;;
323
324 run)
325 # ---- Command: help run
326 _arguments -s -C \
327 '1: :->cmd' \
328 '2: :->cmd2' \
329 '--version[Show version and exit]' \
330 '-V[Show version and exit]' \
331 '--cwd[Change directory]:cwd' \
332 '--help[Show command help]' \
333 '-h[Show command help]' \
334 '--all[]' &&
335 ret=0
336
337 ;;
338
339 upgrade)
340
341 # ---- Command: help upgrade
342 _arguments -s -C \
343 '1: :->cmd' \
344 '2: :->cmd2' \
345 '--version[Show version and exit]' \
346 '-V[Show version and exit]' \
347 '--cwd[Change directory]:cwd' \
348 '--help[Show command help]' \
349 '-h[Show command help]' \
350 '--all[]' &&
351 ret=0
352
353 ;;
354
355 esac
356
357 ;;
358
359 esac
360 ;;
361 install)
362
363 # ---- Command: install
364 _arguments -s -C \
365 '1: :->cmd1' \
366 '--version[Show version and exit]' \
367 '-V[Show version and exit]' \
368 '--help[Show command help]' \
369 '-h[Show command help]' \
370 '--registry[Change default registry (default: \$BUN_CONFIG_REGISTRY || \$npm_config_registry)]:registry' \
371 '--token[Authentication token used for npm registry requests (default: \$npm_config_token)]:token' \
372 '-y[Write a yarn.lock file (yarn v1)]' \
373 '--yarn[Write a yarn.lock file (yarn v1)]' \
374 '--production[Don'"'"'t install devDependencies]' \
375 '-p[Don'"'"'t install devDependencies]' \
376 '--no-save[]' \
377 '--dry-run[Don'"'"'t install anything]' \
378 '--force[Always request the latest versions from the registry & reinstall all dependenices]' \
379 '--lockfile[Store & load a lockfile at a specific filepath]:lockfile' \
380 '--cache-dir[Store & load cached data from a specific directory path]:cache-dir' \
381 '--no-cache[Ignore manifest cache entirely]' \
382 '--silent[Don'"'"'t output anything]' \
383 '--verbose[Excessively verbose logging]' \
384 '--cwd[Set a specific cwd]:cwd' \
385 '-g[Add a package globally]' \
386 '--global[Add a package globally]' \
387 '--backend[Platform-specific optimizations for installing dependencies]:backend:("clonefile" "copyfile" "hardlink" "clonefile_each_dir")' \
388 '--link-native-bins[Link "bin" from a matching platform-specific dependency instead. Default: esbuild, turbo]:link-native-bins' &&
389 ret=0
390
391 ;;
392
393 remove)
394
395 # ---- Command: remove
396 _arguments -s -C \
397 '1: :->cmd1' \
398 '*: :->package' \
399 '--version[Show version and exit]' \
400 '-V[Show version and exit]' \
401 '--help[Show command help]' \
402 '-h[Show command help]' \
403 '--registry[Change default registry (default: \$BUN_CONFIG_REGISTRY || \$npm_config_registry)]:registry' \
404 '--token[Authentication token used for npm registry requests (default: \$npm_config_token)]:token' \
405 '-y[Write a yarn.lock file (yarn v1)]' \
406 '--yarn[Write a yarn.lock file (yarn v1)]' \
407 '--production[Don'"'"'t install devDependencies]' \
408 '-p[Don'"'"'t install devDependencies]' \
409 '--no-save[]' \
410 '--dry-run[Don'"'"'t install anything]' \
411 '-g[Remove a package globally]' \
412 '--global[Remove a package globally]' \
413 '--force[Always request the latest versions from the registry & reinstall all dependenices]' \
414 '--lockfile[Store & load a lockfile at a specific filepath]:lockfile' \
415 '--cache-dir[Store & load cached data from a specific directory path]:cache-dir' \
416 '--no-cache[Ignore manifest cache entirely]' \
417 '--silent[Don'"'"'t output anything]' \
418 '--verbose[Excessively verbose logging]' \
419 '--backend[Platform-specific optimizations for installing dependencies]:backend:("clonefile" "copyfile" "hardlink" "clonefile_each_dir")' \
420 '--link-native-bins[Link "bin" from a matching platform-specific dependency instead. Default: esbuild, turbo]:link-native-bins' &&
421 ret=0
422
423 case $state in
424 package) ;;
425
426 esac
427
428 ;;
429 run)
430 # ---- Command: run
431 _arguments -s -C \
432 '1: :->cmd' \
433 '2: :->script' \
434 '*: :->other' \
435 '--version[Show version and exit]' \
436 '-V[Show version and exit]' \
437 '--cwd[Change directory]:cwd' \
438 '--help[Show command help]' \
439 '-h[Show command help]' \
440 '--silent[Don'"'"'t echo the command]' &&
441 ret=0
442
443 case $state in
444 script)
445 curcontext="${curcontext%:*:*}:bun-grouped"
446 _bun_run_param_script_completion
447 ;;
448 other)
449 _files
450 ;;
451 esac
452
453 ;;
454 upgrade)
455
456 # ---- Command: upgrade
457 _arguments -s -C \
458 '1: :->cmd' \
459 '--version[Show version and exit]' \
460 '-V[Show version and exit]' \
461 '--cwd[Change directory]:cwd' \
462 '--help[Show command help]' \
463 '-h[Show command help]' &&
464 ret=0
465
466 ;;
467
468 esac
469
470 ;;
471
472 esac
473
474}
475
476_bun_run_param_script_completion() {
477 local -a scripts_list
478 IFS=$'\n' scripts_list=($(SHELL=zsh bun getcompletes s))
479 scripts="scripts:scripts:(($scripts_list))"
480
481 IFS=$'\n' bunjs=($(SHELL=zsh bun getcompletes j))
482 IFS=$'\n' bins=($(SHELL=zsh bun getcompletes b))
483
484 if [ -n "$bunjs" ] && [ "$bunjs" != " " ]; then
485 if [ -n "$bins" ] && [ "$bins" != " " ]; then
486 _alternative $scripts "files:files:(($bunjs))" "bin:bin:(($bins))"
487 return 1
488 fi
489
490 _alternative $scripts "args:Bun.js:(($bunjs))"
491 fi
492
493 if [ -n "$bins" ] && [ "$bins" != " " ]; then
494 _alternative $scripts "args:bin:(($bins))"
495 return 1
496 fi
497}
498
499_set_remove() {
500 comm -23 <(echo $1 | sort | tr " " "\n") <(echo $2 | sort | tr " " "\n") 2>/dev/null
501}
502
503_bun_add_param_package_completion() {
504
505 IFS=$'\n' inexact=($(history -n bun | grep -E "^bun add " | cut -c 9- | uniq))
506 IFS=$'\n' exact=($($inexact | grep -E "^$words[$CURRENT]"))
507 IFS=$'\n' packages=($(SHELL=zsh bun getcompletes a $words[$CURRENT]))
508
509 to_print=$inexact
510 if [ ! -z "$exact" -a "$exact" != " " ]; then
511 to_print=$exact
512 fi
513
514 if [ ! -z "$to_print" -a "$to_print" != " " ]; then
515 if [ ! -z "$packages" -a "$packages" != " " ]; then
516 _describe -1 -t to_print 'History' to_print
517 _describe -1 -t packages "Popular" packages
518 return
519 fi
520
521 _describe -1 -t to_print 'History' to_print
522 return
523 fi
524
525 if [ ! -z "$packages" -a "$packages" != " " ]; then
526 _describe -1 -t packages "Popular" packages
527 return
528 fi
529
530}
531
532__bun_dynamic_comp() {
533 local comp=""
534
535 for arg in scripts; do
536 local line
537 while read -r line; do
538 local name="$line"
539 local desc="$line"
540 name="${name%$'\t'*}"
541 desc="${desc/*$'\t'/}"
542 echo
543 done <<<"$arg"
544 done
545
546 return $comp
547}
548
549autoload -U compinit && compinit
550compdef _bun bun