Bash Script To Check Used And Unused And Unused Dependencies

ยท

3 min read

Table of contents

No heading

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:

  1. 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.

  2. 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).

  3. It creates two temporary files, FILES and PACKAGES, to store intermediate data.

  4. Defines a function findCmd that uses the find command to search for .ts, .js, and .json files in the specified path, excluding the node_modules and build directories.

  5. It defines a function check that checks for unused dependencies of a given type (dependencies, devDependencies, or peerDependencies). It uses jq to extract the dependencies from package.json, stores them in the PACKAGES file, and performs a grep search for each dependency in all the files found by the findCmd 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.

  6. The check function is called twice, first to check for unused dependencies, and then to check for unused devDependencies and peerDependencies.

  7. Finally, the temporary files created in step 3 are deleted.

To run the file from the terminal ,perfom the following commands

  1. Make the file executable
chmod +x /path/to/script.sh
  1. 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.

ย