From d6c2339205c59a83f09572c11a425fb5f2eba8ad Mon Sep 17 00:00:00 2001 From: laur Date: Wed, 31 May 2023 17:44:44 +0200 Subject: [PATCH] fix LSP update logic - LSP update logic had couple of problems: a) it prompted user for version update before verifying whether it's even needed; b) if update failed or user opted out, then module returned undefined instead of the existing binary path Fixes NoahTheDuke/coc-clojure#11 --- src/download/download.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/download/download.ts b/src/download/download.ts index 4abfc0d..b965e5c 100644 --- a/src/download/download.ts +++ b/src/download/download.ts @@ -52,11 +52,10 @@ async function fetchFromUrl(fullUrl: string): Promise { export async function getLatestVersion(): Promise { try { - const releasesJSON = await fetchFromUrl( - "https://api.github.com/repos/clojure-lsp/clojure-lsp/releases" + const latestReleaseRaw = await fetchFromUrl( + "https://api.github.com/repos/clojure-lsp/clojure-lsp/releases/latest" ); - const releases = JSON.parse(releasesJSON); - return releases[0].tag_name; + return JSON.parse(latestReleaseRaw).tag_name; } catch (e: any) { logger.error("Error while finding latest clojure-lsp version.", e.message); return ""; @@ -195,17 +194,18 @@ async function maybeDownloadClojureLsp( extensionPath: string, msg: string ): Promise { - const choice = await window.showQuickpick( - ["Yes", "No"], - `clojure-lsp is ${msg}. Download from Github?` - ); - if (choice == 0) { - const { lspVersion, lspInstallPath } = config(); - const currentVersion = readVersionFile(extensionPath); - const downloadVersion = ["", "latest"].includes(lspVersion) - ? await getLatestVersion() - : lspVersion; - if (currentVersion !== downloadVersion && downloadVersion !== "") { + const { lspVersion, lspInstallPath } = config(); + const currentVersion = readVersionFile(extensionPath); + const downloadVersion = ["", "latest"].includes(lspVersion) + ? await getLatestVersion() + : lspVersion; + + if (currentVersion !== downloadVersion && downloadVersion !== "") { + const choice = await window.showQuickpick( + ["Yes", "No"], + `clojure-lsp is ${msg}. Download from Github?` + ); + if (choice == 0) { const path = lspInstallPath || extensionPath; const bin = await downloadClojureLsp(path, downloadVersion); logger.info(`Successfully downloaded clojure-lsp to ${bin}`); @@ -230,7 +230,7 @@ export async function findOrDownloadClojureLsp( const defaultBin = getClojureLspPath(extensionPath); // Is the bin installed at the default location so we can update it? if (config().checkOnStart && bin === defaultBin) { - bin = await maybeDownloadClojureLsp(extensionPath, "outdated"); + bin = await maybeDownloadClojureLsp(extensionPath, "outdated") || bin; } } else { bin = await maybeDownloadClojureLsp(extensionPath, "not found");