From ce89ca7f1449673a6fa28900561dd99d988bdac1 Mon Sep 17 00:00:00 2001 From: Gleison de Almeida Date: Fri, 3 Jun 2022 01:48:38 -0300 Subject: [PATCH] :bug: fix: check of installed cli version --- src/enums/message.enum.ts | 1 - src/functions/cli.function.ts | 23 ++++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/enums/message.enum.ts b/src/enums/message.enum.ts index d207272..9623257 100644 --- a/src/enums/message.enum.ts +++ b/src/enums/message.enum.ts @@ -1,6 +1,5 @@ export enum eMessage { CLI_INSTALLED = "@ngx-devs/cli installed successfully", - CLI_OUTDATED = "@ngx-devs/cli is outdated do you want to update?", CLI_REQUIRED = "@ngx-devs/cli is required to run this extension", SHOULD_INSTALL_CLI = "@ngx-devs/cli is not installed do you want to install?", EXTENSION_ACTIVATED = 'Congratulations, your extension "ngxd-console" is now active!', diff --git a/src/functions/cli.function.ts b/src/functions/cli.function.ts index 1eddfca..4b9d5d6 100644 --- a/src/functions/cli.function.ts +++ b/src/functions/cli.function.ts @@ -6,27 +6,36 @@ import { execShell } from './exec-shell.function'; export const getInstalledVersion = async () => { try { - const result = await execShell("ngxd --version"); - const matches = result.match(/(\d\.\d\.\d)/); - if (!matches) return ""; - const version = matches[0]; - return version; + const globalPackages = await execShell("npm list -g"); + const cliVersion = globalPackages + .split("\n") + .find((line) => line.includes(CLI_PACKAGE_NAME)) + ?.split("@ngx-devs/cli@") + .reverse()[0]; + + return cliVersion; } catch (e) { - return ""; + console.error(e); } }; export const installCliIfNotInstalled = async () => { const cliInstalledVersion = await getInstalledVersion(); + if (!cliInstalledVersion) return installCLI(eMessage.SHOULD_INSTALL_CLI); const manager = new PluginManager(); const ngxdCliLatestVersion = (await manager.queryPackageFromNpm(CLI_PACKAGE_NAME)).version; + console.info(`ngxd CLI installed version: ${cliInstalledVersion}`); + console.info(`ngxd CLI latest version: ${ngxdCliLatestVersion}`); + const isCLIVersionUpToDate = cliInstalledVersion === ngxdCliLatestVersion; if (isCLIVersionUpToDate) return; - await installCLI(eMessage.CLI_OUTDATED); + await installCLI( + `Your CLI version is out of date. Do you want to update version from ${cliInstalledVersion} to ${ngxdCliLatestVersion}?` + ); }; async function installCLI(message: string) {