Skip to content

Instantly share code, notes, and snippets.

@PhantomRay
Forked from gpittarelli/node_prune.sh
Last active March 10, 2021 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhantomRay/ba98b8d082876d9cfb6c9b683caae75f to your computer and use it in GitHub Desktop.
Save PhantomRay/ba98b8d082876d9cfb6c9b683caae75f to your computer and use it in GitHub Desktop.
Prunes very-likely-uneeded files from node_modules directories
#!/usr/bin/env bash
# Port of https://github.com/tj/node-prune to bash
# Also,
# - fixed "*.ts" being overzealous and killing .d.ts files
# - added some more file types from https://github.com/npm/npm/issues/5264#issuecomment-259800486
#
# See also:
# - https://github.com/timoxley/cruft
# - https://yarnpkg.com/en/docs/cli/autoclean
# - https://github.com/ModClean/modclean
#
# Prunes common files that are unnecessarily published in npm packages
# when people don't configure `.npmignore` or package.json's `files`
echo "Before: "$(du -hs .)
# Common unneeded files
find . -type d -name node_modules -prune -exec find {} -type f \( \
-iname Makefile -or \
-iname README -or \
-iname README.md -or \
-iname CHANGELOG -or \
-iname CHANGELOG.md -or \
-name .editorconfig -or \
-name .gitmodules -or \
-name .gitattributes -or \
-name robot.html -or \
-name .lint -or \
-name Gulpfile.js -or \
-name Gruntfile.js -or \
-name .tern-project -or \
-name .gitattributes -or \
-name .editorconfig -or \
-name .eslintrc -or \
-name .jshintrc -or \
-name .flowconfig -or \
-name .documentup.json -or \
-name .yarn-metadata.json -or \
-name .travis.yml -or \
-name thumbs.db -or \
-name .ds_store -or \
-name desktop.ini -or \
-name npm-debug.log -or \
-name .npmrc -or \
-iname LICENSE.txt -or \
-iname LICENSE -or \
-iname AUTHORS -or \
-iname CONTRIBUTORS -or \
-name .yarn-integrity -or \
-name "*.md" -or \
-name "*.sln" -or \
-name "*.gypi" -or \
-name "*.vcxproj" -or \
-name "*.vcxproj.filters" -or \
-name "*.ts" -or \
-name "*.map" -or \
-name "*.jst" -or \
-name "*.coffee" \
-name "*.c" \
-name "*.h" \
\) -print0 \; | xargs -0 rm -rf
# Common unneeded directories
find . -type d -name node_modules -prune -exec find {} -type d \( \
-name __tests__ -or \
-name test -or \
-name tests -or \
-name powered-test -or \
-name docs -or \
-name doc -or \
-name example -or \
-name examples -or \
-name coverage -or \
-name node-gyp -or \
-name node-pre-gyp -or \
-name gyp -or \
-name .nyc_output \
\) -print0 \; | xargs -0 rm -rf
echo "After: "$(du -hs .)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment