#!/bin/bash # Ported from deno: # Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. # https://github.com/denoland/deno/blob/master/LICENSE set -e deno_install="${DENO_INSTALL:-$HOME/.deno}" bin_dir="$deno_install/bin" download_deno() { local target_version=$1 if [ "$(uname -m)" != "x86_64" ]; then echo "Error: Unsupported architecture $(uname -m). Only x64 binaries are available." 1>&2 exit 1 fi if ! command -v unzip >/dev/null; then echo "Error: unzip is required to install Deno (see: https://github.com/denoland/deno_install#unzip-is-required)." 1>&2 exit 1 fi if [ "$OS" = "Windows_NT" ]; then local target="x86_64-pc-windows-msvc" else case $(uname -s) in Darwin) local target="x86_64-apple-darwin" ;; *) local target="x86_64-unknown-linux-gnu" ;; esac fi if [ -z "$target_version" ]; then local deno_asset_path=$( curl -sSf https://github.com/denoland/deno/releases | grep -o "/denoland/deno/releases/download/.*/deno-${target}\\.zip" | head -n 1 ) if [ ! "$deno_asset_path" ]; then echo "Error: Unable to find latest Deno release on GitHub." 1>&2 exit 1 fi echo "deno is not found, devo will download it first" local deno_uri="https://github.com${deno_asset_path}" else echo "deno $target_version is not found, devo will download it first" local deno_uri="https://github.com/denoland/deno/releases/download/${version}/deno-${target}.zip" fi local devo_dir="$bin_dir/.devo" local deno_zip_path="$devo_dir/$deno_fname.zip" if [ ! -d "$bin_dir" ]; then mkdir -p "$bin_dir" fi mkdir -p "$devo_dir" curl --fail --location --progress-bar --output "$deno_zip_path" "$deno_uri" cd "$devo_dir" unzip -o "$deno_zip_path" -d "$deno_fname" > /dev/null chmod +x "$deno_fname/deno" mv "$deno_fname/deno" "../$deno_fname" rm -r "$deno_fname" "$deno_fname.zip" echo "Deno was installed successfully to $bin_dir/$deno_fname" if command -v deno >/dev/null; then echo "Run 'deno --help' to get started" else case $SHELL in /bin/zsh) shell_profile=".zshrc" ;; *) shell_profile=".bash_profile" ;; esac echo "Manually add the directory to your \$HOME/$shell_profile (or similar)" echo " export DENO_INSTALL=\"$deno_install\"" echo " export PATH=\"\$DENO_INSTALL/bin:\$PATH\"" echo "Run '$bin_dir/$deno_fname --help' to get started" fi } if [ -e ".devo" ]; then version="$(cat .devo)" fi reg="^v[0-9]+\.[0-9]+\.[0-9]+$" if [[ $1 =~ $reg ]]; then version="$1" fi if [ "$1" = "global" ]; then if [ -z $2 ]; then echo "Specify the version" exit 1 fi version="$2" fi if [ -z "$version" ]; then deno_fname="deno" else deno_fname="deno_$version" fi exec="$bin_dir/$deno_fname" if [ ! -e "$exec" ]; then download_deno "$version" fi if [[ $1 =~ $reg ]]; then "$exec" ${@:2} elif [ "$1" = "global" ]; then cp "$exec" "$bin_dir/deno" echo "Global version change successful" else "$exec" "$@" fi