Repository navigation
Expand file tree
/
Copy pathsetup-util-node
More file actions
executable file
Β·190 lines (166 loc) Β· 5.85 KB
/
Copy pathsetup-util-node
File metadata and controls
executable file
Β·190 lines (166 loc) Β· 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env bash
function setup_util_node() (
source "$DOROTHY/sources/bash.bash"
source "$(type -P eval-helper)" # enable `install_nvm_node`
# improve performance for detectable utilities with conditional assets
if setup-util --check --cli=node "$@"; then
return 0
fi
# enable EVAL_INSTALL, etc
source "$(type -P setup-util)"
# =====================================
# Helpers
function __get_nvm_versions {
nvm-env -- nvm ls --no-colors --no-alias | echo-regexp -ong --regexp='v([^\s]+)' --replace='$1' || return $?
}
function __clean_nvm {
# swap to system
nvm-env -- nvm use system || :
# fetch versions
local versions=()
__split --target={versions} --no-zero-length --invoke -- \
__get_nvm_versions || return $?
if [[ ${#versions[@]} -eq 0 ]]; then
# already clean
return 0
fi
# clean versions
local version
for version in "${versions[@]}"; do
nvm-env -- nvm uninstall "$version" | echo-regexp -n 'Uninstalled node ' 'Uninstalled: ' || return $?
done
# confirm clean was successful
__split --target={versions} --no-zero-length --invoke -- \
__get_nvm_versions || return $?
[[ ${#versions[@]} -eq 0 ]] || return $?
}
function clean_otherwise_wipe_nvm {
# clean only what is necessary
__print_style --h3='Clean Node.js versions and packages'
if __clean_nvm; then
__print_style --g3='Clean Node.js versions and packages'
else
# nvm buggered, clean it all
__print_style --e3='Clean Node.js versions and packages' --newline \
--h3='Clean Node.js Version Manager (NVM) instead'
setup-util-nvm --uninstall
__print_style --g3='Clean Node.js Version Manager (NVM) instead'
fi
}
function install_nvm_node {
# install
nvm-env -- nvm install --no-progress node # latest active
nvm-env -- nvm install --no-progress --lts # latest LTS
# set default node version
if __is_brew; then
nvm-env -- nvm alias default system
else
# use latest lts
nvm-env -- nvm alias default stable
# use current lts: "$(nvm version-remote --lts)"
fi
}
function setup_system_node {
# https://deb.nodesource.com
# `libatomic1` as otherwise `node: error while loading shared libraries: libatomic.so.1: cannot open shared object file: No such file or directory` when using to fetch version numbers during npm upgrade
setup-util --name='Node.js via System' --cli=node "$@" \
APK='nodejs' APK='npm' \
APT_KEY='https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key' \
APT_REPO='deb [arch={ARCH} signed-by={KEY}] https://deb.nodesource.com/node_22.x nodistro main' \
APT='nodejs' APT='libatomic1' \
AUR='nodejs' AUR='npm' \
BREW='node' \
BSD='nodejs' \
EMERGE='nodejs' \
EOPKG='nodejs' \
RPM='nodejs' \
SCOOP='nodejs' \
SNAP='node --classic' \
WINGET='OpenJS.NodeJS' \
XBPS='nodejs' \
ZYPPER='nodejs'
}
function upgrade_system_or_nvm_npm {
# ensure the cache is configured correctly
# to avoid when say a /Users/... cache config is copied to a Linux machine
local cache_dir
cache_dir="$(nvm-env -- npm config get cache || :)"
if [[ -n $cache_dir && ! -d $cache_dir ]]; then
# try and make it
__mkdirp "$cache_dir" || {
__print_style --notice='Unconfiguring the invalid cache directory:' ' ' --code="$cache_dir" || return $?
nvm-env -- npm config delete cache || return $?
} || return $?
fi
# continue with the upgrade
__print_style --dim="Before: v$(nvm-env -- npm --version)"
setup-util --name='npm' --upgrade --force --quiet \
NPM='npm' || __print_style --notice='You can ignore the npm upgrade failure, this will resolve after nvm is used.'
nvm-env -- npm cache clean --force 2>/dev/null # silent --force warning
__print_lines "After: v$(nvm-env -- npm --version)"
}
# =====================================
# Actions
function node_install {
# =====================================
# Install Node via System
# https://nodejs.org/en/download/package-manager/
# @todo https://github.com/nodejs/unofficial-builds for riscv, but provides tars that include multiple directories
__print_style --h2='Install Node.js via System'
setup_system_node --install --force --quiet
__print_lines "Installed: $(node --version)"
__print_style --g2='Install Node.js via System'
# =====================================
# Clean NVM
# clean NVM unless configured otherwise
# the intent of this is to purge unused global Node.js versions and packages
# we use a configuration variable to change this, as interrupting the flow and asking the user creates too much confusion
# as such a question is only necessary to experienced users
if [[ -d ${NVM_DIR-} ]]; then
clean_otherwise_wipe_nvm
fi
# =====================================
# Install NVM and Node via NVM
# (arm) alpine does not support nvm, fails with:
# > mode no such file or directory
if ! is-system --alpine; then
# install nvm
__print_style --h2='Install Node.js Version Manager (NVM)'
setup-util-nvm --install --upgrade
__print_style --g2='Install Node.js Version Manager (NVM)'
# nvm node
__print_style --h2='Install Node.js via NVM'
__print_style --dim='Installs active and LTS versions.'
eval_helper --quiet --no-wrap -- \
install_nvm_node
local versions=()
__split --target={versions} --no-zero-length --invoke -- \
__get_nvm_versions
for version in "${versions[@]}"; do
__print_lines "Installed: v$version"
done
__print_style --g2='Install Node.js via NVM'
fi
# =====================================
# Upgrade npm
__print_style --h3='Upgrade npm'
upgrade_system_or_nvm_npm
__print_style --g3='Upgrade npm'
}
function node_uninstall {
setup_system_node --uninstall
__wipe_nvm
}
# setup
local options=(
--cli='node'
"$@"
EVAL_INSTALL='node_install'
EVAL_UNINSTALL='node_uninstall'
)
setup_util "${options[@]}"
)
# fire if invoked standalone
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
setup_util_node "$@"
fi