From 1a0f9399936be25717a0acdd5d60da7d24b3175a Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sat, 15 Nov 2014 22:26:37 +0100 Subject: [PATCH 1/7] Introduce nvm use --print-paths It will print all paths that have been changed by nvm use. These paths are exported for all later nvm commands and need to be applied to the environment of other shells (in this case fish), therefore they are printed with the option. --- nvm.sh | 69 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/nvm.sh b/nvm.sh index 969af37d06..5f02501154 100644 --- a/nvm.sh +++ b/nvm.sh @@ -531,22 +531,22 @@ nvm() { echo "Node Version Manager" echo echo "Usage:" - echo " nvm help Show this message" - echo " nvm --version Print out the latest released version of nvm" - echo " nvm install [-s] Download and install a , [-s] from source. Uses .nvmrc if available" - echo " nvm uninstall Uninstall a version" - echo " nvm use Modify PATH to use . Uses .nvmrc if available" - echo " nvm run [] Run with as arguments. Uses .nvmrc if available for " - echo " nvm current Display currently activated version" - echo " nvm ls List installed versions" - echo " nvm ls List versions matching a given description" - echo " nvm ls-remote List remote versions available for install" - echo " nvm deactivate Undo effects of NVM on current shell" - echo " nvm alias [] Show all aliases beginning with " - echo " nvm alias Set an alias named pointing to " - echo " nvm unalias Deletes the alias named " - echo " nvm copy-packages Install global NPM packages contained in to current version" - echo " nvm unload Unload NVM from shell" + echo " nvm help Show this message" + echo " nvm --version Print out the latest released version of nvm" + echo " nvm install [-s] Download and install a , [-s] from source. Uses .nvmrc if available" + echo " nvm uninstall Uninstall a version" + echo " nvm [--print-paths] use Modify PATH (and optionally print it) to use . Uses .nvmrc if available" + echo " nvm run [] Run with as arguments. Uses .nvmrc if available for " + echo " nvm current Display currently activated version" + echo " nvm ls List installed versions" + echo " nvm ls List versions matching a given description" + echo " nvm ls-remote List remote versions available for install" + echo " nvm deactivate Undo effects of NVM on current shell" + echo " nvm alias [] Show all aliases beginning with " + echo " nvm alias Set an alias named pointing to " + echo " nvm unalias Deletes the alias named " + echo " nvm copy-packages Install global NPM packages contained in to current version" + echo " nvm unload Unload NVM from shell" echo echo "Example:" echo " nvm install v0.10.24 Install a specific version number" @@ -794,20 +794,35 @@ nvm() { fi ;; "use" ) + shift # start treating the args given to "use" + if [ $# -eq 0 ]; then - nvm help - return 127 - fi - if [ $# -eq 1 ]; then nvm_rc_version if [ -n "$NVM_RC_VERSION" ]; then VERSION=`nvm_version $NVM_RC_VERSION` fi - elif [ "_$2" != '_system' ]; then - VERSION="$(nvm_version "$2")" else - VERSION="$2" + # Handle options + while [[ $# > 0 ]]; do + key="$1" + shift + case $key in + --print-paths) + PRINT_PATHS=true + ;; + + *) + if [ "_$key" != '_system' ]; then + VERSION="$(nvm_version "$key")" + else + VERSION="$key" + fi + break + ;; + esac + done fi + if [ -z "$VERSION" ]; then nvm help return 127 @@ -822,7 +837,7 @@ nvm() { return 127 fi elif [ "_$VERSION" = "_∞" ]; then - echo "The alias \"$2\" leads to an infinite loop. Aborting." >&2 + echo "The alias \"$1\" leads to an infinite loop. Aborting." >&2 return 8 fi @@ -858,6 +873,12 @@ nvm() { if [ "$NVM_SYMLINK_CURRENT" = true ]; then rm -f "$NVM_DIR/current" && ln -s "$NVM_VERSION_DIR" "$NVM_DIR/current" fi + if [ $PRINT_PATHS ]; then + echo PATH=$PATH + echo NODE_PATH=$NODE_PATH + echo NVM_PATH=$NVM_PATH + echo NVM_BIN=$NVM_BIN + fi echo "Now using node $VERSION" ;; "run" ) From 18b28e47a17fb6a8cac482238b76ab6387fa2238 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sat, 15 Nov 2014 22:28:30 +0100 Subject: [PATCH 2/7] Introduce nvm executable nvm.sh isn't executable so this file was introduced. It dereferences symlinks for e.g `ln -s ~/.nvm/nvm ~/bin/nvm` will now work. --- nvm | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 nvm diff --git a/nvm b/nvm new file mode 100755 index 0000000000..820985c4d0 --- /dev/null +++ b/nvm @@ -0,0 +1,14 @@ +#!/bin/bash + +# Get this script after dereferincing all symlinks +# !! Doesn't check for circular symlinks !! +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + +source "$DIR/nvm.sh" +'nvm' $@ From 1eac915fbaa4d5ba73441904305cc09d86ba1627 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sat, 15 Nov 2014 22:41:05 +0100 Subject: [PATCH 3/7] Introduce nvm function for fish This is a wrapper around nvm. It's main function is to check and promote environment variables changes from nvm to fish. A little more is done to setup the environment in case that hasn't been done yet: * install a stable node version if it doesn't exist * set the default to stable alias if that hasn't been done yet Due to those actions, the first execution of `nvm` might take some time. --- nvm.fish | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 nvm.fish diff --git a/nvm.fish b/nvm.fish new file mode 100644 index 0000000000..a4072bc2a6 --- /dev/null +++ b/nvm.fish @@ -0,0 +1,24 @@ +function nvm --description "Node version manager" -a nvm_command nvm_command_arg1 + set nvm_dir ~/.nvm + set nvm_ $nvm_dir/nvm + + # This sets some environment vars that we need to set too + if test "$nvm_command" = "use" + eval $nvm_ use --print-paths $nvm_command_arg1 | sed -re "s|^(\w+=)|set -x \1|g" -e "s|[=:]| |g" | grep "set -x" | . + else + # Make sure we can use node + if test ! (which node) + # Have we installed node at all ? + if eval $nvm_ ls | grep "N/A" > /dev/null + echo "No node installation found, installing stable" + eval $nvm_ install stable + end + # Make sure we have a default picked + if eval $nvm_ ls default | grep "N/A" + eval $nvm_ alias default stable + end + nvm use default + end + eval $nvm_ $argv + end +end From 67a70e6dc23a9b3881dbb9cffcf641e52a6acfcc Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sat, 15 Nov 2014 22:43:17 +0100 Subject: [PATCH 4/7] Include actions for fish when installing Fish has to be configured to use nvm. Basically 2 actions have to be taken (like with bash and others): 1. Introduce the nvm function 2. Execute the nvm function once the shell is loaded to have access to nodejs The first session with fish might take a while to start, if nvm doesn't have its default env (stable nodejs + default alias) --- install.sh | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/install.sh b/install.sh index 897459317a..6a00f75ff9 100755 --- a/install.sh +++ b/install.sh @@ -1,5 +1,15 @@ #!/bin/bash +# Get this script after dereferincing all symlinks +# !! Doesn't check for circular symlinks !! +SOURCE="${BASH_SOURCE[0]}" +while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink + DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + SOURCE="$(readlink "$SOURCE")" + [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located +done +DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" + set -e nvm_has() { @@ -135,6 +145,24 @@ nvm_do_install() { fi fi + # Actions for fish shell + if (which fish > /dev/null); then + echo "=> fish found, copying nvm function" + FISH_CONFIG_DIR=$HOME/.config/fish + FISH_FUNCTIONS_DIR=$FISH_CONFIG_DIR/functions + + mkdir -p $FISH_FUNCTIONS_DIR + cp $NVM_DIR/nvm.fish $FISH_FUNCTIONS_DIR + + # Apply nvm in fish configuration + FISH_CONFIG_STRING="nvm > /dev/null ^&1" + FISH_CONFIG_FILE=$FISH_CONFIG_DIR/config.fish + if ! grep -q "$FISH_CONFIG_STRING" $FISH_CONFIG_FILE 2> /dev/null ; then + echo "=> appending nvm to fish configuration" + echo $FISH_CONFIG_STRING >> $FISH_CONFIG_DIR/config.fish + fi + fi + echo "=> Close and reopen your terminal to start using nvm" nvm_reset } From 78069717f76070c6cb2587c1cfcadeeb069751ea Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sun, 16 Nov 2014 15:28:16 +0100 Subject: [PATCH 5/7] Revert misplaced commit in 'Include actions for fish when installing' The DIR variable is used in another branch (vagrant) and doesn't belong here --- install.sh | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/install.sh b/install.sh index 6a00f75ff9..04e85d8705 100755 --- a/install.sh +++ b/install.sh @@ -1,15 +1,5 @@ #!/bin/bash -# Get this script after dereferincing all symlinks -# !! Doesn't check for circular symlinks !! -SOURCE="${BASH_SOURCE[0]}" -while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink - DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - SOURCE="$(readlink "$SOURCE")" - [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located -done -DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - set -e nvm_has() { From c485417bd835c49ac0391448421a73c5d272ab35 Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sun, 16 Nov 2014 17:04:07 +0100 Subject: [PATCH 6/7] Make 'Running "nvm deactivate" should unset the nvm environment variables.' work again /bin/sh doesn't like `while [[ ... ]]`. It much rather prefers `while [ .. ]` Also minor spacing changes --- nvm.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index 5f02501154..02a812a0d1 100644 --- a/nvm.sh +++ b/nvm.sh @@ -803,10 +803,10 @@ nvm() { fi else # Handle options - while [[ $# > 0 ]]; do + while [ $# > 0 ]; do key="$1" shift - case $key in + case $key in --print-paths) PRINT_PATHS=true ;; From 7c1d4b12977fc3ba86943ef331985ff66565e69d Mon Sep 17 00:00:00 2001 From: LoveIsGrief Date: Sun, 16 Nov 2014 17:04:40 +0100 Subject: [PATCH 7/7] Make test 'Running "nvm use foo" where "foo" is circular aborts' work again Slight problem with the output. The test expected an alias to be returned, but an empty string was returned. --- nvm.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index 02a812a0d1..9809e8d2cb 100644 --- a/nvm.sh +++ b/nvm.sh @@ -837,7 +837,7 @@ nvm() { return 127 fi elif [ "_$VERSION" = "_∞" ]; then - echo "The alias \"$1\" leads to an infinite loop. Aborting." >&2 + echo "The alias \"$key\" leads to an infinite loop. Aborting." >&2 return 8 fi