Skip to content

Instantly share code, notes, and snippets.

@ryanccn
Created May 10, 2021
Embed
What would you like to do?
Better `brew uninstall`
import {
bold,
brightRed,
gray,
green,
} from "https://deno.land/std/fmt/colors.ts";
const packageName = Deno.args[0];
const logPrefix = gray("(brew-uninstall)");
const log = (str: string) => {
console.log(`${logPrefix} ${str}`);
};
const error = (str: string) => {
console.error(`${logPrefix} ${brightRed(str)}`);
};
const decode = (data: Uint8Array) => {
return new TextDecoder("utf-8").decode(data);
};
const infoProc = Deno.run({
cmd: ["brew", "info", packageName, "--json"],
stdout: "piped",
stderr: "piped",
});
const data = JSON.parse(decode(await infoProc.output()));
if (data[0].installed.length === 0) {
error(`${bold(packageName)} is not installed.`);
Deno.exit(1);
}
const deps = data[0].installed[0].runtime_dependencies.map(
(item: Record<string, string>) => item.full_name,
);
const mainUninstall = await Deno.run({
cmd: ["brew", "uninstall", packageName],
stdout: "piped",
stderr: "piped",
}).status();
if (mainUninstall.success) {
log(green(`Uninstalled ${bold(packageName)}`));
} else {
error(`Failed to uninstall ${bold(packageName)}`);
Deno.exit(1);
}
for (const dep of deps) {
const p = Deno.run({
cmd: ["brew", "uninstall", dep],
stdout: "piped",
stderr: "piped",
});
const [status, stderr] = await Promise.all([p.status(), p.stderrOutput()]);
const textStderr = decode(stderr);
if (!status.success) {
if (/because it is required/.test(textStderr)) {
log(gray(`Skipping ${bold(dep)} as it is required by other packages`));
} else {
error(`Failed to uninstall ${bold(dep)} for unknown reason.`);
Deno.exit(1);
}
} else {
log(green(`Uninstalled ${bold(dep)}`));
}
}
const cleanupProc = Deno.run({
cmd: ["brew", "cleanup"],
stderr: "piped",
});
const [status, stderr] = await Promise.all([
cleanupProc.status(),
cleanupProc.stderrOutput(),
]);
if (status.success) {
log("Cleaned up Homebrew!");
} else {
error("Failed to clean Homebrew");
error(decode(stderr));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment