Skip to content

Instantly share code, notes, and snippets.

@shhac
Last active July 1, 2018 19:59
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 shhac/e1535fc56485130458af976ba6b3dd56 to your computer and use it in GitHub Desktop.
Save shhac/e1535fc56485130458af976ba6b3dd56 to your computer and use it in GitHub Desktop.
CI nvm starter

Easy NVM start for CI and other environments

This script assumes nvm is already installed on your machine, but does not assume that nvm is set up for your current environment

It will attempt to set up nvm for the current environment, then install the desired node version and any needed global packages that are missing

It will keep the existing version of nvm if it finds it already set up in the environment

Usage

Invoke with

  • First arg for required node version
  • Following args for required npm global packages

Example

Say you placed the script in $HOME and need node 8 with gulp and localtunnel, you could run

[ -s "$HOME/nvm-start.sh" ] && . "$HOME/nvm-start.sh" 8 gulp localtunnel
#!/usr/bin/env bash
warn() {
echo "$@" 1>&2
}
setup() {
[ -z "$NVM_DIR" ] \
&& [ -s "$HOME/.nvm" ] \
&& export NVM_DIR="$HOME/.nvm"
[ ! -z "$NVM_DIR" ] \
&& [ ! "$(command -v nvm)" ] \
&& [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ ! -z "$1" ] \
&& [ "$(command -v nvm)" ] \
&& nvm install "$1"
}
test() {
[ ! "$(command -v nvm)" ] && warn "nvm is not set up"
[ ! -x "$(command -v node)" ] && warn "node is not set up"
[ ! -x "$(command -v npm)" ] && warn "npm is not set up"
}
install() {
[ -x "$(command -v npm)" ] && for module in "$@"; do
[ ! -x "$(command -v $module)" ] && npm i -g "$module"
done
}
setup $1
test
install "${@:2}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment