Skip to content

Instantly share code, notes, and snippets.

@tienhieuD
Created May 12, 2023 06:55
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 tienhieuD/609ba69ca9aa2d1b4eaf82838607af56 to your computer and use it in GitHub Desktop.
Save tienhieuD/609ba69ca9aa2d1b4eaf82838607af56 to your computer and use it in GitHub Desktop.
Delete all node_modules of directory
const fs = require("fs");
const path = require("path");
const directory = "E:\\";
const maxDepth = 6;
const ignoreDir = [".git", ".pnpm", ".tool", "$RECYCLE.BIN", ".tmp"]
let total = 0;
const findDelNodeModules = (pathDir, depth = 0) => {
total++
const isDir = fs.statSync(pathDir).isDirectory();
const ignore = ignoreDir.some((idir) => pathDir.includes(idir))
if (depth === maxDepth || !isDir || ignore) {
console.log("info", "exit",
isDir ? 1 : 0,
ignore ? 1 : 0,
depth === maxDepth ? 1 : 0,
total,
pathDir);
return;
}
const subdirs = fs.readdirSync(pathDir);
for (sdir of subdirs) {
try {
const newPath = path.join(pathDir, sdir);
if (sdir === "node_modules" || sdir === ".next" || sdir === ".cache") {
console.log("info", "found", newPath);
fs.rmSync(newPath, { recursive: true, force: true });
} else {
findDelNodeModules(newPath, depth + 1)
}
} catch (err) {
console.log("err", JSON.stringify(err.message));
}
}
}
findDelNodeModules(directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment