Created
November 15, 2021 18:52
-
-
Save juliovedovatto/eda6cb9cf2c6ef0c5ee2333bc483eb1c to your computer and use it in GitHub Desktop.
NVM shell script for node projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
NVM_NODE_VERSION="" | |
NODE_VERSION=$(node --version) | |
IS_WSL=0 | |
SKIP_NVM=0 | |
if grep -q microsoft /proc/version >/dev/null 2>&1; then | |
IS_WSL=1 | |
fi | |
initNVM() { | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion | |
} | |
check_nvm () { | |
if ! command -v nvm &> /dev/null | |
then | |
echo "Error: nvm could not be found" | |
echo "Install instructions: https://github.com/nvm-sh/nvm#installing-and-updating" | |
exit 1 | |
fi | |
} | |
get_nvm_node_version() { | |
if [ ! -f .nvmrc ]; then | |
echo "Error: .nvmrc file was not found" | |
exit 1 | |
fi | |
NVM_NODE_VERSION=$(head -1 .nvmrc | tr -d '[:space:]') | |
} | |
check_nvm_required_node() { | |
if [ -z "${NVM_NODE_VERSION}" ]; then | |
echo "Error: not able to retrive required node version" | |
fi | |
CURRENT_VERSION=$(nvm current) | |
if [ "${CURRENT_VERSION}" != "${NVM_NODE_VERSION}" ]; then | |
if nvm ls | grep -q "$NVM_NODE_VERSION" | |
then | |
nvm use "$NVM_NODE_VERSION" | |
else | |
nvm_ask_install "$NVM_NODE_VERSION" | |
fi | |
fi | |
} | |
nvm_ask_install() { | |
VERSION="$1" | |
echo "Node version ${VERSION} was not found in nvm." | |
read -r -p "Do you want to install it (Y/n)? " answer | |
case ${answer:0:1} in | |
n|N ) | |
echo "No" | |
;; | |
"") | |
nvm_install_version "$VERSION" | |
;; | |
y|Y) | |
nvm_install_version "$VERSION" | |
;; | |
* ) | |
nvm_ask_install "$VERSION" | |
;; | |
esac | |
} | |
nvm_install_version() { | |
VERSION="$1" | |
if [ -z "$VERSION" ]; then | |
echo "Error: Node version not informed." | |
exit 1 | |
fi | |
if nvm install "$VERSION" | |
then | |
check_nvm_required_node | |
else | |
echo "An error occurred while installing Node. Aborting." | |
exit 1 | |
fi | |
check_nvm_required_node | |
} | |
check_node_version() { | |
REQUIRED_MAJOR_VERSION=${NVM_NODE_VERSION%.*.*} | |
NODE_MAJOR_VERSION=${NODE_VERSION%.*.*} | |
if [ "${REQUIRED_MAJOR_VERSION}" = "${NODE_MAJOR_VERSION}" ]; then | |
SKIP_NVM=1 | |
fi | |
} | |
# ------------------------------------------------------------------------------------------------- | |
for arg in "$@" | |
do | |
case $arg in | |
--skip-nvm) | |
SKIP_NVM=1 | |
shift | |
;; | |
esac | |
done | |
if [ $SKIP_NVM = 0 ]; then | |
get_nvm_node_version | |
check_node_version | |
fi | |
if [ $IS_WSL = 1 ]; then | |
echo -e "Warning: You are using Windows WSL. This may be problematic if not correctly configured\n\n" | |
fi | |
if [ $SKIP_NVM = 0 ]; then | |
initNVM | |
check_nvm | |
check_nvm_required_node | |
else | |
echo -e "Skipping NVM configuraiton...\n\n" | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# For NPM Vue projects | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
. "${DIR}"/nvm.sh | |
SCRIPT="${1}" | |
ARGS=${*#"$1"} | |
case ${SCRIPT} in | |
serve ) | |
npx vue-cli-service serve $ARGS | |
;; | |
build ) | |
npx vue-cli-service build $ARGS | |
;; | |
lint ) | |
npx vue-cli-service lint $ARGS | |
;; | |
setup ) | |
npm install --legacy-peer-deps | |
;; | |
install ) | |
npm install $ARGS | |
;; | |
"") | |
echo "Available scripts: setup, serve, build, lint" | |
;; | |
* ) | |
echo "Error: invalid script \"${SCRIPT}\". Aborting..." | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment