Last active
November 19, 2022 09:25
-
-
Save maatthc/8caa1e167177d56986563cf3b14196a3 to your computer and use it in GitHub Desktop.
Search and Destroy - Free up disk space deleting "node_modules" folders recursively
This file contains 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
#!/usr/bin/env node | |
// Search and Destroy | |
// | |
const path = require('path') | |
const fs = require('fs') | |
const createPackageJson = () => { | |
const packageJsonContent = ` | |
{ | |
"name": "sad", | |
"main": "sad.js", | |
"dependencies": { | |
"commander": "^2.20.0", | |
"chalk-pipe": "^2.0.0", | |
"fs-extra": "^7.0.1" | |
} | |
} | |
` | |
fs.writeFileSync(`${__dirname}/package.json`, packageJsonContent) | |
} | |
try { | |
const program = require.resolve('commander') | |
const chalkPipe = require.resolve('chalk-pipe') | |
const fsExtra = require.resolve('fs-extra') | |
} catch (error) { | |
console.error('Please install missing libraries! (Run: yarn)') | |
createPackageJson() | |
process.exit(1) | |
} | |
const program = require('commander') | |
const chalkPipe = require('chalk-pipe') | |
const fsExtra = require('fs-extra') | |
const chalk = (format) => (text) => { | |
return console.log(chalkPipe(format)(`${text}`)) | |
} | |
const okMsg = chalk('bgGreen.black') | |
const errorMsg = chalk('bgRed.#cccccc') | |
const warningMsg = chalk('orange.bold') | |
const textMsg = chalk('bgBlue.bold.white') | |
function* walkSync(dir) { | |
const files = fs.readdirSync(dir) | |
for (const file of files) { | |
const pathToFile = path.join(dir, file) | |
const isDirectory = fs.lstatSync(pathToFile).isDirectory() | |
if (isDirectory) { | |
if (file === 'node_modules') { | |
yield pathToFile | |
} else { | |
yield* walkSync(pathToFile) | |
} | |
} | |
} | |
} | |
let workingDir | |
program | |
.version('0.0.1') | |
.arguments('<dir>') | |
.option('-e, --exclude [directory]', 'Do not remove "node_modules" within this directory - [Optional]') | |
.action((dir) => { | |
workingDir = dir | |
}) | |
program.on('--help', function () { | |
textMsg('') | |
textMsg('Examples:') | |
textMsg(' $ sad ') | |
textMsg(' $ sad -e my_active_project') | |
textMsg(' $ sad ~') | |
textMsg(' $ sad --help') | |
}) | |
program.parse(process.argv) | |
if (typeof (workingDir) === 'undefined') { | |
workingDir = '.' | |
} | |
const options = program.opts() | |
if (options.exclude) errorMsg(`Excluding: ${options.exclude}`) | |
okMsg('Starting..') | |
console.time('time:spent:onDelete') | |
for (const folder of walkSync(workingDir)) { | |
if(!folder.startsWith(options.exclude)){ | |
warningMsg(` Deleting folder : ${folder}`) | |
fsExtra.removeSync(folder) | |
} | |
} | |
console.timeEnd('time:spent:onDelete') | |
textMsg('Done!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment