Skip to content

Instantly share code, notes, and snippets.

@RBrNx
Last active August 3, 2020 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RBrNx/3ac758656c21dd09feb897cfa9bfddb5 to your computer and use it in GitHub Desktop.
Save RBrNx/3ac758656c21dd09feb897cfa9bfddb5 to your computer and use it in GitHub Desktop.
Helper function to delete node_modules and re-install them
cleanup(){
if [[ -f "package.json" ]]
then
echo "Deleting node_modules..." && (rm -rf node_modules || echo "No node_modules to delete")
if [[ -n "$1" ]]
then
if [[ $1 = "npm" ]]; then (rm package-lock.json || rm yarn.lock || echo "No lock files to delete") && echo "Installing packages via npm..." && npm i
elif [[ $1 = "yarn" ]]; then (rm yarn.lock || rm package-lock.json || echo "No lock files to delete") && echo "Installing packages via yarn... " && yarn
else echo "Package manager '$1' not recognised. Aborting..."
fi
elif [[ -f "package-lock.json" ]]
then
if [[ $+commands[npm] ]]; then echo "Deleting package-lock.json...." && rm package-lock.json && echo "Installing packages via npm..." && npm i
else "This directory has a package-lock.json but npm is not available on this system. Aborting..."
fi
elif [[ -f "yarn.lock" ]]
then
if [[ $+commands[yarn] ]]; then echo "Deleting yarn.lock..." && rm yarn.lock && echo "Installing packages via yarn" && yarn
else "This directory has a yarn.lock but yarn is not available on this system. Aborting..."
fi
else
echo "No lock files found."
if [[ $+commands[yarn] ]]; then echo "Installing packages via yarn..." && yarn
elif [[ $+commands[npm] ]]; then echo "Installing packages via npm..." && npm i
else echo "Neither yarn or npm are installed on this system."
fi
fi
else echo "Are you sure this a node directory? package.json is missing."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment