Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Last active November 2, 2018 13:39
Show Gist options
  • Save tobiashm/da3aba3071c826c742bb02e2ad0d14ba to your computer and use it in GitHub Desktop.
Save tobiashm/da3aba3071c826c742bb02e2ad0d14ba to your computer and use it in GitHub Desktop.

Revisions

  1. tobiashm revised this gist Nov 2, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions use-node.sh
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,10 @@
    # I.e. other terminal windows will have the default Node version, unless they’re also changed.
    use-node() {
    wanted=$([ "$1" == "" ] && echo "node" || echo "node@$1")
    if brew ls "$wanted" &>/dev/null; then
    PATH="$(brew --prefix $wanted)/bin:$(echo $PATH | tr ':' '\n' | grep -v $(brew --prefix node) | tr '\n' ':')"
    prefix=$(brew --prefix $wanted 2> /dev/null)
    if [ "$prefix" != "" ]; then
    PATH="$(echo $PATH | tr ':' '\n' | grep -v ${prefix%@*} | tr '\n' ':')"
    if [ "$1" != "" ]; then PATH="$prefix/bin:$PATH"; fi
    export PATH
    else
    echo "Node version '$1' not available"
  2. tobiashm revised this gist Nov 2, 2018. 1 changed file with 5 additions and 9 deletions.
    14 changes: 5 additions & 9 deletions use-node.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,14 @@
    # Assumes you have installed individual major versions with Homebrew, like node@8,
    # this command allows you to switch by calling e.g. `use-node 8`, or to get the
    # current latest version (`brew install node`) if you don't pass an argument.
    # Notice: Unlike e.g. `nvm` this will change the node version for all sessions,
    # since it changes the `/usr/local/bin/node` symlink.
    # Notice: Since it modifies the env, this will only work for the current session.
    # I.e. other terminal windows will have the default Node version, unless they’re also changed.
    use-node() {
    wanted=$([ "$1" == "" ] && echo "node" || echo "node@$1")
    if brew ls "$wanted" &>/dev/null; then
    if which node &> /dev/null; then
    for possible in $(brew ls | grep -E "^node($|@)"); do
    brew unlink $possible > /dev/null
    done
    fi
    brew link --force --overwrite $wanted > /dev/null
    PATH="$(brew --prefix $wanted)/bin:$(echo $PATH | tr ':' '\n' | grep -v $(brew --prefix node) | tr '\n' ':')"
    export PATH
    else
    echo "Node version '$1' not available"
    fi
    }
    }
  3. tobiashm created this gist Nov 2, 2018.
    18 changes: 18 additions & 0 deletions use-node.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    # Assumes you have installed individual major versions with Homebrew, like node@8,
    # this command allows you to switch by calling e.g. `use-node 8`, or to get the
    # current latest version (`brew install node`) if you don't pass an argument.
    # Notice: Unlike e.g. `nvm` this will change the node version for all sessions,
    # since it changes the `/usr/local/bin/node` symlink.
    use-node() {
    wanted=$([ "$1" == "" ] && echo "node" || echo "node@$1")
    if brew ls "$wanted" &>/dev/null; then
    if which node &> /dev/null; then
    for possible in $(brew ls | grep -E "^node($|@)"); do
    brew unlink $possible > /dev/null
    done
    fi
    brew link --force --overwrite $wanted > /dev/null
    else
    echo "Node version '$1' not available"
    fi
    }