Table of contents
No headings in the article.
Sometimes, we find that we install more than we need when it comes to Node Modules. The side effects are felt almost immediately. From slowed-down GitHub Actions to slower loading or deployment time. To fix this, you can go ahead and do some periodic clean-ups.
Today we'll be coming up with a bash script to output **used and unused dependencies. A bash script is a text file that contains a series of commands written in the bash shell language. These commands are executed in sequence when the script is run, making it a convenient way to automate tasks and simplify complex operations.
#!/bin/bash
if ! command -v jq >/dev/null 2>&1; then
echo "Please install jq" >&2
exit 1
fi
DIRNAME=${1:-.}
cd "$DIRNAME"
FILES=$(mktemp) || { echo "Failed to create temporary file" >&2; exit 1; }
PACKAGES=$(mktemp)
export NUMCONCURRENT=8
function findCmd {
startPath=${1:-.}
find "$startPath" -type f \
\( -name "*.ts" -or -name "*.js" -or -name "*.json" \) \
! -path "./node_modules/*" \
! -path "./build/*" \
-print
}
function check {
jq -r ".$1 | keys[]?" package.json > "$PACKAGES"
echo "--------------------------"
echo "Checking $1..."
findCmd > "$FILES"
export FILES
export SQ="'"
xargs -P ${NUMCONCURRENT:-1} -r -I[] bash -c "
PACKAGE='[]'
RES=\$(grep -E -r -w 'import .*[]|require\\(.*[]\\)' \$FILES | wc -l)
if [ \$RES -eq 0 ]; then
echo -e \"UNUSED\t\t \$PACKAGE\"
else
echo -e \"USED (\$RES)\t \$PACKAGE\"
fi
" < "$PACKAGES"
rm -f "$PACKAGES" "$FILES"
}
check "dependencies"
check "devDependencies"
check "peerDependencies"
The script parses through a Node.js project and performs the following steps:
Checks if
jq
(json-parser) is installed. If it's not, the script prints an error message and exits with a non-zero status code.Sets the current working directory to the directory specified as the first argument to the script (or to the current directory if no argument is given).
It creates two temporary files,
FILES
andPACKAGES
, to store intermediate data.Defines a function
findCmd
that uses thefind
command to search for.ts
,.js
, and.json
files in the specified path, excluding thenode_modules
andbuild
directories.It defines a function
check
that checks for unused dependencies of a given type (dependencies
,devDependencies
, orpeerDependencies
). It usesjq
to extract the dependencies frompackage.json
, stores them in thePACKAGES
file, and performs a grep search for each dependency in all the files found by thefindCmd
function. If the dependency is not found in any of the files, the script outputs a message indicating that the dependency is unused. Otherwise, it outputs a message indicating that the dependency is used, along with the number of times it was found.The
check
function is called twice, first to check for unuseddependencies
, and then to check for unuseddevDependencies
andpeerDependencies
.Finally, the temporary files created in step 3 are deleted.
To run the file from the terminal ,perfom the following commands
- Make the file executable
chmod +x /path/to/script.sh
Run the script/path/to/script.sh [directory]
/path/to/script.sh [directory]
This is still a work in progress ๐ฉ๐พโ๐ป, if you have a chance to use it and find ways it can be enhanced, feel free to reach out. I'll be more than happy to engage.