Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --only-remove flag #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 75 additions & 62 deletions node-reinstall
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ FORCE=0
## default node version
NODE_VERSION="5"

## Only remove
ONLY_REMOVE=0

usage () {
printf "%s\n" "node-reinstall"
printf "\t%s\n" "completely re-installs Node & NPM and any global node modules."
Expand All @@ -34,6 +37,7 @@ usage () {
printf "\t%s %s\t\t%s\n" "node-reinstall" "--nave" "re-install using nave"
printf "\t%s %s\t\t%s\n" "node-reinstall" "--nvm" "re-install using stable nvm - the default"
printf "\t%s %s\t%s\n" "node-reinstall" "--nvm-latest" "re-install using latest nvm - creationix/nvm:master"
printf "\t%s %s\t%s\n" "node-reinstall" "--only-remove" "simply remove everything without re-installing"
printf "\t%s %s\t\t%s\n" "node-reinstall" "0.12" "specify a default node version - currently ${NODE_VERSION}"
printf "\n"
}
Expand Down Expand Up @@ -73,6 +77,10 @@ usage () {
STABLE=master
;;

--only-remove)
ONLY_REMOVE=1
;;

*)
if [ "-" == "${opt:0:1}" ]; then
echo >&2 "error: Unknown option \`$opt'"
Expand Down Expand Up @@ -114,38 +122,42 @@ if [[ ! -z $INSTALLED_NODE_VERSION ]]; then
confirm
fi

# check to see if npm is installed
IS_NPM_MISSING=$(which npm)
if [[ -z $IS_NPM_MISSING ]]; then
echo "Installing Node, npm."
else
echo "Completely reinstalling Node, npm."
# get list of global npm modules to reinstall
# omit the lib directory listing
GLOBAL_MODULES=`npm -g list --depth 0 --parseable | xargs basename | sed -E 's/^(lib|npm)$//g'`
if [[ -n $GLOBAL_MODULES ]]; then
echo "Will reinstall these global npm modules:"
echo $GLOBAL_MODULES
if [[ $ONLY_REMOVE == 0 ]]; then
# check to see if npm is installed
IS_NPM_MISSING=$(which npm)
if [[ -z $IS_NPM_MISSING ]]; then
echo "Installing Node, npm."
else
echo "===== ALERT! ====="
echo "The script did not find any global node modules (npm -g list)"
echo "If you are sure you installed global node modules"
echo "(by running npm install -g some-module), you might want to stop "
echo "here and locate those, because they won't be re-installed,"
echo "and since we'll be deleting all the possible install paths "
echo "that most people could use, you probably won't find them."
echo ""
echo "This can sometimes happen if you've installed global node modules"
echo "under a different node environment (for example, using nvm or nave)."
echo "It might help to run: "
echo "history | grep 'npm install' and look for either -g or --global"
echo ""
echo "If you aren't really sure, or you are sure and don't care, "
echo "you can continue; we'll re-install things the proper way, and"
echo "the next time you run this script you'll see a list of "
echo "any global node modules you've installed since now."
echo "Completely reinstalling Node, npm."
# get list of global npm modules to reinstall
# omit the lib directory listing
GLOBAL_MODULES=`npm -g list --depth 0 --parseable | xargs basename | sed -E 's/^(lib|npm)$//g'`
if [[ -n $GLOBAL_MODULES ]]; then
echo "Will reinstall these global npm modules:"
echo $GLOBAL_MODULES
else
echo "===== ALERT! ====="
echo "The script did not find any global node modules (npm -g list)"
echo "If you are sure you installed global node modules"
echo "(by running npm install -g some-module), you might want to stop "
echo "here and locate those, because they won't be re-installed,"
echo "and since we'll be deleting all the possible install paths "
echo "that most people could use, you probably won't find them."
echo ""
echo "This can sometimes happen if you've installed global node modules"
echo "under a different node environment (for example, using nvm or nave)."
echo "It might help to run: "
echo "history | grep 'npm install' and look for either -g or --global"
echo ""
echo "If you aren't really sure, or you are sure and don't care, "
echo "you can continue; we'll re-install things the proper way, and"
echo "the next time you run this script you'll see a list of "
echo "any global node modules you've installed since now."
fi
confirm
fi
confirm
else
echo "Removing Node, npm."
fi

if (( $USE_NVM )); then
Expand Down Expand Up @@ -182,42 +194,43 @@ sudo rm -rf $PREFIX/bin/{node,npm}
sudo rm -rf $PREFIX/share/man/man1/node.1
sudo rm -rf $PREFIX/lib/dtrace/node.d

if (( $USE_NVM )); then
# go home and install NVM just because I feel safe there
cd $HOME
# get the latest stable version number of nvm from the repo's homepage
[ "$STABLE" == "" ] && STABLE=$(curl -s -k https://github.com/creationix/nvm/ | grep "curl https://raw.githubusercontent.com/creationix/nvm/" | grep -oE "v\d+\.\d+\.\d+")
[[ $STABLE =~ ^v[0-9]+.[0-9]+.[0-9]+$ ]] || STABLE="v0.25.1"
curl -sL https://raw.githubusercontent.com/creationix/nvm/$STABLE/install.sh | bash
source $HOME/.nvm/nvm.sh
elif (( $USE_NAVE )); then
curl -sL https://raw.githubusercontent.com/isaacs/nave/master/nave.sh -o $PREFIX/bin/nave
fi
if [[ $ONLY_REMOVE == 0 ]]; then
if (( $USE_NVM )); then
# go home and install NVM just because I feel safe there
cd $HOME
# get the latest stable version number of nvm from the repo's homepage
[ "$STABLE" == "" ] && STABLE=$(curl -s -k https://github.com/creationix/nvm/ | grep "curl https://raw.githubusercontent.com/creationix/nvm/" | grep -oE "v\d+\.\d+\.\d+")
[[ $STABLE =~ ^v[0-9]+.[0-9]+.[0-9]+$ ]] || STABLE="v0.25.1"
curl -sL https://raw.githubusercontent.com/creationix/nvm/$STABLE/install.sh | bash
source $HOME/.nvm/nvm.sh
elif (( $USE_NAVE )); then
curl -sL https://raw.githubusercontent.com/isaacs/nave/master/nave.sh -o $PREFIX/bin/nave
fi

if (( $USE_NVM )); then
# install the specified version of node then set it as the default
nvm install $NODE_VERSION
nvm alias default $NODE_VERSION
elif (( $USE_NAVE )); then
nave usemain $NODE_VERSION
fi

if (( $USE_NVM )); then
# install the specified version of node then set it as the default
nvm install $NODE_VERSION
nvm alias default $NODE_VERSION
elif (( $USE_NAVE )); then
nave usemain $NODE_VERSION
fi
if [[ ! -z ${GLOBAL_MODULES// } ]]; then
echo "Reinstalling your global npm modules:"
echo $GLOBAL_MODULES
if [[ $FORCE == 0 ]]; then
echo "Continue?"
select yn in "Yes" "No"; do
case $yn in
Yes ) npm install --global $GLOBAL_MODULES; break;;
No ) exit;;
esac
done
else
npm install --global $GLOBAL_MODULES
fi

if [[ ! -z ${GLOBAL_MODULES// } ]]; then
echo "Reinstalling your global npm modules:"
echo $GLOBAL_MODULES
if [[ $FORCE == 0 ]]; then
echo "Continue?"
select yn in "Yes" "No"; do
case $yn in
Yes ) npm install --global $GLOBAL_MODULES; break;;
No ) exit;;
esac
done
else
npm install --global $GLOBAL_MODULES
fi

fi

if [[ $OSTYPE =~ "darwin" ]]; then
Expand Down