|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { createRequire } from 'module'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Spec-version drift advisory. |
| 7 | + * |
| 8 | + * Surfaces the one thing an AI agent (or human) upgrading a third-party app |
| 9 | + * almost never finds on its own: the curated per-major migration guide. When |
| 10 | + * an app's authored `manifest.specVersion` declares an OLDER major than the |
| 11 | + * `@objectstack/spec` actually installed in its `node_modules`, the platform |
| 12 | + * moved ahead of the app and there is breaking-change guidance the author |
| 13 | + * should read before proceeding. Every major from v12 on is guaranteed a |
| 14 | + * `content/docs/releases/v<major>.mdx` page (enforced by |
| 15 | + * `scripts/check-release-notes.mjs`), so the URL below never 404s. |
| 16 | + * |
| 17 | + * The check is advisory-only — it never fails a build/validate. It exists so |
| 18 | + * the release notes are discoverable at the exact moment of the upgrade, |
| 19 | + * instead of being reverse-engineered from per-package `CHANGELOG.md` files. |
| 20 | + */ |
| 21 | + |
| 22 | +const RELEASES_BASE = 'https://docs.objectstack.ai/docs/releases'; |
| 23 | + |
| 24 | +export interface SpecVersionGap { |
| 25 | + /** Major of the `@objectstack/spec` resolved from the app's node_modules. */ |
| 26 | + installedMajor: number; |
| 27 | + /** Major declared by the app's `manifest.specVersion` range. */ |
| 28 | + declaredMajor: number; |
| 29 | + /** Full installed spec version (e.g. `14.7.0`). */ |
| 30 | + installedVersion: string; |
| 31 | + /** Canonical migration guide for the installed major. */ |
| 32 | + url: string; |
| 33 | + /** Ready-to-print one-line advisory. */ |
| 34 | + message: string; |
| 35 | + /** Ready-to-print follow-up pointing at the guide. */ |
| 36 | + hint: string; |
| 37 | +} |
| 38 | + |
| 39 | +/** Parse the leading major integer out of a semver range like `^12.0.0`, `>=13`, `14.x`. */ |
| 40 | +function parseMajor(range: unknown): number | null { |
| 41 | + if (typeof range !== 'string') return null; |
| 42 | + const m = range.match(/\d+/); |
| 43 | + if (!m) return null; |
| 44 | + const major = Number.parseInt(m[0], 10); |
| 45 | + return Number.isFinite(major) ? major : null; |
| 46 | +} |
| 47 | + |
| 48 | +/** Resolve the installed `@objectstack/spec` version from the app being operated on. */ |
| 49 | +function resolveInstalledSpecVersion(): string | null { |
| 50 | + try { |
| 51 | + // Resolve relative to the CWD (the app), not the CLI install, so a globally |
| 52 | + // linked CLI still reports the app's locked spec version. Fall back to the |
| 53 | + // CLI's own resolution if the app doesn't hoist spec to its root. |
| 54 | + const requireFromApp = createRequire(`${process.cwd()}/package.json`); |
| 55 | + const pkg = requireFromApp('@objectstack/spec/package.json') as { version?: string }; |
| 56 | + if (typeof pkg.version === 'string') return pkg.version; |
| 57 | + } catch { |
| 58 | + // ignore — try the CLI-relative resolution below |
| 59 | + } |
| 60 | + try { |
| 61 | + const requireFromCli = createRequire(import.meta.url); |
| 62 | + const pkg = requireFromCli('@objectstack/spec/package.json') as { version?: string }; |
| 63 | + if (typeof pkg.version === 'string') return pkg.version; |
| 64 | + } catch { |
| 65 | + // ignore — spec not resolvable, no advisory |
| 66 | + } |
| 67 | + return null; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Compute a spec-version drift advisory for the given app manifest, or `null` |
| 72 | + * when there is nothing to say (spec unresolvable, no `specVersion` declared, |
| 73 | + * or the declared major already matches / leads the installed platform). |
| 74 | + */ |
| 75 | +export function checkSpecVersionGap( |
| 76 | + manifest: { specVersion?: unknown } | undefined | null, |
| 77 | + /** Injectable for tests; defaults to the spec resolved from the app on disk. */ |
| 78 | + installedVersion: string | null = resolveInstalledSpecVersion(), |
| 79 | +): SpecVersionGap | null { |
| 80 | + const declaredMajor = parseMajor(manifest?.specVersion); |
| 81 | + if (declaredMajor == null) return null; |
| 82 | + |
| 83 | + if (!installedVersion) return null; |
| 84 | + const installedMajor = parseMajor(installedVersion); |
| 85 | + if (installedMajor == null) return null; |
| 86 | + |
| 87 | + // Only the upgrade case: the platform on disk is newer than what the app |
| 88 | + // declares. (declaredMajor > installedMajor is a stale/mismatched install — |
| 89 | + // a different problem, out of scope for release-note discoverability.) |
| 90 | + if (declaredMajor >= installedMajor) return null; |
| 91 | + |
| 92 | + const url = `${RELEASES_BASE}/v${installedMajor}`; |
| 93 | + return { |
| 94 | + installedMajor, |
| 95 | + declaredMajor, |
| 96 | + installedVersion, |
| 97 | + url, |
| 98 | + message: `Installed @objectstack/spec is v${installedVersion} but this app declares specVersion for v${declaredMajor}.`, |
| 99 | + hint: `Review the v${installedMajor} migration guide before bumping specVersion: ${url}`, |
| 100 | + }; |
| 101 | +} |
0 commit comments