Created
January 1, 2021 12:42
-
-
Save Yelmor/14b5c81f0463d968af5619d44393d4ec to your computer and use it in GitHub Desktop.
delete all node_modules
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 fsPromises = fs.promises; | |
async function rm(startPath, depth) { | |
const rmPromises = []; | |
const files = await fsPromises.readdir(startPath); | |
if (depth === 0) { | |
return; | |
} | |
for (let file of files) { | |
const filePath = path.join(startPath, file); | |
const stats = await fsPromises.stat(filePath); | |
if (!stats.isDirectory()) { | |
continue; | |
} | |
if (file === 'node_modules') { | |
console.log('rm', filePath); | |
rmPromises.push(fsPromises.rm(filePath, { recursive: true })); | |
} else { | |
rmPromises.push(rm(filePath, depth - 1)); | |
} | |
} | |
return Promise.all(rmPromises); | |
} | |
rm(path.join(__dirname, '../Documents'), 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment