0
0
mirror of https://github.com/tj/n.git synced 2024-11-21 18:48:57 +01:00

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.
This commit is contained in:
Eli Barzilay 2024-08-16 00:13:52 -04:00 committed by GitHub
parent 7872fd0ad6
commit b496b82ef3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

16
bin/n
View File

@ -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