Skip to content

Commit

Permalink
seutp-node: more robust nvm handling
Browse files Browse the repository at this point in the history
setup-util: don't load nvm, so npm inherits caller environment
  • Loading branch information
balupton committed Aug 23, 2023
1 parent 8cfcb39 commit fbb3dd7
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 58 deletions.
119 changes: 66 additions & 53 deletions commands/setup-node
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,20 @@ function setup_node() (
set +Eeu # disable strict mode inside this subshell
source "$DOROTHY/sources/ripgrep.bash"
source "$DOROTHY/sources/nvm.sh"
if ! command -v nvm &>/dev/null; then
return 1
fi
nvm ls --no-colors --no-alias | rg -o 'v([^\s]+)' --replace '$1' || :
)

function clean_nvm() ( # 👍 /docs/bash/errors: disabled errexit
set +Eeu # disable strict mode inside this subshell
local versions version
source "$DOROTHY/sources/nvm.sh"
if ! command -v nvm &>/dev/null; then
return 1
fi
# prepare
local versions version
# swap to system
nvm use system || :
# fetch versions
Expand All @@ -118,7 +125,7 @@ function setup_node() (
)

function wipe_nvm {
rm -Rf "$NVM_DIR"
fs-rm --no-confirm --quiet -- "$NVM_DIR"
}

function clean_otherwise_wipe_nvm {
Expand Down Expand Up @@ -152,27 +159,48 @@ function setup_node() (
git checkout "$(git describe --abbrev=0 --tags --match "v[0-9]*" "$(git rev-list --tags --max-count=1)")"
}

function install_nvm_node {
function install_nvm_node() ( # 👍 /docs/bash/errors: disabled errexit
set +Eeu # disable strict mode inside this subshell
source "$DOROTHY/sources/nvm.sh"
if ! command -v nvm &>/dev/null; then
return 1
fi

# install
nvm install --no-progress node # latest active
nvm-2596 install --no-progress --lts # latest LTS
nvm install --no-progress node || return # latest active
nvm-2596 install --no-progress --lts || return # latest LTS

# set default node version
if is-brew; then
nvm alias default system
nvm alias default system || return
else
# use latest lts
nvm alias default stable
nvm alias default stable || return
# use current lts: "$(nvm version-remote --lts)"
fi
)

# default
nvm use default
function install_system_node {
setup-util --name='Node.js via System' --quiet \
APK='nodejs' APK='npm' \
APT_KEY='https://deb.nodesource.com/gpgkey/nodesource.gpg.key' \
APT_REPO='deb [arch={ARCH} signed-by={KEY}] https://deb.nodesource.com/node_18.x {RELEASE} main' \
APT='nodejs' \
AUR='nodejs' AUR='npm' \
BREW='node' \
BSD='nodejs' \
CHOCO='nodejs.install' \
EMERGE='nodejs' \
EOPKG='nodejs' \
RPM='nodejs' \
SCOOP='nodejs' \
SNAP='node --classic' \
WINGET='OpenJS.NodeJS' \
XBPS='nodejs' \
ZYPPER='nodejs'
}

function upgrade_npm {
function upgrade_system_npm {
# ensure the cache is configured correctly
# to avoid when say a /Users/... cache config is copied to a Linux machine
local cache_dir
Expand All @@ -195,6 +223,31 @@ function setup_node() (
echo-segment --g3='Upgrade npm'
}

function configure_system_npm {
echo-segment --h2='Configure npm'
npm config set init-author-name "$(
ask --required --confirm \
--question="What is the profile name that you want to configure npm with?" \
--default="$(get-profile name -- npm ... || :)"
)"
npm config set init-author-email "$(
ask --required --confirm \
--question="What is the profile email that you want to configure npm with?" \
--default="$(get-profile email -- npm ... || :)"
)"
npm config set init-author-url "$(
ask --required --confirm \
--question="What is the profile homepage that you want to configure npm with?" \
--default="$(get-profile url -- npm ... || :)"
)"
npm config set init-license "$(
ask --required --confirm \
--question="What license do you want to configure npm to default to?" \
--default="$(npm config get init-license)"
)"
echo-segment --g2='Configure npm'
}

# =====================================
# Start

Expand All @@ -206,24 +259,8 @@ function setup_node() (
# https://nodejs.org/en/download/package-manager/
# @todo https://github.com/nodejs/unofficial-builds for riscv, but provides tars that include multiple directories
echo-segment --h2='Install Node.js via System'
setup-util --name='Node.js via System' --quiet \
APK='nodejs' APK='npm' \
APT_KEY='https://deb.nodesource.com/gpgkey/nodesource.gpg.key' \
APT_REPO='deb [arch={ARCH} signed-by={KEY}] https://deb.nodesource.com/node_18.x {RELEASE} main' \
APT='nodejs' \
AUR='nodejs' AUR='npm' \
BREW='node' \
BSD='nodejs' \
CHOCO='nodejs.install' \
EMERGE='nodejs' \
EOPKG='nodejs' \
RPM='nodejs' \
SCOOP='nodejs' \
SNAP='node --classic' \
WINGET='OpenJS.NodeJS' \
XBPS='nodejs' \
ZYPPER='nodejs'
upgrade_npm
install_system_node
upgrade_system_npm
print_line "Installed: $(node --version)"
echo-segment --g2='Install Node.js via System'

Expand Down Expand Up @@ -261,9 +298,6 @@ function setup_node() (
print_line "Installed: v$version"
done
echo-segment --g2='Install Node.js via NVM'

# source nvm
source "$DOROTHY/sources/nvm.sh"
fi

# -------------------------------------
Expand All @@ -281,28 +315,7 @@ function setup_node() (

# reconfigure
if test "$reconfigure" = 'yes'; then
echo-segment --h2='Configure npm'
npm config set init-author-name "$(
ask --required --confirm \
--question="What is the profile name that you want to configure npm with?" \
--default="$(get-profile name -- npm ... || :)"
)"
npm config set init-author-email "$(
ask --required --confirm \
--question="What is the profile email that you want to configure npm with?" \
--default="$(get-profile email -- npm ... || :)"
)"
npm config set init-author-url "$(
ask --required --confirm \
--question="What is the profile homepage that you want to configure npm with?" \
--default="$(get-profile url -- npm ... || :)"
)"
npm config set init-license "$(
ask --required --confirm \
--question="What license do you want to configure npm to default to?" \
--default="$(npm config get init-license)"
)"
echo-segment --g2='Configure npm'
configure_system_npm
fi

# -------------------------------------
Expand Down
6 changes: 1 addition & 5 deletions commands/setup-util
Original file line number Diff line number Diff line change
Expand Up @@ -2010,11 +2010,7 @@ function setup_util() (
fi

# packages
(
set +Eeu # disable strict mode inside this subshell
source "$DOROTHY/sources/nvm.sh"
"${args[@]}" "${packages[@]}"
)
"${args[@]}" "${packages[@]}"
}
function do_npm_fallback {
setup-node
Expand Down

0 comments on commit fbb3dd7

Please sign in to comment.