Last active
May 25, 2020 12:35
-
-
Save loopmode/81e2ce88c7556d8fabfbcf6310a893ac to your computer and use it in GitHub Desktop.
Bash script to find folder paths of installed node packages by name
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/sh | |
# finds node packages installed in a given directory | |
# - takes a directory path and one or more package names | |
# - finds all package.json files in the directory that have a matching name | |
# - returns the paths to the package folders | |
# | |
# Usage example: | |
# | |
# $ find_installed_packages.sh ./project/.cache/ foo foo-utils | |
# ./project/.cache/module__3a7ad8f32129c4035c57e71c60b41b74cb025412a55914eb5fcc42a24ff66cde | |
# ./project/.cache/module__3a7ad8f32129c4035c57e71c60b41b74cb025412a55914eb5fcc42a24ff66cde/node_production_modules/foo-utils | |
# | |
target_dir="$1" | |
shift | |
package_names=("$@") | |
if [ -z $target_dir ]; then | |
echo "No target folder provided" | |
exit | |
fi | |
if [ ! -d $target_dir ]; then | |
echo "Target folder does not exist" | |
exit | |
fi | |
if [ "${#package_names[@]}" == "0" ]; then | |
echo "No package names provided" | |
exit | |
fi | |
for package_name in ${package_names[@]}; do | |
find $target_dir \ | |
-name package.json \ | |
-type f \ | |
| xargs grep "\"name\": \"$package_name\"" \ | |
| xargs dirname {} \ | |
| grep $target_dir | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment