From 6731b0b9f1bb1deee931d6b27ec44d5e4112a59e Mon Sep 17 00:00:00 2001 From: Burke Libbey Date: Mon, 29 Feb 2016 17:42:14 -0500 Subject: [PATCH] Scrape npm's package.json to determine version more quickly --- nvm.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/nvm.sh b/nvm.sh index b4e871f..86da9d4 100755 --- a/nvm.sh +++ b/nvm.sh @@ -60,8 +60,24 @@ nvm_has_system_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() { 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))" fi }