1- import { isNullish } from '@dfinity/utils' ;
2- import { red } from 'kleur' ;
1+ import { isEmptyString , isNullish } from '@dfinity/utils' ;
2+ import { green , red } from 'kleur' ;
33import { clean } from 'semver' ;
44import { version as cliCurrentVersion } from '../../package.json' ;
5- import { githubCliLastRelease } from '../rest/github.rest' ;
6- import { checkVersion } from '../services/version.services' ;
5+ import {
6+ githubCliLastRelease ,
7+ githubJunoDockerLastRelease ,
8+ type GithubLastReleaseResult
9+ } from '../rest/github.rest' ;
10+ import { findEmulatorVersion } from '../services/emulator/version.services' ;
11+ import { checkVersion , type CheckVersionResult } from '../services/version.services' ;
712import { detectPackageManager } from '../utils/pm.utils' ;
8- export const version = async ( ) => {
9- await cliVersion ( ) ;
10- } ;
1113
12- const cliVersion = async ( ) => {
13- const githubRelease = await githubCliLastRelease ( ) ;
14+ export const version = async ( ) => {
15+ const check = await cliVersion ( ) ;
1416
15- if ( githubRelease === undefined ) {
16- console . log ( red ( 'Cannot fetch last release version of Juno on GitHub 😢.' ) ) ;
17+ if ( check . diff === 'error' ) {
1718 return ;
1819 }
1920
20- const { tag_name} = githubRelease ;
21+ await emulatorVersion ( ) ;
22+ } ;
2123
22- const latestVersion = clean ( tag_name ) ;
24+ const cliVersion = async ( ) : Promise < CheckVersionResult > => {
25+ const result = await buildVersionFromGitHub ( {
26+ release : 'CLI' ,
27+ releaseFn : githubCliLastRelease
28+ } ) ;
2329
24- if ( isNullish ( latestVersion ) ) {
25- console . log ( red ( `Cannot extract version from release. Reach out Juno❗️` ) ) ;
26- return ;
30+ if ( result . result === 'error' ) {
31+ return { diff : 'error' } ;
2732 }
2833
29- checkVersion ( {
34+ const { latestVersion} = result ;
35+
36+ return checkVersion ( {
3037 currentVersion : cliCurrentVersion ,
3138 latestVersion,
3239 displayHint : 'CLI' ,
@@ -46,3 +53,66 @@ const installHint = (): string => {
4653 return 'npm i -g @junobuild/cli' ;
4754 }
4855} ;
56+
57+ const emulatorVersion = async ( ) => {
58+ const emulatorResult = await findEmulatorVersion ( ) ;
59+
60+ if ( emulatorResult . status !== 'success' ) {
61+ return ;
62+ }
63+
64+ const { version : emulatorCurrentVersion } = emulatorResult ;
65+
66+ const result = await buildVersionFromGitHub ( {
67+ release : 'Juno Docker' ,
68+ releaseFn : githubJunoDockerLastRelease
69+ } ) ;
70+
71+ if ( result . result === 'error' ) {
72+ return ;
73+ }
74+
75+ const { latestVersion} = result ;
76+
77+ // Images prior to v0.6.3 lacked proper metadata in org.opencontainers.image.version.
78+ // Earlier releases contained invalid values such as "0-arm64", while v0.6.2 returned an empty string.
79+ // Note: sanitizing the version read via docker/podman inspect causes these cases to resolve to null.
80+ if ( isEmptyString ( emulatorCurrentVersion ) ) {
81+ console . log ( `Your Emulator is behind the latest version (${ green ( `v${ latestVersion } ` ) } ).` ) ;
82+ return ;
83+ }
84+
85+ checkVersion ( {
86+ currentVersion : emulatorCurrentVersion ,
87+ latestVersion,
88+ displayHint : 'Emulator'
89+ } ) ;
90+ } ;
91+
92+ const buildVersionFromGitHub = async ( {
93+ releaseFn,
94+ release
95+ } : {
96+ releaseFn : ( ) => Promise < GithubLastReleaseResult > ;
97+ release : 'CLI' | 'Juno Docker' ;
98+ } ) : Promise < { result : 'success' ; latestVersion : string } | { result : 'error' } > => {
99+ const githubRelease = await releaseFn ( ) ;
100+
101+ if ( githubRelease . status === 'error' ) {
102+ console . log ( red ( `Cannot fetch the last version of ${ release } on GitHub 😢.` ) ) ;
103+ return { result : 'error' } ;
104+ }
105+
106+ const {
107+ release : { tag_name}
108+ } = githubRelease ;
109+
110+ const latestVersion = clean ( tag_name ) ;
111+
112+ if ( isNullish ( latestVersion ) ) {
113+ console . log ( red ( `Cannot extract version from the ${ release } release. Reach out Juno❗️` ) ) ;
114+ return { result : 'error' } ;
115+ }
116+
117+ return { result : 'success' , latestVersion} ;
118+ } ;
0 commit comments