From 87583c2f96f437f0de194a2c44caba8ae88efeee Mon Sep 17 00:00:00 2001 From: Filip Dimovski Date: Wed, 26 Apr 2017 12:19:20 +0000 Subject: [PATCH 1/3] Install a version before use Now 'nvm use' checks whether the version is installed before it is used. If the version is missing, it will call 'nvm install' and install it. Otherwise, if the version is present, it will use it and continue as usual. --- nvm.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/nvm.sh b/nvm.sh index 82a4105..5ad022f 100644 --- a/nvm.sh +++ b/nvm.sh @@ -2682,6 +2682,12 @@ nvm() { return 127 fi + # Install version if not available + if ! nvm ls "$PROVIDED_VERSION" &>/dev/null; then + nvm install "$PROVIDED_VERSION" + return + fi + if [ "_$VERSION" = '_system' ]; then if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then if [ $NVM_USE_SILENT -ne 1 ]; then From 8d0e5e7440709898c82da29887a70ad8d8a17b06 Mon Sep 17 00:00:00 2001 From: Filip Dimovski Date: Wed, 26 Apr 2017 15:03:20 +0000 Subject: [PATCH 2/3] Install Bower and Grunt-CLI when version missing, and after it's installed. --- nvm.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/nvm.sh b/nvm.sh index 5ad022f..6d90a5b 100644 --- a/nvm.sh +++ b/nvm.sh @@ -2685,6 +2685,7 @@ nvm() { # Install version if not available if ! nvm ls "$PROVIDED_VERSION" &>/dev/null; then nvm install "$PROVIDED_VERSION" + npm install -g grunt-cli bower return fi From 4c21abea526a0a5900c4811e50fa4973daeb6bdd Mon Sep 17 00:00:00 2001 From: Filip Dimovski Date: Fri, 19 May 2017 09:10:14 +0000 Subject: [PATCH 3/3] Add retries, to make it fail-safe against network interruptions --- nvm.sh | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/nvm.sh b/nvm.sh index 6d90a5b..12582da 100644 --- a/nvm.sh +++ b/nvm.sh @@ -2684,9 +2684,37 @@ nvm() { # Install version if not available if ! nvm ls "$PROVIDED_VERSION" &>/dev/null; then - nvm install "$PROVIDED_VERSION" - npm install -g grunt-cli bower - return + tries_global=3 + installed_flag="/tmp/.nvm_success" + + while [ $tries_global -gt 0 ]; do + ( nvm install "$PROVIDED_VERSION" && touch $installed_flag ) & + proc_id="$!" + #wait $proc_id + #result=$? + + # Check every 2 seconds if the installation finished, + # if it did, install the needed npm packages and exit + tries_install=10 + while [ $tries_install -gt 0 ]; do + if [ -f $installed_flag ]; then + npm install -g grunt-cli bower + + rm $installed_flag &>/dev/null + return + fi + sleep 2 + tries_install=$(( $tries_install - 1 )) + done + + # Kill process after 20 seconds + kill $proc_id &>/dev/null + + tries_global=$(( $tries_global - 1 )) + done + + echo "Cannot install and use $PROVIDED_VERSION - network/disk space issues might be the reason." + return 1 fi if [ "_$VERSION" = '_system' ]; then