From b496b82ef360b1c3c00866900965c4561f8a63f0 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Fri, 16 Aug 2024 00:13:52 -0400 Subject: [PATCH] If `jq` is installed, use it to get values from package.json (#810) `jq` is a popular choice for being installed, especially on CI machines, and using it to read engines from package.json is faster than running node. --- bin/n | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/bin/n b/bin/n index e4aae8d..94de453 100755 --- a/bin/n +++ b/bin/n @@ -1084,9 +1084,14 @@ function get_package_engine_version() { g_target_node= local filepath="$1" verbose_log "found" "${filepath}" - command -v node &> /dev/null || abort "an active version of node is required to read 'engines' from package.json" local range - range="$(node -e "package = require('${filepath}'); if (package && package.engines && package.engines.node) console.log(package.engines.node)")" + if command -v jq &> /dev/null; then + range="$(jq -r '.engines.node // ""' < "${filepath}")" + elif command -v node &> /dev/null; then + range="$(node -e "package = require('${filepath}'); if (package && package.engines && package.engines.node) console.log(package.engines.node)")" + else + abort "either jq or an active version of node is required to read 'engines' from package.json" + fi verbose_log "read" "${range}" [[ -n "${range}" ]] || return 2 if [[ "*" == "${range}" ]]; then @@ -1474,6 +1479,13 @@ function show_diagnostics() { echo_red "Neither curl nor wget found. Need one of them for downloads." fi + printf "\njq\n" + if command -v jq &> /dev/null; then + command -v jq && jq --version + else + echo "jq not found, can be used to get package.json values faster than node" + fi + printf "\nuname\n" uname -a