From d5c0e941660e47eb0cf989831d11bb4cbe9068bf Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 1 Aug 2014 01:26:33 -0700 Subject: [PATCH 1/3] Adding `nvm exec` command. --- README.markdown | 6 +++++- nvm.sh | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 4c2995d..e99abfe 100644 --- a/README.markdown +++ b/README.markdown @@ -38,7 +38,7 @@ Often I also put in a line to use a specific version of node. ## Usage You can create an `.nvmrc` file containing version number in the project root directory (or any parent directory). -`nvm use`, `nvm install`, and `nvm run` will all respect an `.nvmrc` file. +`nvm use`, `nvm install`, `nvm exec`, and `nvm run` will all respect an `.nvmrc` file. To download, compile, and install the latest v0.10.x release of node, do this: @@ -52,6 +52,10 @@ Or you can just run it: nvm run 0.10 --version +Or, you can run any arbitrary command in the node environment: + + nvm exec 0.10 node --version + If you want to see what versions are installed: nvm ls diff --git a/nvm.sh b/nvm.sh index 2ef65bd..e9de28c 100644 --- a/nvm.sh +++ b/nvm.sh @@ -639,6 +639,29 @@ nvm() { echo "Running node $VERSION" NODE_PATH=$RUN_NODE_PATH $NVM_DIR/$VERSION/bin/node "$@" ;; + "exec" ) + shift + + local provided_version + provided_version=$1 + if [ -n "$provided_version" ]; then + VERSION=`nvm_version $provided_version` + if [ $VERSION = "N/A" ]; then + provided_version='' + nvm_rc_version + VERSION=`nvm_version $NVM_RC_VERSION` + else + shift + fi + fi + + if [ ! -d "$NVM_DIR/$VERSION" ]; then + echo "$VERSION version is not installed yet" >&2 + return 1 + fi + echo "Running node $VERSION" + NODE_VERSION=$VERSION $NVM_DIR/nvm-exec "$@" + ;; "ls" | "list" ) local NVM_LS_OUTPUT local NVM_LS_EXIT_CODE From 1fa2acf5a7d6ca5b7c657432d9860d81d76da049 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 2 Aug 2014 15:36:45 -0700 Subject: [PATCH 2/3] Adding nvm exec tests. --- .../Running \"nvm exec 0.x\" should work" | 13 +++++++++++++ ... \"nvm exec\" should pick up .nvmrc version" | 17 +++++++++++++++++ test/slow/nvm exec/setup_dir | 10 ++++++++++ test/slow/nvm exec/teardown_dir | 12 ++++++++++++ 4 files changed, 52 insertions(+) create mode 100755 "test/slow/nvm exec/Running \"nvm exec 0.x\" should work" create mode 100755 "test/slow/nvm exec/Running \"nvm exec\" should pick up .nvmrc version" create mode 100755 test/slow/nvm exec/setup_dir create mode 100755 test/slow/nvm exec/teardown_dir diff --git "a/test/slow/nvm exec/Running \"nvm exec 0.x\" should work" "b/test/slow/nvm exec/Running \"nvm exec 0.x\" should work" new file mode 100755 index 0000000..7798d79 --- /dev/null +++ "b/test/slow/nvm exec/Running \"nvm exec 0.x\" should work" @@ -0,0 +1,13 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +nvm use 0.10 +NPM_VERSION_TEN="$(npm --version)" + +nvm use 0.11.7 && [ "$(node --version)" = "v0.11.7" ] || die "\`nvm use\` failed!" + +[ "$(nvm exec 0.10 npm --version | tail -1)" = "$NPM_VERSION_TEN" ] || die "`nvm exec` failed to run with the correct version" + diff --git "a/test/slow/nvm exec/Running \"nvm exec\" should pick up .nvmrc version" "b/test/slow/nvm exec/Running \"nvm exec\" should pick up .nvmrc version" new file mode 100755 index 0000000..203cd9a --- /dev/null +++ "b/test/slow/nvm exec/Running \"nvm exec\" should pick up .nvmrc version" @@ -0,0 +1,17 @@ +#!/bin/sh + +die () { echo $@ ; exit 1; } + +. ../../../nvm.sh + +nvm use 0.10.7 +NPM_VERSION_TEN="$(npm --version)" + +nvm use 0.11.7 && [ "$(node --version)" = "v0.11.7" ] || die "\`nvm use\` failed!" + +echo "0.10.7" > .nvmrc + +[ "$(nvm exec npm --version | tail -1)" = "$NPM_VERSION_TEN" ] || die "\`nvm exec\` failed to run with the .nvmrc version" + +[ "$(nvm exec npm --version | head -1)" = "Found '$PWD/.nvmrc' with version <0.10.7>" ] || die "\`nvm exec\` failed to print out the \"found in .nvmrc\" message" + diff --git a/test/slow/nvm exec/setup_dir b/test/slow/nvm exec/setup_dir new file mode 100755 index 0000000..a5fc4bc --- /dev/null +++ b/test/slow/nvm exec/setup_dir @@ -0,0 +1,10 @@ +#!/bin/sh + +. ../../../nvm.sh +nvm install 0.10.7 +nvm install 0.11.7 + +if [ -f ".nvmrc" ]; then + mv .nvmrc .nvmrc.bak +fi + diff --git a/test/slow/nvm exec/teardown_dir b/test/slow/nvm exec/teardown_dir new file mode 100755 index 0000000..a6b923c --- /dev/null +++ b/test/slow/nvm exec/teardown_dir @@ -0,0 +1,12 @@ +#!/bin/sh + +. ../../../nvm.sh +nvm uninstall v0.10.7 +nvm uninstall v0.11.7 + +rm .nvmrc + +if [ -f ".nvmrc.bak" ]; then + mv .nvmrc.bak .nvmrc +fi + From a703d3591f17247c9ee99cdd43bd63b0b88fc604 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 2 Aug 2014 19:22:53 -0700 Subject: [PATCH 3/3] Tweaking the readme description --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index e99abfe..03805f8 100644 --- a/README.markdown +++ b/README.markdown @@ -52,7 +52,7 @@ Or you can just run it: nvm run 0.10 --version -Or, you can run any arbitrary command in the node environment: +Or, you can run any arbitrary command in a subshell with the desired version of node: nvm exec 0.10 node --version