|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +declare const CLI_VERSION: string; |
| 6 | + |
| 7 | +const REPO = "ubie-oss/n8n-cli"; |
| 8 | +const LATEST_RELEASE_URL = `https://api.github.com/repos/${REPO}/releases/latest`; |
| 9 | +const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000; |
| 10 | +const FETCH_TIMEOUT_MS = 3000; |
| 11 | + |
| 12 | +interface CacheEntry { |
| 13 | + lastCheckedAt: string; |
| 14 | + latestVersion: string | null; |
| 15 | +} |
| 16 | + |
| 17 | +/** Resolve the cache file path in a platform-appropriate location. */ |
| 18 | +export function cacheFilePath( |
| 19 | + env: NodeJS.ProcessEnv = process.env, |
| 20 | + platform: NodeJS.Platform = process.platform, |
| 21 | + home: string = os.homedir(), |
| 22 | +): string { |
| 23 | + if (env.XDG_CACHE_HOME) { |
| 24 | + return path.join(env.XDG_CACHE_HOME, "n8n-cli", "update-check.json"); |
| 25 | + } |
| 26 | + switch (platform) { |
| 27 | + case "darwin": |
| 28 | + return path.join(home, "Library", "Caches", "n8n-cli", "update-check.json"); |
| 29 | + case "win32": |
| 30 | + return path.join( |
| 31 | + env.LOCALAPPDATA ?? path.join(home, "AppData", "Local"), |
| 32 | + "n8n-cli", |
| 33 | + "Cache", |
| 34 | + "update-check.json", |
| 35 | + ); |
| 36 | + default: |
| 37 | + return path.join(home, ".cache", "n8n-cli", "update-check.json"); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Compare two semver-ish versions. Returns 1 if a>b, -1 if a<b, 0 if equal. |
| 43 | + * Strips leading "v" and trailing "-dirty"/pre-release suffixes for comparison. |
| 44 | + */ |
| 45 | +export function compareVersions(a: string, b: string): number { |
| 46 | + const normalize = (v: string): number[] => |
| 47 | + v |
| 48 | + .replace(/^v/, "") |
| 49 | + .split("-")[0]! |
| 50 | + .split(".") |
| 51 | + .map((s) => Number.parseInt(s, 10)) |
| 52 | + .map((n) => (Number.isNaN(n) ? 0 : n)); |
| 53 | + |
| 54 | + const parsedA = normalize(a); |
| 55 | + const parsedB = normalize(b); |
| 56 | + const len = Math.max(parsedA.length, parsedB.length); |
| 57 | + for (let i = 0; i < len; i++) { |
| 58 | + const x = parsedA[i] ?? 0; |
| 59 | + const y = parsedB[i] ?? 0; |
| 60 | + if (x > y) return 1; |
| 61 | + if (x < y) return -1; |
| 62 | + } |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +function isCheckDisabled(): boolean { |
| 67 | + if (process.env.N8N_CLI_DISABLE_UPDATE_CHECK === "1") return true; |
| 68 | + if (process.env.CI === "true" || process.env.CI === "1") return true; |
| 69 | + return false; |
| 70 | +} |
| 71 | + |
| 72 | +function readCache(filePath: string): CacheEntry | null { |
| 73 | + try { |
| 74 | + const raw = fs.readFileSync(filePath, "utf8"); |
| 75 | + const parsed = JSON.parse(raw) as unknown; |
| 76 | + if ( |
| 77 | + typeof parsed === "object" && |
| 78 | + parsed !== null && |
| 79 | + typeof (parsed as CacheEntry).lastCheckedAt === "string" |
| 80 | + ) { |
| 81 | + return parsed as CacheEntry; |
| 82 | + } |
| 83 | + return null; |
| 84 | + } catch { |
| 85 | + return null; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function writeCache(filePath: string, entry: CacheEntry): void { |
| 90 | + try { |
| 91 | + fs.mkdirSync(path.dirname(filePath), { recursive: true }); |
| 92 | + fs.writeFileSync(filePath, JSON.stringify(entry, null, 2)); |
| 93 | + } catch { |
| 94 | + // ignore — cache write failures must not affect the CLI |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +async function fetchLatestVersion(): Promise<string | null> { |
| 99 | + const controller = new AbortController(); |
| 100 | + const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS); |
| 101 | + try { |
| 102 | + const res = await fetch(LATEST_RELEASE_URL, { |
| 103 | + headers: { |
| 104 | + Accept: "application/vnd.github+json", |
| 105 | + "User-Agent": "n8n-cli-update-check", |
| 106 | + }, |
| 107 | + signal: controller.signal, |
| 108 | + }); |
| 109 | + if (!res.ok) return null; |
| 110 | + const json = (await res.json()) as { tag_name?: unknown }; |
| 111 | + return typeof json.tag_name === "string" ? json.tag_name : null; |
| 112 | + } catch { |
| 113 | + return null; |
| 114 | + } finally { |
| 115 | + clearTimeout(timer); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function currentVersion(): string | null { |
| 120 | + const v = typeof CLI_VERSION !== "undefined" ? CLI_VERSION : "dev"; |
| 121 | + if (v === "dev" || v === "unknown" || v === "") return null; |
| 122 | + return v; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Kick off an update check. Returns a promise so the caller can await it |
| 127 | + * before showing the notice. Safe to fire-and-forget if the caller prefers. |
| 128 | + * Silent on all errors. |
| 129 | + */ |
| 130 | +export async function runUpdateCheck(): Promise<void> { |
| 131 | + if (isCheckDisabled()) return; |
| 132 | + if (currentVersion() === null) return; |
| 133 | + |
| 134 | + const file = cacheFilePath(); |
| 135 | + const cache = readCache(file); |
| 136 | + const now = Date.now(); |
| 137 | + if (cache) { |
| 138 | + const last = Date.parse(cache.lastCheckedAt); |
| 139 | + if (!Number.isNaN(last) && now - last < CHECK_INTERVAL_MS) return; |
| 140 | + } |
| 141 | + |
| 142 | + const latest = await fetchLatestVersion(); |
| 143 | + writeCache(file, { |
| 144 | + lastCheckedAt: new Date(now).toISOString(), |
| 145 | + latestVersion: latest, |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * If a newer version is known (from a prior check), print a one-line notice |
| 151 | + * to stderr. Never throws. |
| 152 | + */ |
| 153 | +export function maybeShowUpdateNotice(): void { |
| 154 | + if (isCheckDisabled()) return; |
| 155 | + const current = currentVersion(); |
| 156 | + if (current === null) return; |
| 157 | + |
| 158 | + const cache = readCache(cacheFilePath()); |
| 159 | + if (!cache || !cache.latestVersion) return; |
| 160 | + |
| 161 | + if (compareVersions(cache.latestVersion, current) > 0) { |
| 162 | + const latest = cache.latestVersion; |
| 163 | + process.stderr.write( |
| 164 | + `\n[n8n-cli] A new version ${latest} is available (current: ${current}).\n` + |
| 165 | + ` Update: git pull && make build (https://github.com/${REPO}/releases/latest)\n` + |
| 166 | + ` Silence: export N8N_CLI_DISABLE_UPDATE_CHECK=1\n`, |
| 167 | + ); |
| 168 | + } |
| 169 | +} |
0 commit comments