Skip to content

Commit b4a86a0

Browse files
committed
feat: list and update aliases from nodejs.org
0 parents  commit b4a86a0

File tree

9 files changed

+421
-0
lines changed

9 files changed

+421
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.github/workflows/release.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Release
2+
on:
3+
push:
4+
tags:
5+
- 'v?[0-9]+.[0-9]+.[0-9]+'
6+
7+
jobs:
8+
github:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: jasonkarns/create-release@master

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
/node-build-*.tgz
3+
/share/node-build

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Sean Tu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# node-build-aliases
2+
3+
## Installation
4+
5+
### Installing with Git
6+
7+
```shell
8+
git clone https://github.com/manlao/node-build-aliases.git $(nodenv root)/plugins/node-build-aliases
9+
```
10+
11+
### macOS
12+
13+
## Usage
14+
15+
List all aliases
16+
17+
```shell
18+
nodenv aliases
19+
```
20+
21+
Update aliases from nodejs.org and remove aliases in $NODENV_ROOT/versions
22+
23+
```shell
24+
nodenv aliases --update
25+
```
26+
27+
Update aliases from nodejs.org and modify aliases in $NODENV_ROOT/versions (and auto install version if not installed)
28+
29+
```shell
30+
nodenv aliases --update --upgrade
31+
```

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

