feat: add uninstall command (#21)

* feat: add uninstall command

Signed-off-by: Chapman Pendery <cpendery@vt.edu>

* fix: linting issues

Signed-off-by: Chapman Pendery <cpendery@vt.edu>

---------

Signed-off-by: Chapman Pendery <cpendery@vt.edu>
This commit is contained in:
Chapman Pendery
2023-11-07 10:38:29 -08:00
committed by GitHub
parent cddb54ae3d
commit 9d5a5efc5c
4 changed files with 95 additions and 0 deletions
+15
View File
@@ -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;
+2
View File
@@ -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();
+15
View File
@@ -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",
);
};
+63
View File
@@ -115,6 +115,69 @@ export const availableBindings = async (): Promise<Shell[]> => {
return bindings;
};
export const unbindAll = async (): Promise<void> => {
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<void> => {
const cliConfigPath = path.join(os.homedir(), cacheFolder);
if (fs.existsSync(cliConfigPath)) {
fs.rmSync(cliConfigPath, { recursive: true });
}
};
export const bind = async (shell: Shell): Promise<void> => {
const cliConfigPath = path.join(os.homedir(), cacheFolder);
if (!fs.existsSync(cliConfigPath)) {