feat: add v2 codemod draft (#1950)
This commit is contained in:
parent
4f226c1e35
commit
48251fe2cb
2 changed files with 9239 additions and 1 deletions
39
packages/codemod/scripts/generateSpecSchemaMap.ts
Normal file
39
packages/codemod/scripts/generateSpecSchemaMap.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
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 specTypeSchemaPath = path.resolve(__dirname, '../../core/src/types/specTypeSchema.ts');
|
||||
|
||||
const source = readFileSync(specTypeSchemaPath, 'utf8');
|
||||
|
||||
// Extract SPEC_SCHEMA_KEYS array entries
|
||||
const keysMatch = source.match(/const SPEC_SCHEMA_KEYS = \[([\s\S]*?)\] as const/);
|
||||
if (!keysMatch) throw new Error('Could not find SPEC_SCHEMA_KEYS in specTypeSchema.ts');
|
||||
|
||||
const protocolSchemas = [...keysMatch[1]!.matchAll(/'([^']+)'/g)].map(m => m[1]!);
|
||||
|
||||
// Extract auth schema keys
|
||||
const authMatch = source.match(/const authSchemas = \{([\s\S]*?)\} as const/);
|
||||
if (!authMatch) throw new Error('Could not find authSchemas in specTypeSchema.ts');
|
||||
|
||||
const authSchemas = [...authMatch[1]!.matchAll(/(\w+Schema)/g)].map(m => m[1]!);
|
||||
|
||||
const allSchemas = [...protocolSchemas, ...authSchemas].toSorted();
|
||||
|
||||
const entries = allSchemas.map((s, i) => ` '${s}'${i < allSchemas.length - 1 ? ',' : ''}`).join('\n');
|
||||
|
||||
const output = `// AUTO-GENERATED — do not edit. Run \`pnpm run generate:spec-schemas\` to regenerate.
|
||||
export const SPEC_SCHEMA_NAMES: ReadonlySet<string> = new Set([
|
||||
${entries}
|
||||
]);
|
||||
|
||||
export function specSchemaToTypeName(schemaName: string): string | undefined {
|
||||
if (!SPEC_SCHEMA_NAMES.has(schemaName)) return undefined;
|
||||
return schemaName.slice(0, -'Schema'.length);
|
||||
}
|
||||
`;
|
||||
|
||||
const outPath = path.resolve(__dirname, '../src/generated/specSchemaMap.ts');
|
||||
writeFileSync(outPath, output);
|
||||
console.log(`Wrote ${outPath} (${allSchemas.length} schemas)`);
|
||||
34
packages/codemod/scripts/generateVersions.ts
Normal file
34
packages/codemod/scripts/generateVersions.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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'
|
||||
};
|
||||
|
||||
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}`);
|
||||
Loading…
Reference in a new issue