aboutsummaryrefslogtreecommitdiff
path: root/src/cli
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-11-01 03:43:27 -0700
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-11-01 03:43:27 -0700
commit173724b4020cae1f0caacb5116a31aec86f2c2db (patch)
tree72bb511823910a8d5fe0697e33b7648d3827ecef /src/cli
parent64d3927879e9af94eaf1aa7f304ab28f03e7a6bb (diff)
downloadbun-173724b4020cae1f0caacb5116a31aec86f2c2db.tar.gz
bun-173724b4020cae1f0caacb5116a31aec86f2c2db.tar.zst
bun-173724b4020cae1f0caacb5116a31aec86f2c2db.zip
[CLI] Add zsh completions and auto-install completions
Diffstat (limited to 'src/cli')
-rw-r--r--src/cli/install.sh97
-rw-r--r--src/cli/run_command.zig21
-rw-r--r--src/cli/shell_completions.zig16
3 files changed, 97 insertions, 37 deletions
diff --git a/src/cli/install.sh b/src/cli/install.sh
index 3e487f448..e49d69f64 100644
--- a/src/cli/install.sh
+++ b/src/cli/install.sh
@@ -13,41 +13,40 @@ BGreen=''
if test -t 1; then
# Reset
- Color_Off='\033[0m' # Text Reset
+ Color_Off='\033[0m' # Text Reset
# Regular Colors
- Red='\033[0;31m' # Red
- Green='\033[0;32m' # Green
- White='\033[0;37m' # White
+ Red='\033[0;31m' # Red
+ Green='\033[0;32m' # Green
+ White='\033[0;37m' # White
# Bold
- BGreen='\033[1;32m' # Green
- BWhite='\033[1;37m' # White
+ BGreen='\033[1;32m' # Green
+ BWhite='\033[1;37m' # White
fi
-
if ! command -v unzip >/dev/null; then
- echo -e "${Red}error${Color_Off}: unzip is required to install Bun (see: https://github.com/Jarred-Sumner/bun#unzip-is-required)." 1>&2
- exit 1
+ echo -e "${Red}error${Color_Off}: unzip is required to install Bun (see: https://github.com/Jarred-Sumner/bun#unzip-is-required)." 1>&2
+ exit 1
fi
if [ "$OS" = "Windows_NT" ]; then
- echo "error: Please install Bun using Windows Subsystem for Linux."
+ echo "error: Please install Bun using Windows Subsystem for Linux."
exit 1
else
- case $(uname -sm) in
- "Darwin x86_64") target="darwin-x64" ;;
- "Darwin arm64") target="darwin-aarch64" ;;
- *) target="linux-x64" ;;
- esac
+ case $(uname -sm) in
+ "Darwin x86_64") target="darwin-x64" ;;
+ "Darwin arm64") target="darwin-aarch64" ;;
+ *) target="linux-x64" ;;
+ esac
fi
github_repo="https://github.com/Jarred-Sumner/bun-releases-for-updater"
if [ $# -eq 0 ]; then
- bun_uri="$github_repo/releases/latest/download/bun-${target}.zip"
+ bun_uri="$github_repo/releases/latest/download/bun-${target}.zip"
else
- bun_uri="$github_repo/releases/download/${1}/bun-${target}.zip"
+ bun_uri="$github_repo/releases/download/${1}/bun-${target}.zip"
fi
bun_install="${BUN_INSTALL:-$HOME/.bun}"
@@ -55,27 +54,32 @@ bin_dir="$bun_install/bin"
exe="$bin_dir/bun"
if [ ! -d "$bin_dir" ]; then
- mkdir -p "$bin_dir"
+ mkdir -p "$bin_dir"
+
+ if (($?)); then
+ echo -e "${Red}error${Color_Off}: Failed to create install directory $bin_dir" 1>&2
+ exit 1
+ fi
fi
curl --fail --location --progress-bar --output "$exe.zip" "$bun_uri"
-if (( $? )); then
+if (($?)); then
echo -e "${Red}error${Color_Off}: Failed to download Bun from $bun_uri" 1>&2
exit 1
fi
unzip -d "$bin_dir" -q -o "$exe.zip"
-if (( $? )); then
+if (($?)); then
echo -e "${Red}error${Color_Off}: Failed to extract Bun" 1>&2
exit 1
fi
mv "$bin_dir/bun-${target}/bun" "$exe"
-if (( $? )); then
+if (($?)); then
echo -e "${Red}error${Color_Off}: Failed to extract Bun" 1>&2
exit 1
fi
chmod +x "$exe"
-if (( $? )); then
+if (($?)); then
echo -e "${Red}error${Color_Off}: Failed to set permissions on bun executable." 1>&2
exit 1
fi
@@ -85,22 +89,47 @@ rm "$exe.zip"
echo -e "${Green}Bun was installed successfully to ${BGreen}$exe$Color_Off"
if command -v bun --version >/dev/null; then
+ # Install completions, but we don't care if it fails
+ IS_BUN_AUTO_UPDATE="true" $exe completions >/dev/null 2>&1
+
echo "Run 'bun --help' to get started"
exit 0
fi
if test $(basename $SHELL) == "fish"; then
- echo ""
- echo "Manually add the directory to your \$HOME/.config/fish"
- echo ""
- echo -e " $BWhite set -Ux BUN_INSTALL \"$bun_install\"$Color_Off"
- echo -e " $BWhite set -px --path PATH \"$bin_dir\"$Color_Off"
-elif test $(basename $SHELL) == "zsh"; then
- echo ""
- echo "Manually add the directory to your \$HOME/.zshrc (or similar)"
- echo ""
- echo -e " $BWhite export BUN_INSTALL=\"$bun_install\"$Color_Off"
- echo -e " $BWhite export PATH=\"\$BUN_INSTALL/bin:\$PATH\"$Color_Off"
+ # Install completions, but we don't care if it fails
+ IS_BUN_AUTO_UPDATE="true" SHELL="fish" $exe completions >/dev/null 2>&1
+ if test -f $HOME/.config/fish; then
+ echo -e "\nset -Ux BUN_INSTALL \"$bun_install\"\n" >>"$HOME/.config/fish"
+ echo -e "\nset -px --path PATH \"$bin_dir\"\n" >>"$HOME/.config/fish"
+ echo ""
+ echo -e "$BWhite Added \"$bin_dir\" to PATH in \"$HOME/.config/fish\"$Color_Off"
+ else
+ echo ""
+ echo "Manually add the directory to your \$HOME/.config/fish"
+ echo ""
+ echo -e " $BWhite set -Ux BUN_INSTALL \"$bun_install\"$Color_Off"
+ echo -e " $BWhite set -px --path PATH \"$bin_dir\"$Color_Off"
+ fi
+elif
+ test $(basename $SHELL) == "zsh"
+then
+ # Install completions, but we don't care if it fails
+ IS_BUN_AUTO_UPDATE="true" SHELL="zsh" $exe completions >/dev/null 2>&1
+
+ if test -f $HOME/.zshrc; then
+ echo -e "export BUN_INSTALL=\"$bun_install\"" >>"$HOME/.zshrc"
+ echo -e "export PATH=\"\$BUN_INSTALL/bin:\$PATH\"" >>"$HOME/.zshrc"
+ echo ""
+ echo -e "$BWhite Added \"$bin_dir\" to PATH in \"$HOME/.zshrc\"$Color_Off"
+ else
+ echo ""
+ echo "Manually add the directory to your \$HOME/.zshrc (or similar)"
+ echo ""
+ echo -e " $BWhite export BUN_INSTALL=\"$bun_install\"$Color_Off"
+ echo -e " $BWhite export PATH=\"\$BUN_INSTALL/bin:\$PATH\"$Color_Off"
+ fi
+
else
echo ""
echo "Manually add the directory to your \$HOME/.bashrc (or similar)"
@@ -111,4 +140,4 @@ fi
echo ""
echo -e "To get started, run"
echo -e "$BWhite"
-echo -e " bun --help$Color_Off" \ No newline at end of file
+echo -e " bun --help$Color_Off"
diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig
index 60688e5a9..4593f5f6a 100644
--- a/src/cli/run_command.zig
+++ b/src/cli/run_command.zig
@@ -273,6 +273,8 @@ pub const RunCommand = struct {
script,
bin,
all,
+ bun_js,
+ all_plus_bun_js,
};
pub fn completions(ctx: Command.Context, default_completions: ?[]const string, comptime filter: Filter) !ShellCompletions {
@@ -327,7 +329,7 @@ pub const RunCommand = struct {
}
}
- if (filter == Filter.bin or filter == Filter.all) {
+ if (filter == Filter.bin or filter == Filter.all or filter == Filter.all_plus_bun_js) {
for (this_bundler.resolver.binDirs()) |bin_path| {
if (this_bundler.resolver.readDirInfo(bin_path) catch null) |bin_dir| {
if (bin_dir.getEntriesConst()) |entries| {
@@ -360,7 +362,22 @@ pub const RunCommand = struct {
}
}
- if (filter == Filter.script or filter == Filter.all) {
+ if (filter == Filter.all_plus_bun_js or filter == Filter.bun_js) {
+ if (this_bundler.resolver.readDirInfo(this_bundler.fs.top_level_dir) catch null) |dir_info| {
+ if (dir_info.getEntriesConst()) |entries| {
+ var iter = entries.data.iterator();
+
+ while (iter.next()) |entry| {
+ const name = entry.value.base();
+ if (this_bundler.options.loader(std.fs.path.extension(name)).isJavaScriptLike() and !strings.contains(name, ".d.ts") and entry.value.kind(&this_bundler.fs.fs) == .file) {
+ _ = try results.getOrPut(this_bundler.fs.filename_store.append(@TypeOf(name), name) catch continue);
+ }
+ }
+ }
+ }
+ }
+
+ if (filter == Filter.script or filter == Filter.all or filter == Filter.all_plus_bun_js) {
if (root_dir_info.enclosing_package_json) |package_json| {
if (package_json.scripts) |scripts| {
try results.ensureUnusedCapacity(scripts.count());
diff --git a/src/cli/shell_completions.zig b/src/cli/shell_completions.zig
index f64e7befb..295593d49 100644
--- a/src/cli/shell_completions.zig
+++ b/src/cli/shell_completions.zig
@@ -7,6 +7,19 @@ pub const Shell = enum {
zsh,
fish,
+ const bash_completions = @embedFile("../../completions/bun.bash");
+ const zsh_completions = @embedFile("../../completions/bun.zsh");
+ const fish_completions = @embedFile("../../completions/bun.fish");
+
+ pub fn completions(this: Shell) []const u8 {
+ return switch (this) {
+ .bash => std.mem.span(bash_completions),
+ .zsh => std.mem.span(zsh_completions),
+ .fish => std.mem.span(fish_completions),
+ else => "",
+ };
+ }
+
pub fn fromEnv(comptime Type: type, SHELL: Type) Shell {
const basename = std.fs.path.basename(SHELL);
if (strings.eqlComptime(basename, "bash")) {
@@ -30,12 +43,13 @@ pub fn print(this: @This()) void {
var writer = Output.writer();
if (this.commands.len == 0) return;
+ const delimiter = if (this.shell == Shell.fish) " " else "\n";
writer.writeAll(this.commands[0]) catch return;
if (this.commands.len > 1) {
for (this.commands[1..]) |cmd, i| {
- writer.writeAll(" ") catch return;
+ writer.writeAll(delimiter) catch return;
writer.writeAll(cmd) catch return;
}