Skip to content

Commit

Permalink
🐛 fix: check of installed cli version
Browse files Browse the repository at this point in the history
  • Loading branch information
gleisonkz committed Jun 3, 2022
1 parent 2e6e324 commit ce89ca7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/enums/message.enum.ts
Original file line number Diff line number Diff line change
@@ -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!',
Expand Down
23 changes: 16 additions & 7 deletions src/functions/cli.function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ce89ca7

Please sign in to comment.