Read codemod target versions from workspace manifests at build time (#2419)

This commit is contained in:
Felix Weinberger 2026-07-02 16:01:32 +01:00 • committed by GitHub
commit 79dc162efc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 27 additions and 49 deletions

View file

@ -41,8 +41,6 @@
],
"scripts": {
"typecheck": "tsgo -p tsconfig.json --noEmit",
"generate:versions": "tsx scripts/generateVersions.ts",
"prebuild": "pnpm run generate:versions",
"build": "tsdown",
"build:watch": "tsdown --watch",
"prepack": "pnpm run build",

View file

@ -1,36 +0,0 @@
import { readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packagesDir = path.resolve(__dirname, '../..');
const PACKAGE_DIRS: Record<string, string> = {
'@modelcontextprotocol/client': 'client',
'@modelcontextprotocol/server': 'server',
'@modelcontextprotocol/node': 'middleware/node',
'@modelcontextprotocol/express': 'middleware/express',
'@modelcontextprotocol/server-legacy': 'server-legacy',
'@modelcontextprotocol/core': 'core'
};
const versions: Record<string, string> = {};
for (const [pkg, dir] of Object.entries(PACKAGE_DIRS)) {
const pkgJsonPath = path.join(packagesDir, dir, 'package.json');
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf8'));
versions[pkg] = `^${pkgJson.version}`;
}
const entries = Object.entries(versions);
const lines = entries.map(([pkg, ver], i) => ` '${pkg}': '${ver}'${i < entries.length - 1 ? ',' : ''}`).join('\n');
const output = `// AUTO-GENERATED — do not edit. Run \`pnpm run generate:versions\` to regenerate.
export const V2_PACKAGE_VERSIONS: Record<string, string> = {
${lines}
};
`;
const outPath = path.resolve(__dirname, '../src/generated/versions.ts');
writeFileSync(outPath, output);
console.log(`Wrote ${outPath}`);

View file

@ -6,10 +6,10 @@ import { tmpdir } from 'node:os';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { V2_PACKAGE_VERSIONS } from '../generated/versions';
import { getMigration } from '../migrations/index';
import { run } from '../runner';
import type { Diagnostic, Migration } from '../types';
import { V2_PACKAGE_VERSIONS } from '../versions';
// ---------------------------------------------------------------------------
// Types

View file

@ -1,9 +0,0 @@
// AUTO-GENERATED — do not edit. Run `pnpm run generate:versions` to regenerate.
export const V2_PACKAGE_VERSIONS: Record<string, string> = {
'@modelcontextprotocol/client': '^2.0.0-beta.1',
'@modelcontextprotocol/server': '^2.0.0-beta.1',
'@modelcontextprotocol/node': '^2.0.0-beta.1',
'@modelcontextprotocol/express': '^2.0.0-beta.1',
'@modelcontextprotocol/server-legacy': '^2.0.0-beta.1',
'@modelcontextprotocol/core': '^2.0.0-beta.1'
};

View file

@ -3,8 +3,8 @@ import path from 'node:path';
import fg from 'fast-glob';
import { V2_PACKAGE_VERSIONS } from '../generated/versions';
import type { PackageJsonChange } from '../types';
import { V2_PACKAGE_VERSIONS } from '../versions';
import { findPackageJson } from './projectAnalyzer';
const V1_PACKAGE = '@modelcontextprotocol/sdk';

View file

@ -0,0 +1,20 @@
import clientPkg from '../../client/package.json';
import corePkg from '../../core/package.json';
import expressPkg from '../../middleware/express/package.json';
import nodePkg from '../../middleware/node/package.json';
import serverPkg from '../../server/package.json';
import serverLegacyPkg from '../../server-legacy/package.json';
/**
* Caret ranges for the v2 packages the codemod writes into migrated package.json
* files. Versions come straight from the workspace manifests — the bundler inlines
* them at build time — so a release bump can never leave this map stale.
*/
export const V2_PACKAGE_VERSIONS: Record<string, string> = {
'@modelcontextprotocol/client': `^${clientPkg.version}`,
'@modelcontextprotocol/server': `^${serverPkg.version}`,
'@modelcontextprotocol/node': `^${nodePkg.version}`,
'@modelcontextprotocol/express': `^${expressPkg.version}`,
'@modelcontextprotocol/server-legacy': `^${serverLegacyPkg.version}`,
'@modelcontextprotocol/core': `^${corePkg.version}`
};