#!/bin/sh set -eu usage() { cat <<'EOF' Install the standalone Turborepo CLI without Node.js or a JavaScript package manager. Usage: install [--version VERSION] [--install-dir DIRECTORY] [--no-modify-path] install --uninstall [--install-dir DIRECTORY] By default, the installer uses the latest stable release and installs to ~/.local/bin. Set TURBO_VERSION or TURBO_INSTALL_DIR to configure this in CI. --version VERSION An exact version, "latest" (default), or "canary" for the newest canary release. The installer adds the install directory to PATH. It does not activate managed tools in your shell. --no-modify-path Do not edit shell profiles (TURBO_NO_MODIFY_PATH=1). --uninstall Remove the turbo binary, PATH entry, and any shell hook added by an older installer for the install directory, and nothing else (TURBO_UNINSTALL=1). EOF } fail() { printf '%s\n' "$1" >&2 exit 1 } version=${TURBO_VERSION:-} install_dir=${TURBO_INSTALL_DIR:-} no_modify_path=${TURBO_NO_MODIFY_PATH:-0} uninstall=${TURBO_UNINSTALL:-0} while [ "$#" -gt 0 ]; do case "$1" in --uninstall) uninstall=1 shift ;; --no-modify-path) no_modify_path=1 shift ;; --version) [ "$#" -ge 2 ] || fail "--version requires a value" version=$2 shift 2 ;; --install-dir) [ "$#" -ge 2 ] || fail "--install-dir requires a value" install_dir=$2 shift 2 ;; --help|-h) usage exit 0 ;; --) shift break ;; *) fail "unknown option: $1" ;; esac done [ "$#" -eq 0 ] || fail "unexpected argument: $1" [ -n "${HOME:-}" ] || fail 'HOME is not set' [ -n "$install_dir" ] || install_dir="$HOME/.local/bin" case "$install_dir" in /*) ;; *) fail "install directory must be an absolute path: $install_dir" ;; esac newline=$(printf '\n_') newline=${newline%_} carriage_return=$(printf '\r') case "$install_dir" in *"$newline"*|*"$carriage_return"*|*:*) fail 'install directory must not contain a colon or line break' ;; esac os=$(uname -s) architecture=$(uname -m) case "$os:$architecture" in Darwin:x86_64) target=x86_64-apple-darwin ;; Darwin:arm64|Darwin:aarch64) target=aarch64-apple-darwin ;; Linux:x86_64|Linux:amd64) target=x86_64-unknown-linux-musl ;; Linux:arm64|Linux:aarch64) target=aarch64-unknown-linux-musl ;; *) fail "unsupported platform: $os/$architecture" ;; esac binary_name=turbo destination="$install_dir/$binary_name" if [ "$uninstall" != 1 ] && existing_turbo=$(command -v turbo 2>/dev/null); then if [ "$existing_turbo" != "$destination" ]; then fail "Found an existing turbo on PATH at $existing_turbo. Uninstall it using the tool that installed it, then rerun: curl -fsSL https://turborepo.dev/install | sh" fi fi check_destination() { if [ -L "$destination" ] || { [ -e "$destination" ] && [ ! -f "$destination" ]; }; then fail "$destination is not a regular file; it was left untouched. Remove it yourself or choose another --install-dir." fi } [ "$uninstall" = 1 ] || check_destination for utility in curl tar awk grep sed head mktemp mkdir cp chmod mv ln rm tr; do command -v "$utility" >/dev/null 2>&1 || fail "required command not found: $utility" done work_dir=$(mktemp -d "${TMPDIR:-/tmp}/turbo-install.XXXXXX") || fail 'could not create a temporary directory' staged_binary= cleanup() { if [ -n "$staged_binary" ]; then rm -f "$staged_binary" fi rm -rf "$work_dir" } trap cleanup 0 trap 'exit 130' INT trap 'exit 143' TERM shell_quote() { quoted=$(printf '%s' "$1" | sed "s/'/'\\\\''/g") printf "'%s'" "$quoted" } path_marker="# Turborepo standalone installer PATH: $install_dir" path_block() { quoted_install_dir=$(shell_quote "$install_dir") printf '%s\n' "$path_marker" printf 'case ":$PATH:" in\n' printf ' *:%s:*) ;;\n' "$quoted_install_dir" printf ' *) PATH=%s:"$PATH" ;;\n' "$quoted_install_dir" printf 'esac\nexport PATH\n' } append_path_profile() { profile=$1 if [ -f "$profile" ] && grep -Fqx "$path_marker" "$profile"; then return 0 fi { printf '\n'; path_block; } >> "$profile" } # Retained only so uninstall can remove hooks written by older installers. hook_marker='# Turborepo standalone installer shell hook' hook_line_for() { printf 'eval "$(%s hook %s)" %s' "$(shell_quote "$destination")" "$1" "$hook_marker" } # Removes this installation's PATH block and hook lines, with the blank line # the installer wrote before each, so the profile returns to its prior bytes. # Exits 3 when nothing matches. remove_profile_lines() { PATH_BLOCK=$(path_block) HOOK_ZSH=$(hook_line_for zsh) HOOK_BASH=$(hook_line_for bash) awk ' function drop(first, count) { for (j = first; j < first + count; j++) dropped[j] = 1 removed = 1 if (first > 1 && !dropped[first - 1]) { if (line[first - 1] == "") dropped[first - 1] = 1 else unterminated[first - 1] = 1 } } { line[NR] = $0 } END { for (i = 1; i <= NR; i++) { block = line[i] for (k = 1; k <= 5 && i + k <= NR; k++) block = block "\n" line[i + k] if (line[i] == ENVIRON["HOOK_ZSH"] || line[i] == ENVIRON["HOOK_BASH"]) drop(i, 1) else if (block == ENVIRON["PATH_BLOCK"]) { drop(i, 6); i += 5 } } if (!removed) exit 3 last = 0 for (i = 1; i <= NR; i++) if (!dropped[i]) last = i for (i = 1; i <= last; i++) { if (dropped[i]) continue # The installer ended this line when the profile had no final newline. if (i == last && unterminated[i]) printf "%s", line[i] else print line[i] } } ' "$1" } uninstall_profile() { profile=$1 [ -f "$profile" ] || return 0 had_path=0 had_hook=0 if grep -Fqx "$path_marker" "$profile"; then had_path=1; fi if grep -Fqx -e "$(hook_line_for zsh)" -e "$(hook_line_for bash)" "$profile"; then had_hook=1; fi removal_status=0 remove_profile_lines "$profile" > "$work_dir/profile" || removal_status=$? case "$removal_status" in 0) if [ -s "$work_dir/profile" ] || [ -L "$profile" ]; then cat "$work_dir/profile" > "$profile" || return 1 else rm -f "$profile" || return 1 fi ;; 3) ;; *) return 1 ;; esac if [ "$had_path" = 1 ] && ! grep -Fqx "$path_marker" "$profile" 2>/dev/null; then printf 'Removed the turbo PATH entry from %s\n' "$profile" fi if [ "$had_hook" = 1 ]; then printf 'Removed the turbo shell hook from %s\n' "$profile" fi if grep -Fq '# Turborepo standalone installer ' "$profile" 2>/dev/null; then printf 'Left lines in %s that are marked by the installer but do not match this installation.\n' "$profile" fi } # Removes only what this installer owns: a regular file at the destination # (the same rule upgrades use) and its own marked profile lines. uninstall_turbo() { if [ -L "$destination" ] || { [ -e "$destination" ] && [ ! -f "$destination" ]; }; then printf 'Left %s because it is not a regular file.\n' "$destination" elif [ -f "$destination" ]; then rm -f "$destination" || fail "could not remove $destination" printf 'Removed %s\n' "$destination" else printf 'No turbo found at %s\n' "$destination" fi for profile in "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.bash_login" "$HOME/.bashrc" "$HOME/.zprofile" "$HOME/.zshrc"; do uninstall_profile "$profile" || fail "could not update $profile" done fish_config="$HOME/.config/fish/config.fish" if [ -f "$fish_config" ] && grep -Fq "$(shell_quote "$destination") hook fish" "$fish_config"; then printf 'Left the turbo hook line in %s because the installer did not add it; remove it yourself.\n' "$fish_config" fi if other_turbo=$(command -v turbo 2>/dev/null) && [ "$other_turbo" != "$destination" ]; then printf 'Left %s because the installer did not install it.\n' "$other_turbo" fi } if [ "$uninstall" = 1 ]; then uninstall_turbo exit 0 fi download() { curl --fail --show-error --silent --location \ --proto '=https' --proto-redir '=https' "$@" } github_api() { download --output "$1" \ --header 'Accept: application/vnd.github+json' \ --header 'X-GitHub-Api-Version: 2022-11-28' \ "https://api.github.com/repos/vercel/turborepo/$2" } metadata="$work_dir/release.json" case "$version" in ''|latest) github_api "$metadata" releases/latest || fail 'could not resolve the latest stable release from GitHub' version=$(sed -nE 's/.*"tag_name"[[:space:]]*:[[:space:]]*"v?([^"]+)".*/\1/p' "$metadata" | head -n 1) ;; canary) # Releases are listed newest first. Splitting on commas keeps the first # match first even if GitHub returns the JSON on a single line. github_api "$metadata" 'releases?per_page=30' || fail 'could not list releases from GitHub' version=$(tr ',' '\n' < "$metadata" \ | sed -nE 's/.*"tag_name"[[:space:]]*:[[:space:]]*"v?([0-9]+\.[0-9]+\.[0-9]+-canary\.[0-9]+)".*/\1/p' \ | head -n 1) [ -n "$version" ] || fail 'could not find a canary release on GitHub' ;; esac case "$version" in v*) version=${version#v} ;; esac if ! printf '%s\n' "$version" | LC_ALL=C grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?$'; then fail "invalid release version: ${version:-}" fi archive_name="turbo-$version-$target.tar.gz" release_url="https://github.com/vercel/turborepo/releases/download/v$version" manifest="$work_dir/SHA256SUMS" archive="$work_dir/$archive_name" if ! download --output "$manifest" "$release_url/SHA256SUMS"; then fail "could not download the checksum manifest for v$version" fi if ! download --output "$archive" "$release_url/$archive_name"; then fail "could not download the standalone archive for $target at v$version" fi expected_checksum=$(awk -v filename="$archive_name" ' $2 == filename { count++ if (NF == 2) digest = $1 else digest = "" } END { if (count == 1) print digest } ' "$manifest") if ! printf '%s\n' "$expected_checksum" | LC_ALL=C grep -Eq '^[[:xdigit:]]{64}$'; then fail "checksum manifest must contain exactly one SHA-256 entry for $archive_name" fi if command -v sha256sum >/dev/null 2>&1; then actual_checksum=$(sha256sum "$archive" | awk '{print $1}') elif command -v shasum >/dev/null 2>&1; then actual_checksum=$(shasum -a 256 "$archive" | awk '{print $1}') else fail 'a SHA-256 utility (sha256sum or shasum) is required' fi expected_checksum=$(printf '%s' "$expected_checksum" | tr '[:upper:]' '[:lower:]') actual_checksum=$(printf '%s' "$actual_checksum" | tr '[:upper:]' '[:lower:]') [ "$actual_checksum" = "$expected_checksum" ] || fail "SHA-256 verification failed for $archive_name" members=$(tar -tzf "$archive") || fail "could not inspect $archive_name" [ "$members" = "$binary_name" ] || fail "unexpected archive contents in $archive_name; expected only $binary_name" extracted_dir="$work_dir/extracted" mkdir "$extracted_dir" tar -xzf "$archive" -C "$extracted_dir" || fail "could not extract $archive_name" extracted_binary="$extracted_dir/$binary_name" [ -f "$extracted_binary" ] && [ ! -L "$extracted_binary" ] || fail 'archive did not contain a regular turbo executable' chmod 0755 "$extracted_binary" mkdir -p "$install_dir" || fail "could not create install directory: $install_dir" check_destination staged_binary=$(mktemp "$install_dir/.turbo-install.XXXXXX") || fail "could not create a staging file in $install_dir" if ! cp "$extracted_binary" "$staged_binary" || ! chmod 0755 "$staged_binary"; then fail "could not stage turbo in $install_dir" fi check_destination if [ -e "$destination" ]; then action=Upgraded # A rename in the same directory replaces the old binary without a missing-file window. if ! mv -f "$staged_binary" "$destination"; then fail "could not replace $destination" fi else action=Installed if ! ln "$staged_binary" "$destination"; then fail "could not install $destination without replacing an existing file" fi fi rm -f "$staged_binary" staged_binary= configure_path() { if [ -n "${GITHUB_PATH:-}" ]; then if ! grep -Fqx "$install_dir" "$GITHUB_PATH"; then printf '%s\n' "$install_dir" >> "$GITHUB_PATH" || return 1 fi return 0 fi [ "$no_modify_path" != 1 ] || return 1 shell_name=${SHELL:-} shell_name=${shell_name##*/} case "$shell_name" in zsh) append_path_profile "$HOME/.zprofile" || return 1 append_path_profile "$HOME/.zshrc" || return 1 ;; bash) if [ -f "$HOME/.bash_profile" ]; then login_profile="$HOME/.bash_profile" elif [ -f "$HOME/.bash_login" ]; then login_profile="$HOME/.bash_login" else login_profile="$HOME/.profile" fi append_path_profile "$login_profile" || return 1 append_path_profile "$HOME/.bashrc" || return 1 ;; fish) return 1 ;; *) append_path_profile "$HOME/.profile" || return 1 ;; esac } printf '%s turbo %s at %s\n' "$action" "$version" "$destination" if configure_path; then case ":${PATH:-}:" in *":$install_dir:"*) ;; *) printf 'PATH configured for future shells; open a new shell to run `turbo` by name.\n' ;; esac else printf 'Add %s to PATH to run `turbo` by name.\n' "$install_dir" fi