diff --git a/src/commands/uninstall.ts b/src/commands/uninstall.ts new file mode 100644 index 0000000..306129a --- /dev/null +++ b/src/commands/uninstall.ts @@ -0,0 +1,41 @@ +import * as vscode from "vscode"; +import * as fsp from "fs/promises"; +import * as path from "path"; +import * as os from "os"; + +export const run = async (cacheDir: string) => { + if (cacheDir[0] === "~") { + cacheDir = path.join(os.homedir(), cacheDir.slice(1)); + } + const bin = path.join(cacheDir, "nextls"); + await fsp + .rm(bin) + .then( + async () => + await vscode.window.showInformationMessage( + `Uninstalled Next LS from ${bin}` + ) + ) + .catch( + async (error) => + await vscode.window.showErrorMessage( + `Failed to uninstall Next LS from ${bin} due to ${error}` + ) + ); +}; + +const registerUninstallCommand = ( + config: vscode.WorkspaceConfiguration, + context: vscode.ExtensionContext +) => { + const uninstallCommand = "elixir-tools.uninstall-nextls"; + + const uninstall = async () => + run(config.get("installationDirectory")); + + context.subscriptions.push( + vscode.commands.registerCommand(uninstallCommand, uninstall) + ); +}; + +export default registerUninstallCommand; diff --git a/src/extension.ts b/src/extension.ts index cf5c1f5..791b71c 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -13,6 +13,8 @@ import { StreamInfo, } from "vscode-languageclient/node"; +import registerUninstallCommand from "./commands/uninstall"; + let credoClient: LanguageClient; let nextLSClient: LanguageClient; @@ -99,33 +101,7 @@ async function activateNextLS( ) { let config = vscode.workspace.getConfiguration("elixir-tools.nextLS"); - const command = "elixir-tools.uninstall-nextls"; - - const uninstallNextLS = async () => { - let cacheDir: string = config.get("installationDirectory")!; - if (cacheDir[0] === "~") { - cacheDir = path.join(os.homedir(), cacheDir.slice(1)); - } - const bin = path.join(cacheDir, "nextls"); - await fsp - .rm(bin) - .then( - async () => - await vscode.window.showInformationMessage( - `Uninstalled Next LS from ${bin}` - ) - ) - .catch( - async () => - await vscode.window.showErrorMessage( - `Failed to uninstall Next LS from ${bin}` - ) - ); - }; - - context.subscriptions.push( - vscode.commands.registerCommand(command, uninstallNextLS) - ); + registerUninstallCommand(config, context); if (config.get("enable")) { let serverOptions: ServerOptions; diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 9f265f9..b5910de 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -6,18 +6,22 @@ import * as path from "path"; // as well as import your extension to test it import * as vscode from "vscode"; import * as myExtension from "../../extension.js"; +import * as uninstall from "../../commands/uninstall.js"; import * as sinon from "sinon"; suite("Extension Test Suite", () => { vscode.window.showInformationMessage("Start all tests."); + let showInformationMessage; setup(function () { fs.rmSync("./test-bin", { recursive: true, force: true }); - sinon.stub(vscode.window, "showInformationMessage").returns( - new Promise((resolve) => { - return resolve({ title: "Yes" }); - }) - ); + showInformationMessage = sinon + .stub(vscode.window, "showInformationMessage") + .returns( + new Promise((resolve) => { + return resolve({ title: "Yes" }); + }) + ); }); teardown(function () { @@ -30,4 +34,16 @@ suite("Extension Test Suite", () => { let result = await myExtension.ensureNextLSDownloaded("test-bin"); assert.equal(path.normalize(result), path.normalize("test-bin/nextls")); }); + + test("uninstalls Next LS", async function () { + fs.mkdirSync("./test-bin", { recursive: true }); + fs.writeFileSync("./test-bin/nextls", "hello word"); + + await uninstall.run("./test-bin"); + + assert.equal( + showInformationMessage.getCall(0).args[0], + `Uninstalled Next LS from ${path.normalize("test-bin/nextls")}` + ); + }); });