Skip to content

Instantly share code, notes, and snippets.

@Yelmor
Created January 1, 2021 12:42
Show Gist options
  • Save Yelmor/14b5c81f0463d968af5619d44393d4ec to your computer and use it in GitHub Desktop.
Save Yelmor/14b5c81f0463d968af5619d44393d4ec to your computer and use it in GitHub Desktop.
delete all node_modules
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