unbrew/bin/unbrew.js

75 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-05-10 23:12:35 +00:00
#!/usr/bin/env node
2020-05-10 22:53:00 +00:00
const cp = require('child_process')
const chalk = require('chalk')
const inquirer = require('inquirer')
function checkIfBrewIsInstalled() {
try {
cp.execSync('brew --version')
return true
} catch (e) {
return false
}
}
function getListOfLeaves() {
const list = cp.execSync('brew leaves', { encoding: 'utf-8' })
return list.trim().split('\n')
}
2020-05-11 16:14:32 +00:00
function getLoosers(keepers, leaves = getListOfLeaves()) {
return leaves.filter((leave) => !keepers.includes(leave))
2020-05-10 22:53:00 +00:00
}
async function main() {
if (!checkIfBrewIsInstalled()) {
console.log(chalk.red.underline('Brew not installed'))
return
}
2020-05-11 16:14:32 +00:00
let leaves
let loosers
leaves = getListOfLeaves()
2020-05-10 22:53:00 +00:00
const { keepers } = await inquirer.prompt([
{
type: 'checkbox',
2020-05-11 16:14:32 +00:00
message: 'Select packages to keep (all by default)',
2020-05-10 22:53:00 +00:00
name: 'keepers',
2020-05-11 16:14:32 +00:00
choices: leaves.map((leave) => ({
2020-05-10 22:53:00 +00:00
name: leave,
checked: true,
})),
},
])
2020-05-11 16:14:32 +00:00
loosers = getLoosers(keepers, leaves)
const { confirmed } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmed',
message: `Delelte: ${chalk.bold.blue(loosers.join(' '))}`,
},
])
2020-05-10 22:53:00 +00:00
2020-05-11 16:14:32 +00:00
if (!confirmed) {
console.log(chalk.bold.red('Aborted'))
return
}
console.log('🗑 Uninstalling')
while (loosers.length) {
2020-05-10 23:12:35 +00:00
const joinedLoosers = loosers.join(' ')
cp.execSync(`brew uninstall ${joinedLoosers}`)
2020-05-11 16:14:32 +00:00
loosers = getLoosers(keepers)
2020-05-10 23:12:35 +00:00
}
2020-05-11 16:14:32 +00:00
2020-05-10 22:53:00 +00:00
console.log('🧽 Cleaning up')
cp.execSync(`brew cleanup`)
2020-05-11 16:14:32 +00:00
2020-05-10 22:53:00 +00:00
console.log(chalk.bold.green('🚀 Done'))
}
main()