Skip to content

Instantly share code, notes, and snippets.

@gpittarelli
Last active March 3, 2022 17:04
Show Gist options
  • Save gpittarelli/64d1e9b7c1a4af762ec467b1c7571dc2 to your computer and use it in GitHub Desktop.
Save gpittarelli/64d1e9b7c1a4af762ec467b1c7571dc2 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 "*.obj" -or \
-name "*.gypi" -or \
-name "*.vcxproj" -or \
-name "*.vcxproj.filters" -or \
\( -name '*.ts' -and \! -name '*.d.ts' \) -or \
-name "*.jst" -or \
-name "*.coffee" \
\) -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 website -or \
-name images -or \
-name assets -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 .)
@neophob
Copy link

neophob commented Nov 19, 2017

about license files, they tend to be quite different:

LICENSE extensions:

  • .md
  • .txt
  • -MIT
  • -MIT.txt
  • .BSD
  • -BSD
  • -jsbn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment