Skip to content

Instantly share code, notes, and snippets.

@luvies
Created October 24, 2018 00:17
Show Gist options
  • Save luvies/af3f0b98d50636e1cf690c2f5f82ce9a to your computer and use it in GitHub Desktop.
Save luvies/af3f0b98d50636e1cf690c2f5f82ce9a to your computer and use it in GitHub Desktop.
#!/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