-
-
Save kbtz/7d06224d0711d986f3c41b79f87cb019 to your computer and use it in GitHub Desktop.
lnx
This file contains 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 | |
# A npm link alternative that avoids duplication of shared dependencies. | |
# | |
# It will install a packed version of your local package instead of just soft linking | |
# the package directory (which would include its own node_modules in dependency resolution). | |
# | |
# Another advantage is that only files that would be published are included in the packed version, | |
# then these extracted files are replaced with links to their original files. | |
# Hard links are used since many module resolution and bundling tools might try to resolve | |
# soft links to their real paths. | |
# | |
# The installed packages (local and its dependencies) aren't added to the package.json in your | |
# consumer project, so you might want store their paths and the command in your package scripts. | |
# Creating, moving or removing files in the local package will require a rerun to update the | |
# hard links, however I might add a watch mode later. | |
# | |
# Command examples: | |
# [my-project]$ lnx ../my-package | |
# [my-project]$ lnx /path/to/projects/{my-package1,my-package2} | |
cwd=$PWD | |
err() { | |
echo "$@" 1>&2 | |
exit 1 | |
} | |
[ ! -f package.json ] && \ | |
err "package.json not found" | |
# put node_modules in a clean state | |
npm install && mkdir -p node_modules | |
for source in "$@"; do | |
if [ ! -d $source ] || [ ! -f $source/package.json ]; then | |
err "package.json not found at $source" | |
fi | |
done | |
for source in "$@"; do | |
source=$(realpath $source) | |
cd $source | |
archive=$(npm pack) | |
package=${archive%-[0-9]*.[0-9]*.[0-9]*.tgz} | |
cd $cwd | |
npm install $source/$archive --no-save | |
rm $source/$archive | |
cd node_modules/$package || \ | |
err "failed to install $package" | |
for file in $(find . -type f); do | |
rm $file | |
link $source/$file $file | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment