()
| 40 | } |
| 41 | |
| 42 | export async function checkForUpdates(): Promise<UpdateObject | null> { |
| 43 | try { |
| 44 | // Skip update check when running from source (development mode) |
| 45 | if (process.env['DEV'] === 'true') { |
| 46 | return null; |
| 47 | } |
| 48 | const packageJson = await getPackageJson(); |
| 49 | if (!packageJson || !packageJson.name || !packageJson.version) { |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | const { name, version: currentVersion } = packageJson; |
| 54 | const isNightly = currentVersion.includes('nightly'); |
| 55 | const createNotifier = (distTag: 'latest' | 'nightly') => |
| 56 | updateNotifier({ |
| 57 | pkg: { |
| 58 | name, |
| 59 | version: currentVersion, |
| 60 | }, |
| 61 | updateCheckInterval: 0, |
| 62 | shouldNotifyInNpmScript: true, |
| 63 | distTag, |
| 64 | }); |
| 65 | |
| 66 | if (isNightly) { |
| 67 | const [nightlyUpdateInfo, latestUpdateInfo] = await Promise.all([ |
| 68 | createNotifier('nightly').fetchInfo(), |
| 69 | createNotifier('latest').fetchInfo(), |
| 70 | ]); |
| 71 | |
| 72 | const bestUpdate = getBestAvailableUpdate( |
| 73 | nightlyUpdateInfo, |
| 74 | latestUpdateInfo, |
| 75 | ); |
| 76 | |
| 77 | if (bestUpdate && semver.gt(bestUpdate.latest, currentVersion)) { |
| 78 | const message = `A new version of ANUS is available! ${currentVersion} → ${bestUpdate.latest}`; |
| 79 | return { |
| 80 | message, |
| 81 | update: { ...bestUpdate, current: currentVersion }, |
| 82 | }; |
| 83 | } |
| 84 | } else { |
| 85 | const updateInfo = await createNotifier('latest').fetchInfo(); |
| 86 | |
| 87 | if (updateInfo && semver.gt(updateInfo.latest, currentVersion)) { |
| 88 | const message = `ANUS update available! ${currentVersion} → ${updateInfo.latest}`; |
| 89 | return { |
| 90 | message, |
| 91 | update: { ...updateInfo, current: currentVersion }, |
| 92 | }; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return null; |
| 97 | } catch (e) { |
| 98 | console.warn('Failed to check for updates: ' + e); |
| 99 | return null; |
no test coverage detected