UNPKG

zephyr-agent

Version:
103 lines (102 loc) • 4.74 kB
import "node:module"; import { readFileSync } from "node:fs"; import { createRequire } from "node:module"; import { safe_json_parse } from "zephyr-edge-contract"; import { fetchWithRetries } from "../http/fetch-with-retries.mjs"; import { brightYellowBgName } from "../logging/debug.mjs"; import { getZephyrAgentVersion } from "./zephyr-agent-version.mjs"; import { fileURLToPath as __rspack_fileURLToPath } from "node:url"; var __rspack_import_meta_filename__ = __rspack_fileURLToPath(import.meta.url); const warnedVersionPairs = new Set(); const NPM_FETCH_TIMEOUT_MS = 1500; const requireFromHere = createRequire(__rspack_import_meta_filename__); function parseVersion(version) { const sanitized = version.trim().replace(/^v/i, ''); const match = /^(\d+)\.(\d+)\.(\d+)(?:-(canary|next)\.(\d+))?$/.exec(sanitized); if (!match) return null; return { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]), channel: match[4] ?? 'stable', prereleaseNumber: match[5] ? Number(match[5]) : null }; } function isOlderVersion(currentVersion, latestVersion) { const current = parseVersion(currentVersion); const latest = parseVersion(latestVersion); if (!current || !latest) return false; if (current.major !== latest.major) return current.major < latest.major; if (current.minor !== latest.minor) return current.minor < latest.minor; if (current.patch !== latest.patch) return current.patch < latest.patch; if (current.channel !== latest.channel) return false; if ('stable' === current.channel) return false; if (null === current.prereleaseNumber || null === latest.prereleaseNumber) return false; return current.prereleaseNumber < latest.prereleaseNumber; } function getComparisonTarget(currentVersion, distTags) { const parsedCurrentVersion = parseVersion(currentVersion); if (!parsedCurrentVersion) return null; if ('canary' === parsedCurrentVersion.channel) return 'string' == typeof distTags.canary ? { label: 'latest canary', version: distTags.canary } : null; if ('next' === parsedCurrentVersion.channel) return 'string' == typeof distTags.next ? { label: 'latest next', version: distTags.next } : null; return 'string' == typeof distTags.latest ? { label: 'latest', version: distTags.latest } : null; } async function fetchDistTags(packageName) { const npmDistTagsUrl = new URL(`https://registry.npmjs.org/-/package/${encodeURIComponent(packageName)}/dist-tags`); const abortController = new AbortController(); const timeoutId = setTimeout(()=>abortController.abort(), NPM_FETCH_TIMEOUT_MS); try { const response = await fetchWithRetries(npmDistTagsUrl, { signal: abortController.signal }, 2); return await response.json(); } catch { return null; } finally{ clearTimeout(timeoutId); } } function resolveInstalledPluginVersion(pluginPackageName) { try { const packageJsonPath = requireFromHere.resolve(`${pluginPackageName}/package.json`, { paths: [ process.cwd() ] }); const packageJsonContent = readFileSync(packageJsonPath, 'utf8'); const parsed = safe_json_parse(packageJsonContent); if (parsed?.version && 'string' == typeof parsed.version) return parsed.version; } catch {} return getZephyrAgentVersion(); } async function maybeShowOutdatedPluginWarning(pluginPackageName) { const currentVersion = resolveInstalledPluginVersion(pluginPackageName); const distTags = await fetchDistTags(pluginPackageName); if (!distTags) return; const comparisonTarget = getComparisonTarget(currentVersion, distTags); if (!comparisonTarget) return; if (!isOlderVersion(currentVersion, comparisonTarget.version)) return; const versionPair = `${pluginPackageName}:${currentVersion}->${comparisonTarget.version}`; if (warnedVersionPairs.has(versionPair)) return; warnedVersionPairs.add(versionPair); const message = [ `${brightYellowBgName} Your Zephyr Plugin version is outdated (current: ${currentVersion}, ${comparisonTarget.label}: ${comparisonTarget.version}).`, `${brightYellowBgName} Older versions can cause unexpected build and deployment issues.`, `${brightYellowBgName} If you are facing any issue, upgrade ${pluginPackageName} first.` ].join('\n'); console.error(message); } function resetOutdatedPluginWarningStateForTests() { warnedVersionPairs.clear(); } export { maybeShowOutdatedPluginWarning, resetOutdatedPluginWarningStateForTests }; //# sourceMappingURL=outdated-plugin-warning.mjs.map