Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(auto-reshim): add npm wrapper and remove npm hooks #232

Merged
merged 8 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Created by https://www.toptal.com/developers/gitignore/api/linux,macos,windows
# Edit at https://www.toptal.com/developers/gitignore?templates=linux,macos,windows

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*
augustobmoura marked this conversation as resolved.
Show resolved Hide resolved

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/linux,macos,windows
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Node.js plugin for [asdf](https://github.com/asdf-vm/asdf) version manager
After installing [asdf](https://github.com/asdf-vm/asdf), install the plugin by running:

```bash
asdf plugin add nodejs
asdf plugin add https://github.com/asdf-vm/asdf-nodejs.git
```

## Use
Expand Down Expand Up @@ -68,10 +68,6 @@ The plugin automatically imports the NodeJS release team's OpenPGP keys. If you
bash -c '${ASDF_DATA_DIR:=$HOME/.asdf}/plugins/nodejs/bin/import-previous-release-team-keyring'
```

## Temporarily disable reshimming

To avoid a slowdown when installing large packages (see https://github.com/asdf-vm/asdf-nodejs/issues/46), you can `ASDF_SKIP_RESHIM=1 npm i -g <package>` and reshim after installing all packages using `asdf reshim nodejs`.

## Using a dedicated OpenPGP keyring

The `bash` script mentioned in [the installation instructions](#install) (`import-release-team-keyring`) imports the OpenPGP public keys in your main OpenPGP keyring. However, you can also use a dedicated keyring in order to mitigate [this issue](https://github.com/nodejs/node/issues/9859).
Expand Down
4 changes: 0 additions & 4 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ install_canon_version() {
else
tar zxf "$archive_path" -C "$install_path" --strip-components=1 || exit 1
fi

mkdir -p "$install_path/.npm/lib/node_modules/.hooks"
cp "$(dirname "$(dirname "$0")")"/npm-hooks/* "$install_path/.npm/lib/node_modules/.hooks/"
chmod +x "$install_path"/.npm/lib/node_modules/.hooks/*
)
}

Expand Down
10 changes: 0 additions & 10 deletions bin/postinstall

This file was deleted.

7 changes: 6 additions & 1 deletion lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ fi

ASDF_NODEJS_KEYRING=asdf-nodejs.gpg

ASDF_NODEJS_PLUGIN_NAME="$(basename "$(dirname "$(dirname "${BASH_SOURCE[0]}")")")"
# TODO: Replace with an asdf variable once asdf starts providing the plugin name
# as a variable
plugin_name() {
basename "$(dirname "$(dirname "$0")")"
printf "%s\n" "$ASDF_NODEJS_PLUGIN_NAME"
augustobmoura marked this conversation as resolved.
Show resolved Hide resolved
}

ASDF_DIR="${ASDF_DIR:-$HOME/.asdf}"
export ASDF_NODEJS_PLUGIN_NAME ASDF_DIR

die() {
>&2 echo "$@"
exit 1
Expand Down Expand Up @@ -81,3 +85,4 @@ print_index_tab(){

rm "$temp_headers_file"
}

5 changes: 0 additions & 5 deletions npm-hooks/postinstall

This file was deleted.

68 changes: 68 additions & 0 deletions shims/npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#! /usr/bin/env bash

set -eu
set -o pipefail

# This script wraps around npm, so to run `asdf reshim` after a global install/uninstall
# Any other cases are passed-through to npm

this_dir="$(dirname "${BASH_SOURCE[0]}")"
plugin_name="$(basename "$(dirname "$this_dir")")"

quote_path() {
sed 's/\\/\\\\/g; s/\//\\\//g' <<< "$1"
}

# Remove current script from PATH and search the next by priority npm executable
# It works because asdf-core adds every binary path candidate to PATH before calling this script
filtered_path="$(sed "s/$(quote_path "$this_dir")//g; s/::/:/g" <<< "$PATH")"
npm="$(PATH="$filtered_path" command -v npm)"

should_reshim() {
if [ "${ASDF_SKIP_RESHIM:-}" ]; then
return 1
fi

local is_global= cmd= cmd_needs_reshim=

for arg; do
case "$arg" in
-g|--global)
is_global=true
;;

-*) ;; # Skip other options

*)
cmd="${cmd:-$arg}"
;;
esac
done

case "$cmd" in
# npm install aliases
install|i|in|ins|inst|insta|instal|isnt|isnta|isntal|add)
cmd_needs_reshim=true
;;

# npm uninstall aliases
uninstall|un|unlink|remove|rm|r)
cmd_needs_reshim=true
;;
esac

if [ "$is_global" ] && [ "$cmd_needs_reshim" ]; then
return 0
fi

return 1
}


if should_reshim "$@"; then
"$npm" "$@"
printf "Reshimming asdf %s...\n" "$plugin_name" >&2
asdf reshim "$plugin_name"
else
exec "$npm" "$@"
fi