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')
|
|
|
|
|
2020-11-07 12:22:31 +00:00
|
|
|
const { version } = require('../package.json')
|
|
|
|
|
2020-05-10 22:53:00 +00:00
|
|
|
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-11-07 12:22:31 +00:00
|
|
|
console.log(
|
|
|
|
`${chalk.bold.blue('UnBrew')} - Brew cleanup utility\nVersion: ${version}\n`
|
|
|
|
)
|
2020-05-10 22:53:00 +00:00
|
|
|
|
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)
|
2020-12-29 20:35:49 +00:00
|
|
|
if (loosers.length === 0) {
|
|
|
|
console.log(chalk.bold('No package/s selected for deletion.'))
|
|
|
|
return
|
|
|
|
}
|
2020-05-11 16:14:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:52:38 +00:00
|
|
|
console.log('🗑 Uninstalling')
|
2020-11-07 12:11:11 +00:00
|
|
|
const allLoosers = []
|
2020-05-11 16:14:32 +00:00
|
|
|
while (loosers.length) {
|
2020-11-07 12:11:11 +00:00
|
|
|
allLoosers.push(...loosers)
|
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-11-07 12:11:11 +00:00
|
|
|
console.log('✅ Uninstalled: ' + allLoosers.join(', '))
|
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'))
|
|
|
|
}
|
2020-12-29 20:35:49 +00:00
|
|
|
main().finally(() => {
|
|
|
|
console.log(chalk.blue('Bye Bye 👋'))
|
|
|
|
})
|