Finding Unused files with Require.js imports

In an effort to clean up some code, I wanted to find a list of files not imported by my app. I didn't see anything in a brief search, so I wrote this little script. Hopefully its helpful. It finds all javascript files in directories you specify as parameters to the script, greps your git repository for references to those files and echos those that should be removed.

This script does have the possibility for false negatives (ie: things that aren't used that we don't mention) because we use basenames. Examples might be a foo/utils.js which is used hiding the bar/utils.js which isn't used. You may need to run this multiple times as deleting some files will uncover new unused files.

#!/bin/bash
JS_FILES=$(find "$@" -name "*.js")

for FILE in $JS_FILES; do
    short_name=`basename $FILE`
    filename="${short_name%.*}"
    git grep --quiet $filename 1>/dev/null
    if [ "$?" == "1" ]; then
        echo "Should delete: $FILE"
    fi
done;
© 2012 - 2023 · Home — Theme Simpleness