Skip to content

Instantly share code, notes, and snippets.

@adalinesimonian
Last active September 18, 2015 01:03
Show Gist options
  • Save adalinesimonian/e196d20aa171a4428519 to your computer and use it in GitHub Desktop.
Save adalinesimonian/e196d20aa171a4428519 to your computer and use it in GitHub Desktop.
Find all "require"s for dependencies and find out if all "require"s have matching dependencies
#!/bin/sh
# Requires ag - The Silver Searcher (https://github.com/ggreer/the_silver_searcher)
# (no options) - checks if dependencies are in use
# -d - checks if all requires have a matching dependency entry
# One-liner that checks for in-use dependencies
# grep -Pzo "(?s)(?<=\"dependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)" | xargs -L1 --replace sh -c "if ! ag \"require[\\( ]'{}'\" > /dev/null 2> /dev/null; then printf '\\033[1;31mREQUIRE NOT FOUND:\\t%s\033[m\\n' {}; else printf 'Require found:\\t\\t%s\\n' {}; fi"
requires="$(ag "require(\(| )('|\")[^'\"]+" | grep -ohP "(?<=require[\( ]('|\"))[^'\"/]+(?=('|\"))" | sort -u)"
dependencies="$(grep -Pzo "(?s)(?<=\"dependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)")"
devDependencies="$(grep -Pzo "(?s)(?<=\"devDependencies\": {)[^}]+" package.json | grep -ohP "(?<=\")[^\"]+(?=\":)")"
nativeModules="$(node -e "console.log(Object.keys(process.binding('natives')).filter(function (el) {return !/^_|^internal/.test(el) && ['freelist', 'sys'].indexOf(el) === -1;}).join('\\n'))")"
if [ "$1" = "-d" ]; then
printf '%s\n' "$requires" | while IFS= read -r line
do
if [ ! -z "$(echo "$dependencies" | grep -Pzo "^$line$")" ]; then
printf 'Dependency found:\t%s\n' $line
elif [ ! -z "$(echo "$devDependencies" | grep -Pzo "^$line$")" ]; then
printf 'Dev dependency found:\t%s\n' $line
elif [ ! -z "$(echo "$nativeModules" | grep -Pzo "^$line$")" ]; then
printf 'Native module found:\t%s\n' $line
else
printf '\033[1;31mDEPENDENCY NOT FOUND:\t%s\033[m\n' $line
fi
done
else
printf '%s\n' "$dependencies" | while IFS= read -r line
do
if [ ! -z "$(echo "$requires" | grep -Pzo "^$line$")" ]; then
printf 'Require found:\t\t%s\n' $line
else
printf '\033[1;31mREQUIRE NOT FOUND:\t%s\033[m\n' $line
fi
done
printf '%s\n' "$devDependencies" | while IFS= read -r line
do
if [ ! -z "$(echo "$requires" | grep -Pzo "^$line$")" ]; then
printf 'Require found:\t\t%s\n' $line
else
printf '\033[1;31mREQUIRE NOT FOUND:\t%s\033[m\n' $line
fi
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment