|
| 1 | +/** |
| 2 | + * Checks for updates to a given npm package and notifies the user if an update is available. |
| 3 | + */ |
| 4 | + |
| 5 | +import * as os from 'os'; |
| 6 | +import * as path from 'path'; |
| 7 | +import * as semver from 'semver'; |
| 8 | +import { OutputHelper } from '@quenty/cli-output-helpers'; |
| 9 | +import { readFile, writeFile } from 'fs/promises'; |
| 10 | + |
| 11 | +const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24 hours |
| 12 | + |
| 13 | +interface VersionCache { |
| 14 | + lastCheck: number; |
| 15 | + latestVersion: string; |
| 16 | + currentVersion: string; |
| 17 | +} |
| 18 | + |
| 19 | +interface UpdateCheckResult { |
| 20 | + updateAvailable: boolean; |
| 21 | + currentVersion: string; |
| 22 | + latestVersion: string; |
| 23 | +} |
| 24 | + |
| 25 | +interface VersionCheckerOptions { |
| 26 | + packageName: string; |
| 27 | + humanReadableName?: string; |
| 28 | + registryUrl: string; |
| 29 | + currentVersion?: string; |
| 30 | + packageJsonPath?: string; |
| 31 | + updateCommand?: string; |
| 32 | + verbose?: boolean; |
| 33 | +} |
| 34 | + |
| 35 | +export async function checkForUpdatesAsync( |
| 36 | + options: VersionCheckerOptions |
| 37 | +): Promise<void> { |
| 38 | + try { |
| 39 | + await checkForUpdatesInternalAsync(options); |
| 40 | + } catch (error) { |
| 41 | + const name = options.humanReadableName || options.packageName; |
| 42 | + OutputHelper.box(`Failed to check for updates for ${name} due to ${error}`); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +async function checkForUpdatesInternalAsync( |
| 47 | + options: VersionCheckerOptions |
| 48 | +): Promise<void> { |
| 49 | + const { |
| 50 | + packageName, |
| 51 | + registryUrl, |
| 52 | + currentVersion, |
| 53 | + packageJsonPath, |
| 54 | + updateCommand = `npm install -g ${packageName}@latest`, |
| 55 | + } = options; |
| 56 | + |
| 57 | + const version = await queryOurVersionAsync(currentVersion, packageJsonPath); |
| 58 | + if (!version) { |
| 59 | + if (options.verbose) { |
| 60 | + OutputHelper.error( |
| 61 | + `Could not determine current version for ${packageName}, skipping update check.` |
| 62 | + ); |
| 63 | + } |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const result = await queryUpdateStateAsync(packageName, version, registryUrl); |
| 68 | + |
| 69 | + if (options.verbose) { |
| 70 | + OutputHelper.info( |
| 71 | + `Checked for updates for ${packageName}. Current version: ${result.currentVersion}, Latest version: ${result.latestVersion}, and update available: ${result.updateAvailable}` |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + if (result.updateAvailable) { |
| 76 | + const name = options.humanReadableName || packageName; |
| 77 | + const text = [ |
| 78 | + `${name} update available: ${result.currentVersion} → ${result.latestVersion}`, |
| 79 | + '', |
| 80 | + OutputHelper.formatHint(`Run '${updateCommand}' to update`), |
| 81 | + ].join('\n'); |
| 82 | + |
| 83 | + OutputHelper.box(text, { centered: true }); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +async function queryOurVersionAsync( |
| 88 | + currentVersion: string | undefined, |
| 89 | + packageJsonPath: string | undefined |
| 90 | +): Promise<string | null> { |
| 91 | + if (currentVersion) { |
| 92 | + return currentVersion; |
| 93 | + } |
| 94 | + |
| 95 | + if (!packageJsonPath) { |
| 96 | + throw new Error( |
| 97 | + 'Either currentVersion or packageJsonPath must be provided to determine the current version.' |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const pkg = JSON.parse(await readFile(packageJsonPath, 'utf8')); |
| 102 | + return pkg.version || null; |
| 103 | +} |
| 104 | + |
| 105 | +async function queryUpdateStateAsync( |
| 106 | + packageName: string, |
| 107 | + currentVersion: string, |
| 108 | + registryUrl: string |
| 109 | +): Promise<UpdateCheckResult> { |
| 110 | + // Use a simple cache file in the user's home directory |
| 111 | + const cacheKey = `${packageName.replace('/', '-').replace('@', '')}-version`; |
| 112 | + const cacheFile = path.join(os.homedir(), '.nevermore-version-cache'); |
| 113 | + |
| 114 | + // Try to read cached data |
| 115 | + let cachedData: VersionCache | undefined; |
| 116 | + let loadedCacheData; |
| 117 | + try { |
| 118 | + const cacheContent = await readFile(cacheFile, 'utf-8'); |
| 119 | + loadedCacheData = JSON.parse(cacheContent); |
| 120 | + cachedData = loadedCacheData[cacheKey] as VersionCache | undefined; |
| 121 | + } catch (error) { |
| 122 | + // Cache file doesn't exist or is invalid, will check for updates |
| 123 | + } |
| 124 | + |
| 125 | + // If we checked recently, skip |
| 126 | + const now = Date.now(); |
| 127 | + if ( |
| 128 | + cachedData && |
| 129 | + (now - cachedData.lastCheck < CHECK_INTERVAL_MS || |
| 130 | + cachedData.currentVersion !== currentVersion) |
| 131 | + ) { |
| 132 | + return { |
| 133 | + updateAvailable: semver.gt(cachedData.latestVersion, currentVersion), |
| 134 | + currentVersion: currentVersion, |
| 135 | + latestVersion: cachedData.latestVersion, |
| 136 | + }; |
| 137 | + } |
| 138 | + |
| 139 | + const { default: latestVersion } = await import('latest-version'); |
| 140 | + |
| 141 | + // Check for new version |
| 142 | + const latestVersionString = await latestVersion(packageName, { |
| 143 | + registryUrl: registryUrl, |
| 144 | + }); |
| 145 | + |
| 146 | + // Save to cache |
| 147 | + const newCache: VersionCache = { |
| 148 | + lastCheck: now, |
| 149 | + latestVersion: latestVersionString, |
| 150 | + currentVersion: currentVersion, |
| 151 | + }; |
| 152 | + const newResults = loadedCacheData || {}; |
| 153 | + newResults[cacheKey] = newCache; |
| 154 | + |
| 155 | + try { |
| 156 | + await writeFile(cacheFile, JSON.stringify(newResults, null, 2), 'utf-8'); |
| 157 | + } catch (error) { |
| 158 | + // Ignore cache write errors, update check still worked |
| 159 | + OutputHelper.warn(`Failed to write cache file: ${error}`); |
| 160 | + } |
| 161 | + |
| 162 | + // Return whether update is available |
| 163 | + return { |
| 164 | + updateAvailable: semver.gt(latestVersionString, currentVersion), |
| 165 | + currentVersion: currentVersion, |
| 166 | + latestVersion: latestVersionString, |
| 167 | + }; |
| 168 | +} |
0 commit comments