// Description: Drive homebrew through Kit-script
// Author: Pierre Borckmans
// shortcut: ctrl opt cmd b
import "@johnlindquist/kit"
const BREW_CMD = '/opt/homebrew/bin/brew'
const cmdList = async (cmd) => (await cmd)._stdout.split("\n").slice(0, -1)
const getInstalled = async (type) => {
return await cmdList($`${BREW_CMD} list --${type} -1`)
}
const getAvailable = async (type) => {
const items = (await cmdList($`${BREW_CMD} ${type} -1`)).map(o => ({
name: o,
value: o,
description: type.slice(0, -1)
}))
const alreadyInstalled = await getInstalled(type.slice(0, -1))
return items.filter(i => !alreadyInstalled.find(ai => ai === i.value));
}
const install = async () => {
const packageName = await arg("Enter a package to install", [...await getAvailable("formulae"), ...await getAvailable("casks")])
await $`${BREW_CMD} install ${packageName}`
}
const uninstall = async () => {
const packageName = await arg("Choose a package to uninstall", [...await list("formula"), ...await list("cask")])
await $`${BREW_CMD} uninstall ${packageName}`
}
const menu = async () => {
const menuOptions = [
{
value: "formulae",
name: "List installed formulae"
},
{
value: "casks",
name: "List installed casks"
},
{
value: "install",
name: "Install a cask or formula"
},
{
value: "uninstall",
name: "Uninstall a cask or formula"
},
]
const menuChoice = await arg("Select an option", menuOptions)
switch (menuChoice) {
case "formulae":
await arg(`Homebrew formulas`, await getInstalled("formula"))
break
case "casks":
await arg(`Homebrew casks`, await getInstalled("cask"))
break
case "install":
await install()
break
case "uninstall":
await uninstall()
break
default:
break;
}
await menu()
}
await menu();