/ package / cleanNpmModules.ps1
cleanNpmModules.ps1
 1  # cleanNpmModules.ps1
 2  [CmdletBinding()]
 3  param(
 4    [string] $NodeModulesPath = ".\node_modules"
 5  )
 6  
 7  $ErrorActionPreference = "Stop"
 8  
 9  $root = Resolve-Path -LiteralPath $NodeModulesPath -ErrorAction Stop
10  
11  # Remove top-level + one-level-deep folders
12  $dirsToRemove = @(
13    ".cache",
14    "docs",
15    "esm",
16    "umd",
17    "mjs",
18    "types",
19    "typings",
20    "example",
21    "examples",
22    "benchmarks",
23    "test",
24    "tests",
25    ".nyc_output",
26    "coverage",
27    ".github",
28    ".gitlab",
29    ".vscode",
30    ".idea",
31    ".husky",
32    "dist.es2015",
33    "dist.esm",
34    "dist-esm",
35    "__tests__",
36    "__mocks__",
37    "__fixtures__",
38    "__snapshots__"
39  )
40  
41  # node_modules/.cache
42  foreach ($d in $dirsToRemove) {
43    Remove-Item -LiteralPath (Join-Path $root $d) -Recurse -Force -ErrorAction SilentlyContinue
44  }
45  
46  # node_modules/*/<dir>
47  foreach ($d in $dirsToRemove) {
48    Get-ChildItem -LiteralPath $root -Directory -ErrorAction SilentlyContinue |
49      ForEach-Object {
50        Remove-Item -LiteralPath (Join-Path $_.FullName $d) -Recurse -Force -ErrorAction SilentlyContinue
51      }
52  }
53  
54  # Delete files by patterns
55  $patternsToDelete = @(
56    "*.md", "*.mdx", "*.markdown", "*.txt",
57    "LICENSE*", "LICENSE", "license", "license*",
58    "CHANGELOG*", "CONTRIBUTING*", "README*", "readme*",
59    "AUTHORS", "Jenkinsfile", "Makefile",
60  
61    "*.ts", "*.mts", "*.tsx", "*.tsbuildinfo",
62    "*.map", "*.d.cts",
63    ".eslint*", "eslint.config.js", "tsconfig.json",
64    ".editorconfig", ".mzrc", "bower.json", ".babelrc",
65    "biome.json",
66    "jest.config.*", "rollup.config.*", "commitlint.config.*",
67    "*.spec.js",
68    ".jsdoc*", "*.jsdoc", "*.jshint",
69    ".prettier*",
70    ".npm*", ".travis*",
71  
72    "*.png", "*.gif", "*.ico", "*.jpg",
73    "*.css", "*.html", "*.svg", "*.swf",
74  
75    "*.cpp", "*.c", "*.h", "*.hpp", "*.cc",
76    "*.patch", "*.csv", "*.coffee",
77    "*.cmd", "*.bat",
78    "*.cs", "*.exe", "*.pdb",
79    "*.ps1", "*.sh",
80    "*.py", "*.pyc",
81    "*.proto", "*.scss",
82    "*.sln", "*.vcxproj",
83    "*.tar", "*.zip",
84    "*.url", "*.xml",
85    "*.php", "*.snap", "*.toml", "*.rs", "*.zig", "*.hbs",
86    "*.gyp", "*.gypi"
87  )
88  
89  foreach ($pattern in $patternsToDelete) {
90    Get-ChildItem -LiteralPath $root -Recurse -File -Force -Filter $pattern -ErrorAction SilentlyContinue |
91      Remove-Item -Force -ErrorAction SilentlyContinue
92  }