Skip to content

Commit b3b3223

Browse files
refactor: move version print to own service module (#492)
Signed-off-by: David Dal Busco <[email protected]>
1 parent a201ad2 commit b3b3223

File tree

3 files changed

+78
-77
lines changed

3 files changed

+78
-77
lines changed

src/commands/version.ts

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,5 @@
1-
import {isEmptyString} from '@dfinity/utils';
2-
import {green} from 'kleur';
3-
import {version as cliCurrentVersion} from '../../package.json';
4-
import {githubCliLastRelease, githubJunoDockerLastRelease} from '../rest/github.rest';
5-
import {findEmulatorVersion} from '../services/emulator/version.services';
6-
import {
7-
buildVersionFromGitHub,
8-
checkVersion,
9-
type CheckVersionResult
10-
} from '../services/version/version.services';
11-
import {pmInstallHint} from '../utils/pm.utils';
1+
import {printVersion} from '../services/version/version.print.services';
122

13-
export const version = async () => {
14-
const check = await cliVersion();
15-
16-
if (check.diff === 'error') {
17-
return;
18-
}
19-
20-
await emulatorVersion();
21-
};
22-
23-
const cliVersion = async (): Promise<CheckVersionResult> => {
24-
const result = await buildVersionFromGitHub({
25-
logReleaseOnError: () => 'CLI',
26-
releaseFn: githubCliLastRelease
27-
});
28-
29-
if (result.result === 'error') {
30-
return {diff: 'error'};
31-
}
32-
33-
const {latestVersion} = result;
34-
35-
return checkVersion({
36-
currentVersion: cliCurrentVersion,
37-
latestVersion,
38-
displayHint: 'CLI',
39-
commandLineHint: pmInstallHint()
40-
});
41-
};
42-
43-
const emulatorVersion = async () => {
44-
const emulatorResult = await findEmulatorVersion();
45-
46-
if (emulatorResult.status !== 'success') {
47-
return;
48-
}
49-
50-
const {version: emulatorCurrentVersion} = emulatorResult;
51-
52-
const result = await buildVersionFromGitHub({
53-
logReleaseOnError: () => 'Juno Docker',
54-
releaseFn: githubJunoDockerLastRelease
55-
});
56-
57-
if (result.result === 'error') {
58-
return;
59-
}
60-
61-
const {latestVersion} = result;
62-
63-
// Images prior to v0.6.3 lacked proper metadata in org.opencontainers.image.version.
64-
// Earlier releases contained invalid values such as "0-arm64", while v0.6.2 returned an empty string.
65-
// Note: sanitizing the version read via docker/podman inspect causes these cases to resolve to null.
66-
if (isEmptyString(emulatorCurrentVersion)) {
67-
console.log(`Your Emulator is behind the latest version (${green(`v${latestVersion}`)}).`);
68-
return;
69-
}
70-
71-
checkVersion({
72-
currentVersion: emulatorCurrentVersion,
73-
latestVersion,
74-
displayHint: 'Emulator'
75-
});
3+
export const logVersion = async () => {
4+
await printVersion();
765
};

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {helpSnapshot, snapshot} from './commands/snapshot';
1818
import {startStop} from './commands/start-stop';
1919
import {status} from './commands/status';
2020
import {upgrade} from './commands/upgrade';
21-
import {version as versionCommand} from './commands/version';
21+
import {logVersion} from './commands/version';
2222
import {whoami} from './commands/whoami';
2323
import {help} from './help/help';
2424
import {logHelpLogin} from './help/login.help';
@@ -55,7 +55,7 @@ export const run = async () => {
5555

5656
// Special use case if dev runs "juno --version"
5757
if (['-v', '--version'].includes(cmd)) {
58-
await versionCommand();
58+
await logVersion();
5959
return;
6060
}
6161

@@ -149,7 +149,7 @@ export const run = async () => {
149149
await clear();
150150
break;
151151
case 'version':
152-
await versionCommand();
152+
await logVersion();
153153
break;
154154
case 'status':
155155
await status();
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {isEmptyString} from '@dfinity/utils';
2+
import {green} from 'kleur';
3+
import {version as cliCurrentVersion} from '../../../package.json';
4+
import {githubCliLastRelease, githubJunoDockerLastRelease} from '../../rest/github.rest';
5+
import {pmInstallHint} from '../../utils/pm.utils';
6+
import {findEmulatorVersion} from '../emulator/version.services';
7+
import {buildVersionFromGitHub, checkVersion, type CheckVersionResult} from './version.services';
8+
9+
export const printVersion = async () => {
10+
const check = await cliVersion();
11+
12+
if (check.diff === 'error') {
13+
return;
14+
}
15+
16+
await emulatorVersion();
17+
};
18+
19+
const cliVersion = async (): Promise<CheckVersionResult> => {
20+
const result = await buildVersionFromGitHub({
21+
logReleaseOnError: () => 'CLI',
22+
releaseFn: githubCliLastRelease
23+
});
24+
25+
if (result.result === 'error') {
26+
return {diff: 'error'};
27+
}
28+
29+
const {latestVersion} = result;
30+
31+
return checkVersion({
32+
currentVersion: cliCurrentVersion,
33+
latestVersion,
34+
displayHint: 'CLI',
35+
commandLineHint: pmInstallHint()
36+
});
37+
};
38+
39+
const emulatorVersion = async () => {
40+
const emulatorResult = await findEmulatorVersion();
41+
42+
if (emulatorResult.status !== 'success') {
43+
return;
44+
}
45+
46+
const {version: emulatorCurrentVersion} = emulatorResult;
47+
48+
const result = await buildVersionFromGitHub({
49+
logReleaseOnError: () => 'Juno Docker',
50+
releaseFn: githubJunoDockerLastRelease
51+
});
52+
53+
if (result.result === 'error') {
54+
return;
55+
}
56+
57+
const {latestVersion} = result;
58+
59+
// Images prior to v0.6.3 lacked proper metadata in org.opencontainers.image.version.
60+
// Earlier releases contained invalid values such as "0-arm64", while v0.6.2 returned an empty string.
61+
// Note: sanitizing the version read via docker/podman inspect causes these cases to resolve to null.
62+
if (isEmptyString(emulatorCurrentVersion)) {
63+
console.log(`Your Emulator is behind the latest version (${green(`v${latestVersion}`)}).`);
64+
return;
65+
}
66+
67+
checkVersion({
68+
currentVersion: emulatorCurrentVersion,
69+
latestVersion,
70+
displayHint: 'Emulator'
71+
});
72+
};

0 commit comments

Comments
 (0)