From f43a699f7b5c2ac61f889b31ea4b960333e6a90d Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Mon, 16 Mar 2026 21:30:04 +0100 Subject: [PATCH] refactor: move version print to own service module Signed-off-by: David Dal Busco --- src/commands/version.ts | 77 +------------------ src/index.ts | 6 +- .../version/version.print.services.ts | 72 +++++++++++++++++ 3 files changed, 78 insertions(+), 77 deletions(-) create mode 100644 src/services/version/version.print.services.ts diff --git a/src/commands/version.ts b/src/commands/version.ts index f73e0c1..19ff285 100644 --- a/src/commands/version.ts +++ b/src/commands/version.ts @@ -1,76 +1,5 @@ -import {isEmptyString} from '@dfinity/utils'; -import {green} from 'kleur'; -import {version as cliCurrentVersion} from '../../package.json'; -import {githubCliLastRelease, githubJunoDockerLastRelease} from '../rest/github.rest'; -import {findEmulatorVersion} from '../services/emulator/version.services'; -import { - buildVersionFromGitHub, - checkVersion, - type CheckVersionResult -} from '../services/version/version.services'; -import {pmInstallHint} from '../utils/pm.utils'; +import {printVersion} from '../services/version/version.print.services'; -export const version = async () => { - const check = await cliVersion(); - - if (check.diff === 'error') { - return; - } - - await emulatorVersion(); -}; - -const cliVersion = async (): Promise => { - const result = await buildVersionFromGitHub({ - logReleaseOnError: () => 'CLI', - releaseFn: githubCliLastRelease - }); - - if (result.result === 'error') { - return {diff: 'error'}; - } - - const {latestVersion} = result; - - return checkVersion({ - currentVersion: cliCurrentVersion, - latestVersion, - displayHint: 'CLI', - commandLineHint: pmInstallHint() - }); -}; - -const emulatorVersion = async () => { - const emulatorResult = await findEmulatorVersion(); - - if (emulatorResult.status !== 'success') { - return; - } - - const {version: emulatorCurrentVersion} = emulatorResult; - - const result = await buildVersionFromGitHub({ - logReleaseOnError: () => 'Juno Docker', - releaseFn: githubJunoDockerLastRelease - }); - - if (result.result === 'error') { - return; - } - - const {latestVersion} = result; - - // Images prior to v0.6.3 lacked proper metadata in org.opencontainers.image.version. - // Earlier releases contained invalid values such as "0-arm64", while v0.6.2 returned an empty string. - // Note: sanitizing the version read via docker/podman inspect causes these cases to resolve to null. - if (isEmptyString(emulatorCurrentVersion)) { - console.log(`Your Emulator is behind the latest version (${green(`v${latestVersion}`)}).`); - return; - } - - checkVersion({ - currentVersion: emulatorCurrentVersion, - latestVersion, - displayHint: 'Emulator' - }); +export const logVersion = async () => { + await printVersion(); }; diff --git a/src/index.ts b/src/index.ts index c3d9de7..98f847a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,7 +18,7 @@ import {helpSnapshot, snapshot} from './commands/snapshot'; import {startStop} from './commands/start-stop'; import {status} from './commands/status'; import {upgrade} from './commands/upgrade'; -import {version as versionCommand} from './commands/version'; +import {logVersion} from './commands/version'; import {whoami} from './commands/whoami'; import {help} from './help/help'; import {logHelpLogin} from './help/login.help'; @@ -55,7 +55,7 @@ export const run = async () => { // Special use case if dev runs "juno --version" if (['-v', '--version'].includes(cmd)) { - await versionCommand(); + await logVersion(); return; } @@ -149,7 +149,7 @@ export const run = async () => { await clear(); break; case 'version': - await versionCommand(); + await logVersion(); break; case 'status': await status(); diff --git a/src/services/version/version.print.services.ts b/src/services/version/version.print.services.ts new file mode 100644 index 0000000..9b70e2f --- /dev/null +++ b/src/services/version/version.print.services.ts @@ -0,0 +1,72 @@ +import {isEmptyString} from '@dfinity/utils'; +import {green} from 'kleur'; +import {version as cliCurrentVersion} from '../../../package.json'; +import {githubCliLastRelease, githubJunoDockerLastRelease} from '../../rest/github.rest'; +import {pmInstallHint} from '../../utils/pm.utils'; +import {findEmulatorVersion} from '../emulator/version.services'; +import {buildVersionFromGitHub, checkVersion, type CheckVersionResult} from './version.services'; + +export const printVersion = async () => { + const check = await cliVersion(); + + if (check.diff === 'error') { + return; + } + + await emulatorVersion(); +}; + +const cliVersion = async (): Promise => { + const result = await buildVersionFromGitHub({ + logReleaseOnError: () => 'CLI', + releaseFn: githubCliLastRelease + }); + + if (result.result === 'error') { + return {diff: 'error'}; + } + + const {latestVersion} = result; + + return checkVersion({ + currentVersion: cliCurrentVersion, + latestVersion, + displayHint: 'CLI', + commandLineHint: pmInstallHint() + }); +}; + +const emulatorVersion = async () => { + const emulatorResult = await findEmulatorVersion(); + + if (emulatorResult.status !== 'success') { + return; + } + + const {version: emulatorCurrentVersion} = emulatorResult; + + const result = await buildVersionFromGitHub({ + logReleaseOnError: () => 'Juno Docker', + releaseFn: githubJunoDockerLastRelease + }); + + if (result.result === 'error') { + return; + } + + const {latestVersion} = result; + + // Images prior to v0.6.3 lacked proper metadata in org.opencontainers.image.version. + // Earlier releases contained invalid values such as "0-arm64", while v0.6.2 returned an empty string. + // Note: sanitizing the version read via docker/podman inspect causes these cases to resolve to null. + if (isEmptyString(emulatorCurrentVersion)) { + console.log(`Your Emulator is behind the latest version (${green(`v${latestVersion}`)}).`); + return; + } + + checkVersion({ + currentVersion: emulatorCurrentVersion, + latestVersion, + displayHint: 'Emulator' + }); +};