Skip to content

Instantly share code, notes, and snippets.

@ptm108
Created August 15, 2021 10:57
Show Gist options
  • Save ptm108/494faf153e507bb777115cc66a691458 to your computer and use it in GitHub Desktop.
Save ptm108/494faf153e507bb777115cc66a691458 to your computer and use it in GitHub Desktop.
Manual Node Dependency checker script
#!/bin/bash
DIRNAME=${1:-.}
cd $DIRNAME
source ~/.bashrc
FILES=$(mktemp)
PACKAGES=$(mktemp)
find . \
-path ./node_modules -prune -or \
-path ./dist -prune -or \
\( -name "*.tsx" -or -name "*.jsx" -or -name "*.ts" -or -name "*.js" -or -name "*.json" \) -print > $FILES
function check {
cat package.json \
| jq "{} + .$1 | keys" \
| sed -n 's/.*"\(.*\)".*/\1/p' > $PACKAGES
echo "--------------------------"
echo "Checking $1..."
while read PACKAGE
do
# echo "Checking ${PACKAGE}..."
RES=$(cat $FILES | xargs -I {} egrep -i "(import|require|loader|plugins|${PACKAGE}).*['\"](${PACKAGE}|.?\d+)[\"']" '{}' | wc -l)
if [ $RES = 0 ]
then
echo -e "UNUSED\t\t $PACKAGE"
else
echo -e "USED ($RES)\t $PACKAGE"
fi
done < $PACKAGES
}
check "dependencies"
check "devDependencies"
check "peerDependencies"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment