bun
bun copied to clipboard
Improve ZSH completions
Refactoring the code and adding improvements
Autocomplete for --define flag
- Reads the environment variables and autocomplete them in json format
- Autocomplete the environment variable
process.env.NODE_ENV

Autocomplete for --load flag
Auto complete file extensions with valid loaders

Fixed bug when autocompleting recent packages
Previously, when adding multiple packages in the same command, the autocomplete did not work well.
bun add react vite esbuild nanoid
Before:

After: Segmentation in
- recent
- popular

Autocomplete for remove command
Segmentation in
- Dependencies
- Dev Dependencies
- Optional Dependencies
- Peer Dependencies

Segmentation for the run command
Segmentation in
- files
- binaries
- scripts

Overall, this is great. The only part I'm not sure about is cross-platform compatibility with grep and sed
I ran into a similar issue with bun's Makefile, ended up doing this:
SED = $(shell which gsed || which sed)
homebrew aliases GNU versions of coreutils with g, so ggrep and gsed – but not everyone bothers to install the coreutils (or grep) homebrew package
Maybe we could assume the commands will work but re-run without the sed or grep or jq specific features if it returns a non-zero exit code? That way the happy path still works
Updating RegExp to be compatible with MacOS and BSD and checking if the jq command is installed
- RegExp
Since grep and sed in Macos don't support Perl RegExp I updated to BRE RegExp.
Before:
_envs=(
${(f)"$(export | sed -r "/^\w+=(true|false|null|[0-9]+)$/! s/='?(.*?)('|$)/=\"\1\"/")"}
)
_recent=($(history -n | grep -oP '(?<=^bun add ).+'))
After:
_envs=(
${(f)"$(export | sed "/^\w\+=\(true\|false\|null\|[0-9]\+\)$/! s/='\?\(.*\)\('\|$\)/=\"\1\"/")"}
)
_recent=($(history -n | sed -n 's/^\(bun[[:space:]]\+add[[:space:]]\+\)\(.\+\)/\2/p'))
- jq
Since jq is not an essential tool for distributions there is a chance that it is not installed, so I added a check to determine if it is installed.
if ! command -v jq >/dev/null 2>&1; then
_message 'jq is required to complete bun remove command'
return
fi
Unfortunately, sed has a similar issue on macOS as grep
bun add completions don't work after the changes:

bun remove works though!

Would you mind providing more information about the error? Unfortunately I don't have a MacOs to perform the necessary tests.
Please run this and show me the output:
# Test of the `add` command:
history -n | sed -n 's/^\(bun[[:space:]]\+add[[:space:]]\+\)\(.\+\)/\2/p'
# Test of the `--define` option:
export | sed "/^\w\+=\(true\|false\|null\|[0-9]\+\)$/! s/='\?\(.*\)\('\|$\)/=\"\1\"/"
@srealmoreno if this is still relevant: on Mac, it would work like this
history -n | sed -n 's/^\(bun[[:space:]]\{1,\}add[[:space:]]\{1,\}\)\(.\{1,\}\)$/\2/p'
or with extended regex:
history -n | sed -nE 's/^(bun[[:space:]]+add[[:space:]]+)(.+)$/\2/p'
Similarly for export (slightly corrected version):
export | sed -E "/^[[:alnum:]_]+=(true|false|null|[0-9])$/! s/='(.*)'$|=(.*)$/=\"\1\2\"/"
(branches don't work in BRE if I'm not mistaken)
@alexkuz Thanks, I'll add it right now.