Created
May 19, 2019 21:42
-
-
Save Conduitry/08acfb182ecf508500a7efd42d5a359b to your computer and use it in GitHub Desktop.
Easy simple npm linker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| if [ $# = 0 ]; then echo 'Arguments: <package>...'; exit 1; fi | |
| DIR="$PWD" | |
| for PKG in "$@"; do | |
| BASE="$(basename "$PKG")" | |
| while :; do | |
| if [ -e node_modules/"$PKG" ]; then | |
| DEST="$PWD"/node_modules/"$PKG" | |
| while :; do | |
| if [ -d "$BASE" ]; then | |
| TARGET="$(realpath -L --relative-to "$DEST"/.. "$BASE")" | |
| echo "$DEST -> $TARGET" | |
| rm -rf "$DEST" | |
| ln -s "$TARGET" "$DEST" | |
| break | |
| fi | |
| if [ "$PWD" = / ]; then >&2 echo Could not find source of $PKG; break; fi | |
| cd .. | |
| done | |
| break | |
| fi | |
| if [ "$PWD" = / ]; then >&2 echo Could not find usage of $PKG; break; fi | |
| cd .. | |
| done | |
| cd "$DIR" | |
| done |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When run with
foo, this searches up until it findsnode_modules/fooand then searches further up until it findsfooand creates a symbolic link.When run with
@foo/bar, this searches up until it findsnode_modules/@foo/barand then searches further up until it findsbarand creates a symbolic link.This uses GNU Bash isms, and will almost certainly need adjustment for macOS or other Bashes.