Scrape npm's package.json to determine version more quickly

This commit is contained in:
Burke Libbey 2016-02-29 17:42:14 -05:00
parent 2176894a5b
commit 6731b0b9f1
No known key found for this signature in database
GPG Key ID: E893DEF914F22410
1 changed files with 16 additions and 0 deletions

16
nvm.sh
View File

@ -60,8 +60,24 @@ nvm_has_system_iojs() {
[ "$(nvm deactivate >/dev/null 2>&1 && command -v iojs)" != '' ] [ "$(nvm deactivate >/dev/null 2>&1 && command -v iojs)" != '' ]
} }
# npm --version can report its version, but it typically takes a couple hundred
# ms to start npm. If we can load npm's own package.json file, we can scrape
# the version out of it and avoid that work. This speeds up `nvm use`
# substantially, at the cost of some extra complexity here.
nvm_print_npm_version() { nvm_print_npm_version() {
if nvm_has "npm"; then if nvm_has "npm"; then
local PACKAGE_JSON
PACKAGE_JSON="$(dirname "$(dirname "$(which npm 2>/dev/null)")")/lib/node_modules/npm/package.json"
if [ -f "${package_json}" ]; then
local NPM_VERSION
NPM_VERSION="$(command awk -F '"' '$2 == "version" { print $4; exit }' < "${PACKAGE_JSON}")"
if [ -n "${NPM_VERSION}" ]; then
echo " (npm v${NPM_VERSION})"
return
fi
fi
echo " (npm v$(npm --version 2>/dev/null))" echo " (npm v$(npm --version 2>/dev/null))"
fi fi
} }