bin/nodenv-aliases

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Summary: List or Update Node aliases
4+
#
5+
# Usage: nodenv aliases
6+
# nodenv aliases --update [--upgrade]
7+
# nodenv aliases --version
8+
#
9+
# --update Update aliases from nodejs.org
10+
# --upgrade Upgrade installed aliases
11+
# --version Show version of node-build-aliases
12+
#
13+
# More: https://github.com/manlao/node-build-aliases
14+
15+
set -e
16+
[ -n "$NODENV_DEBUG" ] && set -x
17+
18+
shopt -s nullglob
19+
20+
READLINK=$(type -p greadlink readlink 2>/dev/null | head -1)
21+
22+
if [ -z "$READLINK" ]; then
23+
echo "cannot find readlink - are you missing GNU coreutils?" >&2
24+
exit 1
25+
fi
26+
27+
resolve_link() {
28+
"$READLINK" "$1"
29+
}
30+
31+
realpath() {
32+
local CWD="$PWD"
33+
local REALPATH="$1"
34+
local NAME
35+
36+
while [ -n "$REALPATH" ]; do
37+
NAME="${REALPATH##*/}"
38+
[ "$NAME" = "$REALPATH" ] || cd "${REALPATH%/*}" || exit 1
39+
REALPATH="$(resolve_link "$NAME" || true)"
40+
done
41+
42+
echo "${PWD}/$NAME"
43+
cd "$CWD" || exit 1
44+
}
45+
46+
realdir() {
47+
local REALPATH
48+
REALPATH=$(realpath "$1")
49+
50+
echo "${REALPATH%/*}"
51+
}
52+
53+
resolve_definition() {
54+
local REALPATH
55+
REALPATH=$(resolve_link "$(aliases_path)/$1")
56+
57+
echo "${REALPATH##*/}"
58+
}
59+
60+
resolve_aliases() {
61+
local ALIASES_PATH
62+
ALIASES_PATH=$(aliases_path)
63+
local ALIASES ALIAS
64+
local RESOLVED=()
65+
IFS=" " read -ra ALIASES <<< "$(get_aliases)"
66+
67+
for ALIAS in "${ALIASES[@]}"; do
68+
local REALPATH
69+
REALPATH=$(resolve_link "$ALIASES_PATH/$ALIAS")
70+
local DEFINITION=
71+
72+
while [ -z "$DEFINITION" ]; do
73+
if [ -L "$REALPATH" ]; then
74+
local LINK_ALIAS="${REALPATH/$ALIASES_PATH\//}"
75+
REALPATH=$(resolve_link "$ALIASES_PATH/$LINK_ALIAS")
76+
else
77+
DEFINITION="${REALPATH##*/}"
78+
fi
79+
done
80+
81+
if [ "$DEFINITION" = "$1" ]; then
82+
RESOLVED+=("$ALIAS")
83+
fi
84+
done
85+
86+
echo "${RESOLVED[@]}"
87+
}
88+
89+
get_aliases() {
90+
local PREFIX="${1+$1/}"
91+
local PATTERN
92+
PATTERN="$(aliases_path)/$PREFIX*"
93+
local ALIASES=()
94+
local DIRS=()
95+
local ITEM
96+
97+
for ITEM in $PATTERN; do
98+
if [ -L "$ITEM" ]; then
99+
ALIASES+=("$PREFIX${ITEM##*/}")
100+
elif [ -d "$ITEM" ]; then
101+
DIRS+=("$ITEM")
102+
fi
103+
done
104+
105+
for ITEM in "${DIRS[@]}"; do
106+
local DIR_ALIASES
107+
DIR_ALIASES=$(get_aliases "$PREFIX${ITEM##*/}")
108+
ALIASES+=("${DIR_ALIASES[@]}")
109+
done
110+
111+
echo "${ALIASES[@]}"
112+
}
113+
114+
get_alias_info() {
115+
local ALIAS="$1"
116+
local INFO="$ALIAS"
117+
local ALIASES_PATH
118+
ALIASES_PATH=$(aliases_path)
119+
local REALPATH
120+
REALPATH=$(resolve_link "$ALIASES_PATH/$ALIAS")
121+
local DEFINITION
122+
123+
while [ -z "$DEFINITION" ]; do
124+
if [ -L "$REALPATH" ]; then
125+
ALIAS="${REALPATH/$ALIASES_PATH\//}"
126+
INFO="$INFO -> $ALIAS"
127+
REALPATH=$(resolve_link "$ALIASES_PATH/$ALIAS")
128+
else
129+
DEFINITION="${REALPATH##*/}"
130+
INFO="$INFO -> $DEFINITION"
131+
fi
132+
done
133+
134+
if [ -d "$(nodenv root)/versions/$DEFINITION" ]; then
135+
echo "$INFO (installed)"
136+
else
137+
echo "$INFO"
138+
fi
139+
}
140+
141+
list() {
142+
local ALIASES ALIAS
143+
IFS=" " read -ra ALIASES <<< "$(get_aliases)"
144+
145+
for ALIAS in "${ALIASES[@]}"; do
146+
get_alias_info "$ALIAS"
147+
done
148+
}
149+
150+
update() {
151+
local ALIASES_PATH
152+
ALIASES_PATH=$(aliases_path)
153+
local DEFINITIONS_PATH
154+
DEFINITIONS_PATH=$(definitions_path)
155+
local INDEX_TABLE
156+
INDEX_TABLE=$(curl -fsSL "https://nodejs.org/dist/index.tab")
157+
158+
# https://github.com/nvm-sh/nvm/blob/master/nvm.sh
159+
echo "$INDEX_TABLE" \
160+
| command sed "1d; s/^//;" \
161+
| command awk '{
162+
if ($10 ~ /^\-?$/) { next }
163+
if ($10 && !a[tolower($10)]++) {
164+
if (alias) { print alias, version }
165+
alias_name = "lts/" tolower($10)
166+
if (!alias) { print "lts/*", alias_name }
167+
alias = alias_name
168+
version = $1
169+
}
170+
}
171+
END {
172+
if (alias) {
173+
print alias, version
174+
}
175+
}' \
176+
| while read -r LINE; do
177+
local ALIAS="${LINE%% *}"
178+
local VERSION="${LINE#* }"
179+
180+
mkdir -p "$(dirname "$ALIASES_PATH/$ALIAS")"
181+
182+
if [[ "$VERSION" =~ ^(v|V)([0-9\.]+)$ ]]; then
183+
ln -sf "$DEFINITIONS_PATH/${BASH_REMATCH[2]}" "$ALIASES_PATH/$ALIAS"
184+
else
185+
ln -sf "$ALIASES_PATH/$VERSION" "$ALIASES_PATH/$ALIAS"
186+
fi
187+
done
188+
189+
local MANIFEST
190+
read -ra MANIFEST < <(echo "$INDEX_TABLE" | grep src | sort -rn -t $'\t' -k2,2 -k1,1 | head -1)
191+
local LATEST="${MANIFEST[0]}"
192+
ln -sf "$DEFINITIONS_PATH/${LATEST##*v}" "$ALIASES_PATH/node"
193+
194+
local VERSIONS_PATH
195+
VERSIONS_PATH="$(nodenv root)/versions"
196+
local ALIASES ALIAS
197+
IFS=" " read -ra ALIASES <<< "$(get_aliases)"
198+
199+
for ALIAS in "${ALIASES[@]}"; do
200+
if [ -L "$VERSIONS_PATH/$ALIAS" ]; then
201+
local REALPATH
202+
REALPATH=$(realpath "$VERSIONS_PATH/$ALIAS")
203+
local DEFINITION
204+
DEFINITION=$(resolve_definition "$ALIAS")
205+
206+
if [ "${REALPATH##*/}" != "$DEFINITION" ]; then
207+
rm -f "$VERSIONS_PATH/$ALIAS"
208+
209+
if [ "$1" = "--upgrade" ]; then
210+
nodenv install "$ALIAS"
211+
fi
212+
fi
213+
fi
214+
done
215+
}
216+
217+
version() {
218+
cat "$(realdir "${BASH_SOURCE[0]}")/../VERSION"
219+
}
220+
221+
aliases_path() {
222+
echo "$(realdir "${BASH_SOURCE[0]}")/../share/node-build"
223+
}
224+
225+
definitions_path() {
226+
echo "$(realdir "$(command -v nodenv-install)")/../share/node-build"
227+
}
228+
229+
case "$#" in
230+
0 )
231+
list
232+
;;
233+
234+
1 )
235+
case "$1" in
236+
--update )
237+
update
238+
;;
239+
--version | --aliases_path | --definitions_path )
240+
"${1##*-}"
241+
;;
242+
* )
243+
nodenv-help --usage aliases >&2
244+
;;
245+
esac
246+
;;
247+
248+
2 )
249+
case "$1" in
250+
--update )
251+
if [ "$2" = "--upgrade" ]; then
252+
update "$2"
253+
else
254+
nodenv-help --usage aliases >&2
255+
fi
256+
;;
257+
--resolve_definition | --resolve_aliases )
258+
"${1##*-}" "$2"
259+
;;
260+
* )
261+
nodenv-help --usage aliases >&2
262+
;;
263+
esac
264+
;;
265+
266+
* )
267+
nodenv-help --usage aliases >&2
268+
;;
269+
esac
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env bash
2+
3+
resolve_versions() {
4+
if [ -L "$(nodenv-aliases --aliases_path)/$DEFINITION" ]; then
5+
VERSION_NAME=$(nodenv-aliases --resolve_definition "$DEFINITION")
6+
export VERSION_NAME
7+
export PREFIX="$NODENV_ROOT/versions/$VERSION_NAME"
8+
export DEFINITION="$VERSION_NAME"
9+
10+
if [ -d "$PREFIX" ] && [ -d "${PREFIX}/bin" ]; then
11+
if [ -z "$FORCE" ] && [ -z "$SKIP_EXISTING" ]; then
12+
echo "nodenv: $PREFIX already exists" >&2
13+
read -rp "continue with installation? (y/N) "
14+
15+
case "$REPLY" in
16+
y* | Y* )
17+
;;
18+
* )
19+
exit 1 ;;
20+
esac
21+
elif [ -n "$SKIP_EXISTING" ]; then
22+
exit 0
23+
fi
24+
fi
25+
fi
26+
}
27+
28+
alias_versions() {
29+
if [ "$STATUS" -eq 0 ] && [ -n "$VERSION_NAME" ] && [ -f "$(nodenv-aliases --definitions_path)/$VERSION_NAME" ]; then
30+
local ALIASES=()
31+
IFS=" " read -ra ALIASES <<< "$(nodenv-aliases --resolve_aliases "$VERSION_NAME")"
32+
33+
local ALIAS
34+
35+
for ALIAS in "${ALIASES[@]}"; do
36+
if [ -n "$ALIAS" ]; then
37+
mkdir -p "$(dirname "$NODENV_ROOT/versions/$ALIAS")"
38+
ln -sf "${PREFIX:-$NODENV_ROOT/versions/$VERSION_NAME}" "$NODENV_ROOT/versions/$ALIAS"
39+
echo "Alias: $ALIAS -> $VERSION_NAME"
40+
fi
41+
done
42+
fi
43+
}
44+
45+
before_install resolve_versions
46+
47+
after_install alias_versions

0 commit comments

Comments
 (0)