Skip to content

Commit

Permalink
Add "--node" option to install NodeJS and NPM (#7)
Browse files Browse the repository at this point in the history
- Less verbose when extract archive
- Fix ownership after extract archive
  • Loading branch information
aifrak authored May 18, 2021
1 parent a37718a commit 2fc2757
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"ohmyzsh",
"pytest",
"readlink",
"shfmt"
"shfmt",
"systemtap",
"tapset"
],
"cSpell.ignoreWords": [
"Fira",
Expand All @@ -51,6 +53,7 @@
"gitstatus",
"gitstatusd",
"globalias",
"jxfo",
"lsdeluxe",
"ludeeus",
"markdownlint",
Expand All @@ -61,6 +64,7 @@
"tagger",
"tvdias",
"zcompdump",
"zxfo",
"zxvf"
],

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ COPY --chown=$USER:$GROUP . .

RUN \
export DEBIAN_FRONTEND=noninteractive \
&& ./install $USER --zsh --dependencies --fonts --dockerfile --shellscript --elixir
&& ./install $USER \
--zsh --dependencies --node --fonts --dockerfile --shellscript --elixir
38 changes: 34 additions & 4 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ OPT_DEPENDENCIES=0
OPT_DOCKERFILE=0
OPT_ELIXIR=0
OPT_FONTS=0
OPT_ZSH=1
OPT_NODE=0
OPT_SHELLSCRIPT=0
OPT_ZSH=1

for opt in "$@"; do
case $opt in
Expand All @@ -142,6 +143,7 @@ for opt in "$@"; do
--dockerfile) OPT_DOCKERFILE=1 ;;
--elixir) OPT_ELIXIR=1 ;;
--fonts) OPT_FONTS=1 ;;
--node) OPT_NODE=1 ;;
--shellscript) OPT_SHELLSCRIPT=1 ;;

