Created
October 24, 2018 00:17
-
-
Save luvies/af3f0b98d50636e1cf690c2f5f82ce9a to your computer and use it in GitHub Desktop.
A Node.js implementation of https://github.com/connorworley/dotfiles/blob/master/.bin/brew-autoremove
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
#!/usr/bin/env node | |
'use strict'; | |
/* | |
A Node.js implementation of | |
https://github.com/connorworley/dotfiles/blob/master/.bin/brew-autoremove | |
*/ | |
const child = require('child_process'); | |
const util = require('util'); | |
childExec = util.promisify(child.exec); | |
function readKey() { | |
return new Promise(resolve => { | |
const proc = child.spawn('read -n 1 -r && echo $REPLY', [], { | |
shell: true, | |
stdio: ['inherit', 'pipe', 'pipe'] | |
}) | |
let stdout = ''; | |
proc.stdout.on('data', chunk => { | |
stdout += chunk.toString(); | |
}) | |
proc.on('exit', () => { | |
resolve(stdout.trim()); | |
}); | |
}); | |
} | |
async function exec(proc) { | |
const res = await childExec(proc); | |
return res.stdout.split('\n').filter(v => v); | |
} | |
const visited = {}; | |
async function check_formulas(...formulas) { | |
visited['brew cask'] = true; | |
for (const formula of formulas) { | |
dependees = await exec(`brew uses --installed ${formula}`); | |
if (!dependees.length && !visited[formula]) { | |
process.stdout.write(`${formula} has no dependees, remove? [Y/n] `); | |
const ans = await readKey(); | |
console.log(); | |
visited[formula] = true; | |
if (!ans.match(/n/i)) { | |
console.log(`Removing ${formula}...`); | |
await childExec(`brew remove ${formula}`); | |
await check_formulas(...(await exec(`brew deps --1 --installed ${formula}`))); | |
} | |
} | |
} | |
} | |
async function main() { | |
await check_formulas(...(await exec('brew list'))); | |
} | |
main().catch(err => { | |
console.error(err); | |
process.exitCode = 1; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment