Created
May 12, 2023 06:55
-
-
Save tienhieuD/609ba69ca9aa2d1b4eaf82838607af56 to your computer and use it in GitHub Desktop.
Delete all node_modules of directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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