|
| 1 | +import {isNullish, nonNullish} from '@dfinity/utils'; |
| 2 | +import ora from 'ora'; |
| 3 | +import {compare} from 'semver'; |
| 4 | +import {version as cliCurrentVersion} from '../../../package.json'; |
| 5 | +import { |
| 6 | + getCachedVersions, |
| 7 | + saveCachedVersions, |
| 8 | + updateLastCheckToNow |
| 9 | +} from '../../configs/cli.versions.config'; |
| 10 | +import { |
| 11 | + githubCliLastRelease, |
| 12 | + githubJunoDockerLastRelease, |
| 13 | + type GithubLastReleaseResult |
| 14 | +} from '../../rest/github.rest'; |
| 15 | +import {type CachedVersions} from '../../types/cli/cli.versions'; |
| 16 | +import {pmInstallHint} from '../../utils/pm.utils'; |
| 17 | +import {findEmulatorVersion} from '../emulator/version.services'; |
| 18 | +import { |
| 19 | + buildVersionFromGitHub, |
| 20 | + type BuildVersionFromGitHubResult, |
| 21 | + checkVersion |
| 22 | +} from './version.services'; |
| 23 | + |
| 24 | +const ONE_WEEK_MS = 7 * 24 * 60 * 60 * 1000; |
| 25 | + |
| 26 | +export const checkCliVersion = async () => { |
| 27 | + const checkVersionFn = ({latestVersion}: {latestVersion: string}) => { |
| 28 | + checkVersion({ |
| 29 | + currentVersion: cliCurrentVersion, |
| 30 | + latestVersion, |
| 31 | + displayHint: 'CLI', |
| 32 | + commandLineHint: pmInstallHint(), |
| 33 | + logUpToDate: false, |
| 34 | + logSpacer: true |
| 35 | + }); |
| 36 | + }; |
| 37 | + |
| 38 | + await check({ |
| 39 | + key: 'cli', |
| 40 | + currentVersion: cliCurrentVersion, |
| 41 | + releaseFn: githubCliLastRelease, |
| 42 | + checkVersionFn |
| 43 | + }); |
| 44 | +}; |
| 45 | + |
| 46 | +export const checkEmulatorVersion = async () => { |
| 47 | + const emulatorResult = await findEmulatorVersion(); |
| 48 | + |
| 49 | + if (emulatorResult.status !== 'success') { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const {version: emulatorCurrentVersion} = emulatorResult; |
| 54 | + |
| 55 | + // We fetched the emulator but the version is null which could happen has providing the metadata |
| 56 | + // was patched in Juno Docker v0.6.3 |
| 57 | + if (isNullish(emulatorCurrentVersion)) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const checkVersionFn = ({latestVersion}: {latestVersion: string}) => { |
| 62 | + checkVersion({ |
| 63 | + currentVersion: emulatorCurrentVersion, |
| 64 | + latestVersion, |
| 65 | + displayHint: 'Emulator', |
| 66 | + logUpToDate: false, |
| 67 | + logSpacer: true |
| 68 | + }); |
| 69 | + }; |
| 70 | + |
| 71 | + await check({ |
| 72 | + key: 'emulator', |
| 73 | + currentVersion: emulatorCurrentVersion, |
| 74 | + releaseFn: githubJunoDockerLastRelease, |
| 75 | + checkVersionFn |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +const check = async ({ |
| 80 | + key, |
| 81 | + currentVersion, |
| 82 | + releaseFn, |
| 83 | + checkVersionFn |
| 84 | +}: { |
| 85 | + key: keyof CachedVersions; |
| 86 | + currentVersion: string; |
| 87 | + releaseFn: () => Promise<GithubLastReleaseResult>; |
| 88 | + checkVersionFn: (params: {latestVersion: string}) => void; |
| 89 | +}) => { |
| 90 | + const cachedVersions = getCachedVersions(); |
| 91 | + |
| 92 | + const cachedInfo = cachedVersions.get(key); |
| 93 | + |
| 94 | + const lastCheck = cachedInfo?.lastCheck; |
| 95 | + |
| 96 | + if (isNullish(lastCheck)) { |
| 97 | + saveCachedVersions({ |
| 98 | + key, |
| 99 | + versions: { |
| 100 | + local: currentVersion |
| 101 | + } |
| 102 | + }); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const cachedLocalVersion = cachedInfo?.local; |
| 107 | + |
| 108 | + // The version was never cached or the developer upgraded since the last check. |
| 109 | + // We assume they are on the latest version. If not, the next weekly check will catch it. |
| 110 | + if (isNullish(cachedLocalVersion) || compare(currentVersion, cachedLocalVersion) > 0) { |
| 111 | + saveCachedVersions({ |
| 112 | + key, |
| 113 | + versions: { |
| 114 | + local: currentVersion |
| 115 | + } |
| 116 | + }); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + const checkIsDue = |
| 121 | + new Date(new Date(lastCheck).getTime() + ONE_WEEK_MS).getTime() <= new Date().getTime(); |
| 122 | + |
| 123 | + const cachedRemoteVersion = cachedInfo?.remote; |
| 124 | + |
| 125 | + // The weekly check is not due and we got a remote version in cache |
| 126 | + if (!checkIsDue && nonNullish(cachedRemoteVersion)) { |
| 127 | + // The current version is newer or equals, we assume the dev use the latest |
| 128 | + if (compare(currentVersion, cachedRemoteVersion) >= 0) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + // Behind but check not due, compare cached remote version |
| 133 | + checkVersionFn({latestVersion: cachedRemoteVersion}); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + const loadVersionWithGitHub = async (): Promise<BuildVersionFromGitHubResult> => { |
| 138 | + const spinner = ora(`Two secs, fetching ${key} latest version...`).start(); |
| 139 | + |
| 140 | + try { |
| 141 | + return await buildVersionFromGitHub({ |
| 142 | + releaseFn |
| 143 | + }); |
| 144 | + } finally { |
| 145 | + spinner.stop(); |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + const result = await loadVersionWithGitHub(); |
| 150 | + |
| 151 | + if (result.result === 'error') { |
| 152 | + updateLastCheckToNow({key}); |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + const {latestVersion} = result; |
| 157 | + |
| 158 | + saveCachedVersions({ |
| 159 | + key, |
| 160 | + versions: { |
| 161 | + local: currentVersion, |
| 162 | + remote: latestVersion |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + checkVersionFn({latestVersion}); |
| 167 | +}; |
0 commit comments