add support for cask

This commit is contained in:
2022-08-01 17:16:35 +02:00
parent 689776d975
commit 9e892cb06e
4 changed files with 155 additions and 76 deletions

View File

@@ -1,10 +1,31 @@
#!/usr/bin/env node
import cp from 'child_process'
import chalk from 'chalk'
import cp from 'child_process'
import inquirer from 'inquirer'
const VERSION = '1.2.3'
const VERSION = '1.3.0'
class ListEntry {
name!: string
type!: 'package' | 'cask'
constructor(options: ListEntry) {
Object.assign(this, options)
}
toString() {
return `${this.type === 'package' ? '📦' : '🍾'} ${this.name}`
}
static parse(line: string): ListEntry {
const [type, name] = line.split(' ')
if (!name) throw new Error('Could not parse line')
if (type === '📦') return new ListEntry({ name, type: 'package' })
if (type === '🍾') return new ListEntry({ name, type: 'cask' })
throw new Error('Could not parse type')
}
}
function checkIfBrewIsInstalled() {
try {
@@ -15,13 +36,30 @@ function checkIfBrewIsInstalled() {
}
}
function getListOfLeaves(): string[] {
function getListOfPackages(): ListEntry[] {
const list = cp.execSync('brew leaves', { encoding: 'utf-8' })
return list.trim().split('\n')
return list
.trim()
.split('\n')
.map((line) => new ListEntry({ name: line, type: 'package' }))
}
function getLoosers(keepers: string[], leaves = getListOfLeaves()): string[] {
return leaves.filter((leave) => !keepers.includes(leave))
function getListOfCasks(): ListEntry[] {
const list = cp.execSync('brew list --cask -1', { encoding: 'utf-8' })
return list
.trim()
.split('\n')
.map((line) => new ListEntry({ name: line, type: 'cask' }))
}
function getList(cask: boolean): ListEntry[] {
const list = getListOfPackages()
if (cask) list.push(...getListOfCasks())
return list
}
function getLoosers(keepers: ListEntry[], leaves: ListEntry[]): ListEntry[] {
return leaves.filter((leave) => !keepers.some((keeper) => keeper.name === leave.name))
}
async function main() {
@@ -31,49 +69,64 @@ async function main() {
}
console.log(`${chalk.bold.blue('UnBrew')} - Brew cleanup utility\nVersion: ${VERSION}\n`)
let leaves: string[]
let loosers: string[]
const { cask } = await inquirer.prompt([
{
type: 'confirm',
name: 'cask',
message: `Also consider casks?`,
},
])
let initialState = getList(cask)
leaves = getListOfLeaves()
const { keepers } = await inquirer.prompt([
{
type: 'checkbox',
message: 'Select packages to keep (all by default)',
name: 'keepers',
choices: leaves.map((leave) => ({
name: leave,
choices: initialState.map((entry) => ({
name: entry,
checked: true,
})),
},
])
loosers = getLoosers(keepers, leaves)
if (loosers.length === 0) {
console.log(chalk.bold('No package/s selected for deletion.'))
return
}
const { confirmed } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmed',
message: `Delelte: ${chalk.bold.blue(loosers.join(' '))}`,
},
])
// Uninstalling
let first = true
let allUninstalled: ListEntry[] = []
while (true) {
// Get all to be uninstalled
const loosers = getLoosers(keepers, first ? initialState : getList(cask))
if (!confirmed) {
console.log(chalk.bold.red('Aborted'))
return
// First time prompt
if (first) {
if (loosers.length === 0) {
console.log(chalk.bold('No package/s selected for deletion.'))
return
}
const { confirmed } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirmed',
message: `Delelte: ${chalk.bold.blue(loosers.map((l) => l.name).join(', '))} and their dependencies?`,
},
])
if (!confirmed) {
console.log(chalk.bold.red('Aborted'))
return
}
console.log('🗑 Uninstalling')
first = false
}
// Actual uninstalling
if (loosers.length === 0) break
allUninstalled.push(...loosers)
cp.execSync(`brew uninstall ${loosers.map((l) => l.name).join(' ')}`)
}
console.log('🗑 Uninstalling')
const allLoosers: string[] = []
while (loosers.length) {
allLoosers.push(...loosers)
const joinedLoosers = loosers.join(' ')
cp.execSync(`brew uninstall ${joinedLoosers}`)
loosers = getLoosers(keepers)
}
console.log('✅ Uninstalled: ' + allLoosers.join(', '))
console.log('✅ Uninstalled: ' + allUninstalled.join(', '))
console.log('🧽 Cleaning up')
cp.execSync(`brew cleanup`)
@@ -81,5 +134,5 @@ async function main() {
console.log(chalk.bold.green('🚀 Done'))
}
main().finally(() => {
console.log(chalk.blue('Bye Bye 👋'))
console.log(chalk.blue('👋 Bye Bye'))
})