# zsh
Expand Down Expand Up @@ -262,7 +264,7 @@ install_gitstatus() {
validate_checksum "$CHECKSUM" "$FILE"

mkdir -p "$DIR"
tar zxvf "$FILE" --directory "$DIR"
tar zxfo "$FILE" --directory "$DIR"

success "✓ Gitstatus installed"
}
Expand Down Expand Up @@ -355,7 +357,7 @@ enable_oh_my_zsh_plugin() {
download_binary() {
NAME=$1

if [ -f "$BIN/$NAME" ]; then
if which "$NAME" >/dev/null; then
warning "$NAME already installed: skipped"
return 0
fi
Expand Down Expand Up @@ -388,7 +390,7 @@ install_shellcheck() {
wget -nv -O "$TMP/$ARCHIVE" $URL
validate_checksum "$CHECKSUM" "$TMP/$ARCHIVE"

tar xf "$TMP/$ARCHIVE" -C "$BIN" $NAME-$VERSION/$NAME --strip-components 1
tar xfo "$TMP/$ARCHIVE" -C "$BIN" $NAME-$VERSION/$NAME --strip-components=1

chmod +x "$BIN/$NAME"

Expand Down Expand Up @@ -455,10 +457,38 @@ install_fonts() {
success "✓ Fira Code from Nerd fonts installed"
}

# Inspired from https://github.com/nodejs/docker-node
install_node() {
if which node >/dev/null; then
warning "Node already installed: skipped"
return 0
fi

DIR="/usr/local"
FILE=$TMP/node.xz
ARCHIVE_ROOT_DIR="node-v14.16.0-linux-x64"
VERSION="14.16.0"
CHECKSUM="2e079cf638766fedd720d30ec8ffef5d6ceada4e8b441fc2a093cb9a865f4087"

wget -nv -O "$FILE" https://nodejs.org/dist/v$VERSION/node-v$VERSION-linux-x64.tar.xz
validate_checksum "$CHECKSUM" "$FILE"

mkdir -p "$DIR"
tar Jxfo "$FILE" --directory="$DIR" --strip-components=1 \
$ARCHIVE_ROOT_DIR/bin \
$ARCHIVE_ROOT_DIR/include \
$ARCHIVE_ROOT_DIR/lib \
$ARCHIVE_ROOT_DIR/share
ln -s /usr/local/bin/node /usr/local/bin/nodejs

success "✓ Elixir: Environment variables exported"
}

apply_options() {
[ "$OPT_DOCKERFILE" -eq 1 ] && install_hadolint
[ "$OPT_ELIXIR" -eq 1 ] && setup_elixir_env
[ "$OPT_FONTS" -eq 1 ] && install_fonts
[ "$OPT_NODE" -eq 1 ] && install_node

if [ "$OPT_SHELLSCRIPT" -eq 1 ]; then
install_shellcheck
Expand Down
14 changes: 14 additions & 0 deletions tests/install_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ def test_home_directory(host):

@pytest.mark.order(2)
@pytest.mark.parametrize('name, version', [
# Node
('node', '14.16.0'),
('npm', '6.14.11'),
('npx', '6.14.11'),
# Other binaries
('hadolint', '2.1.0'),
('shellcheck', '0.7.1'),
('shfmt', '3.2.4'),
Expand Down Expand Up @@ -156,3 +161,12 @@ def test_enabled_elixir_env_vars(host, env_var):
zshenv = host.file(HOME + '/.zshenv')

assert zshenv.contains(rf'^export {env_var}=')


@pytest.mark.order(2)
@pytest.mark.parametrize('path, origin', [
('/usr/local/bin/nodejs', '/usr/local/bin/node'),
])
def test_symlinks(host, path, origin):
assert host.file(path).is_symlink
assert host.file(path).linked_to == origin
15 changes: 15 additions & 0 deletions tests/uninstall_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def test_uninstalled_packages(host, name):
@pytest.mark.order(4)
@pytest.mark.parametrize('command, path', [
('hadolint --version', BIN + '/hadolint'),
('node --version', BIN + '/node'),
('npm --version', BIN + '/npm'),
('shellcheck --version', BIN + '/shellcheck'),
('shfmt --version', BIN + '/shfmt'),
])
Expand Down Expand Up @@ -67,11 +69,19 @@ def test_uninstall_gitstatus(host, path):

@pytest.mark.order(4)
@pytest.mark.parametrize('path', [
# Node
(BIN + '/node'),
(BIN + '/npm'),
(BIN + '/npx'),
('/usr/local/share/man/man1/node.1'),
('/usr/local/share/systemtap/tapset/node.stp'),
# oh-my-zsh
(HOME + '/.z'),
(HOME + '/.zshenv'),
(HOME + '/.zshrc'),
(HOME + '/.p10k.zsh'),
(HOME + '/.oh-my-zsh/custom/aliases.zsh'),
# fonts
(FONTS + '/Fura Code Light Nerd Font Complete.ttf'),
(FONTS + '/Fura Code Regular Nerd Font Complete.ttf'),
(FONTS + '/Fura Code Medium Nerd Font Complete.ttf'),
Expand All @@ -84,6 +94,11 @@ def test_removed_files(host, path):

@pytest.mark.order(4)
@pytest.mark.parametrize('path', [
# Node
('/usr/local/include/node'),
('/usr/local/lib/node_modules'),
('/usr/local/share/doc/node'),
# oh-my-zsh
(HOME + '/.oh-my-zsh'),
(HOME + '/.cache/p10k-' + USER),
(HOME + '/.config/znt'),
Expand Down
14 changes: 14 additions & 0 deletions uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ uninstall_binaries() {
success "✓ Binaries uninstalled"
}

uninstall_node() {
remove "$BIN/node"
remove "$BIN/npm"
remove "$BIN/npx"
remove "/usr/local/include/node"
remove "/usr/local/lib/node_modules"
remove "/usr/local/share/doc/node"
remove "/usr/local/share/man/man1/node.1"
remove "/usr/local/share/systemtap/tapset/node.stp"

success "✓ Node uninstalled"
}

# Input: $@ -> options
main() {
uninstall_fonts
Expand All @@ -240,6 +253,7 @@ main() {
uninstall_gitstatus
uninstall_oh_my_zsh
uninstall_binaries
uninstall_node
uninstall_packages

success "✓ Uninstall done"
Expand Down

0 comments on commit 2fc2757

Please sign in to comment.