Skip to content

Instantly share code, notes, and snippets.

@mfdj
Last active May 26, 2016 20:31
Show Gist options
  • Save mfdj/3370404ccb6a4a44701e62d6ea0d715d to your computer and use it in GitHub Desktop.
Save mfdj/3370404ccb6a4a44701e62d6ea0d715d to your computer and use it in GitHub Desktop.
Extends n (https://github.com/tj/n) to pick node version from .node-version or package.json
n() {
# forward straght to n
[[ $# > 0 ]] && {
command n "$@"
return
}
# look for a version
[[ -f .node-version ]] && {
command n $(cat .node-version) && node --version
return
}
[[ -f package.json ]] && {
if command -v jq > /dev/null; then
command n $(jq -r .engines.node package.json | awk '{print $2}') && node --version
return
else
echo "could not parse package.json, missing jq — please 'brew install jq'"
fi
}
# use interactive picker
command n
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment