From a6020b4dcedde95f75359f1a1c35c8464a0af401 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 11 May 2020 00:53:00 +0200 Subject: [PATCH] initial commit --- .gitignore | 2 ++ .prettierrc | 6 ++++++ package.json | 17 +++++++++++++++++ src/index.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 .gitignore create mode 100644 .prettierrc create mode 100644 package.json create mode 100644 src/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..254e3d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +yarn.lock +node_modules \ No newline at end of file diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..6fed3d0 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "tabWidth": 2, + "useTabs": false, + "semi": false, + "singleQuote": true +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..9444e98 --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "unbrew", + "version": "0.1.0", + "author": { + "name": "Niccolo Borgioli", + "email": "hi@nicco.io", + "url": "https://nicco.io" + }, + "license": "MIT", + "bin": { + "unbrew": "./src/index.js" + }, + "dependencies": { + "chalk": "4", + "inquirer": "7" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..155550d --- /dev/null +++ b/src/index.js @@ -0,0 +1,54 @@ +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') +} + +function getLoosers(keepers) { + return getListOfLeaves().filter((leave) => !keepers.includes(leave)) +} + +async function main() { + if (!checkIfBrewIsInstalled()) { + console.log(chalk.red.underline('Brew not installed')) + return + } + + const { keepers } = await inquirer.prompt([ + { + type: 'checkbox', + message: 'Uncheck all unwanted packages', + name: 'keepers', + choices: getListOfLeaves().map((leave) => ({ + name: leave, + checked: true, + })), + }, + ]) + + console.log('🗑 Uninstalling:', chalk.bold.blue(getLoosers(keepers).join(' '))) + // while (true) { + // const loosers = getLoosers(keepers) + // if (!loosers.length) break + + // const joinedLoosers = loosers.join(' ') + // cp.execSync(`brew uninstall ${joinedLoosers}`) + // } + console.log('🧽 Cleaning up') + cp.execSync(`brew cleanup`) + console.log(chalk.bold.green('🚀 Done')) +} +main()