Skip to content

Instantly share code, notes, and snippets.

@Conduitry
Created May 19, 2019 21:42
Show Gist options
  • Save Conduitry/08acfb182ecf508500a7efd42d5a359b to your computer and use it in GitHub Desktop.
Save Conduitry/08acfb182ecf508500a7efd42d5a359b to your computer and use it in GitHub Desktop.
Easy simple npm linker
#!/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
@Conduitry
Copy link
Author

When run with foo, this searches up until it finds node_modules/foo and then searches further up until it finds foo and creates a symbolic link.

When run with @foo/bar, this searches up until it finds node_modules/@foo/bar and then searches further up until it finds bar and creates a symbolic link.

This uses GNU Bash isms, and will almost certainly need adjustment for macOS or other Bashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment