From 9d5a5efc5c8fdecf9dc28a01f6d23ce55b6ee54b Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Tue, 7 Nov 2023 10:38:29 -0800 Subject: [PATCH] feat: add uninstall command (#21) * feat: add uninstall command Signed-off-by: Chapman Pendery * fix: linting issues Signed-off-by: Chapman Pendery --------- Signed-off-by: Chapman Pendery --- src/commands/uninstall.ts | 15 ++++++++++ src/index.ts | 2 ++ src/ui/ui-uninstall.ts | 15 ++++++++++ src/utils/bindings.ts | 63 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 src/commands/uninstall.ts create mode 100644 src/ui/ui-uninstall.ts diff --git a/src/commands/uninstall.ts b/src/commands/uninstall.ts new file mode 100644 index 0000000..bfa9887 --- /dev/null +++ b/src/commands/uninstall.ts @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import { Command } from "commander"; +import { render } from "../ui/ui-uninstall.js"; + +const action = async () => { + await render(); +}; + +const cmd = new Command("uninstall"); +cmd.description(`removes all bindings and configuration for inshellisense`); +cmd.action(action); + +export default cmd; diff --git a/src/index.ts b/src/index.ts index a0ae9fb..5b8ae61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { Command } from "commander"; import bind from "./commands/bind.js"; +import uninstall from "./commands/uninstall.js"; import { action, supportedShells } from "./commands/root.js"; import { getVersion } from "./utils/version.js"; @@ -24,5 +25,6 @@ program .action(action); program.addCommand(bind); +program.addCommand(uninstall); program.parse(); diff --git a/src/ui/ui-uninstall.ts b/src/ui/ui-uninstall.ts new file mode 100644 index 0000000..1469e99 --- /dev/null +++ b/src/ui/ui-uninstall.ts @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +import chalk from "chalk"; +import { unbindAll, deleteConfigFolder } from "../utils/bindings.js"; + +export const render = async () => { + await unbindAll(); + process.stdout.write(chalk.green("✓") + " successfully uninstalled all existing bindings \n"); + deleteConfigFolder(); + process.stdout.write(chalk.green("✓") + " successfully deleted the .inshellisense config folder \n"); + process.stdout.write( + chalk.magenta("•") + " to complete the uninstall, run the the command: " + chalk.underline(chalk.cyan("npm uninstall -g @microsoft/inshellisense")) + "\n", + ); +}; diff --git a/src/utils/bindings.ts b/src/utils/bindings.ts index 33f1ee4..674c877 100644 --- a/src/utils/bindings.ts +++ b/src/utils/bindings.ts @@ -115,6 +115,69 @@ export const availableBindings = async (): Promise => { return bindings; }; +export const unbindAll = async (): Promise => { + try { + const bashConfigPath = path.join(os.homedir(), ".bashrc"); + const bashConfig = (await fsAsync.readFile(bashConfigPath)).toString(); + if (bashConfig.includes(bashScriptCommand())) { + const unboundBashConfig = bashConfig.toString().replace(bashScriptCommand(), ""); + await fsAsync.writeFile(bashConfigPath, unboundBashConfig); + } + } catch { + /* empty */ + } + + try { + const zshConfigPath = path.join(os.homedir(), ".zshrc"); + const zshConfig = (await fsAsync.readFile(zshConfigPath)).toString(); + if (zshConfig.includes(zshScriptCommand())) { + const unboundZshConfig = zshConfig.toString().replace(zshScriptCommand(), ""); + await fsAsync.writeFile(zshConfigPath, unboundZshConfig); + } + } catch { + /* empty */ + } + + try { + const fishConfigPath = path.join(os.homedir(), ".config", "fish", "config.fish"); + const fishConfig = (await fsAsync.readFile(fishConfigPath)).toString(); + if (fishConfig.includes(fishScriptCommand())) { + const unboundFishConfig = fishConfig.toString().replace(fishScriptCommand(), ""); + await fsAsync.writeFile(fishConfigPath, unboundFishConfig); + } + } catch { + /* empty */ + } + + try { + const powershellConfigPath = path.join(os.homedir(), "Documents", "WindowsPowershell", "Microsoft.PowerShell_profile.ps1"); + const powershellConfig = (await fsAsync.readFile(powershellConfigPath)).toString(); + if (powershellConfig.includes(fishScriptCommand())) { + const unboundPowershellConfig = powershellConfig.toString().replace(powershellScriptCommand(), ""); + await fsAsync.writeFile(powershellConfigPath, unboundPowershellConfig); + } + } catch { + /* empty */ + } + + try { + const pwshConfig = (await fsAsync.readFile(pwshConfigPath())).toString(); + if (pwshConfig.includes(fishScriptCommand())) { + const unboundPwshConfig = pwshConfig.toString().replace(pwshScriptCommand(), ""); + await fsAsync.writeFile(pwshConfigPath(), unboundPwshConfig); + } + } catch { + /* empty */ + } +}; + +export const deleteConfigFolder = async (): Promise => { + const cliConfigPath = path.join(os.homedir(), cacheFolder); + if (fs.existsSync(cliConfigPath)) { + fs.rmSync(cliConfigPath, { recursive: true }); + } +}; + export const bind = async (shell: Shell): Promise => { const cliConfigPath = path.join(os.homedir(), cacheFolder); if (!fs.existsSync(cliConfigPath)) {