|
| 1 | +import { readFile, writeFile } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +type JsonObject = Record<string, unknown>; |
| 5 | + |
| 6 | +function isRecord(value: unknown): value is JsonObject { |
| 7 | + return typeof value === "object" && value !== null && !Array.isArray(value); |
| 8 | +} |
| 9 | + |
| 10 | +function isUnknownArray(value: unknown): value is readonly unknown[] { |
| 11 | + return Array.isArray(value); |
| 12 | +} |
| 13 | + |
| 14 | +function parseJsonObject(text: string, label: string): JsonObject { |
| 15 | + const parsed: unknown = JSON.parse(text); |
| 16 | + if (!isRecord(parsed)) { |
| 17 | + throw new Error(`${label} must contain a JSON object`); |
| 18 | + } |
| 19 | + return parsed; |
| 20 | +} |
| 21 | + |
| 22 | +function requireString(value: unknown, label: string): string { |
| 23 | + if (typeof value !== "string") { |
| 24 | + throw new Error(`${label} must be a string`); |
| 25 | + } |
| 26 | + return value; |
| 27 | +} |
| 28 | + |
| 29 | +function stringifyJson(value: JsonObject): string { |
| 30 | + return `${JSON.stringify(value, null, 2)}\n`; |
| 31 | +} |
| 32 | + |
| 33 | +const root = process.cwd(); |
| 34 | +const packagePath = path.join(root, "package.json"); |
| 35 | +const pluginPath = path.join(root, ".claude-plugin/plugin.json"); |
| 36 | +const serverPath = path.join(root, "server.json"); |
| 37 | + |
| 38 | +const packageObject = parseJsonObject( |
| 39 | + await readFile(packagePath, "utf8"), |
| 40 | + "package.json", |
| 41 | +); |
| 42 | +const pluginObject = parseJsonObject( |
| 43 | + await readFile(pluginPath, "utf8"), |
| 44 | + ".claude-plugin/plugin.json", |
| 45 | +); |
| 46 | +const serverObject = parseJsonObject( |
| 47 | + await readFile(serverPath, "utf8"), |
| 48 | + "server.json", |
| 49 | +); |
| 50 | + |
| 51 | +const packageVersion = requireString( |
| 52 | + packageObject.version, |
| 53 | + "package.json version", |
| 54 | +); |
| 55 | +const mcpName = requireString(packageObject.mcpName, "package.json mcpName"); |
| 56 | +const packageName = requireString(packageObject.name, "package.json name"); |
| 57 | +const serverName = requireString(serverObject.name, "server.json name"); |
| 58 | +const serverVersion = requireString( |
| 59 | + serverObject.version, |
| 60 | + "server.json version", |
| 61 | +); |
| 62 | +const pluginVersion = requireString( |
| 63 | + pluginObject.version, |
| 64 | + ".claude-plugin/plugin.json version", |
| 65 | +); |
| 66 | + |
| 67 | +if (serverName !== mcpName) { |
| 68 | + throw new Error( |
| 69 | + `server.json name (${serverName}) must match package.json mcpName (${mcpName})`, |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +const packagesValue = serverObject.packages; |
| 74 | +if (!isUnknownArray(packagesValue) || packagesValue.length !== 1) { |
| 75 | + throw new Error("server.json must define exactly one package entry"); |
| 76 | +} |
| 77 | + |
| 78 | +const packageEntryValue = packagesValue[0]; |
| 79 | +if (!isRecord(packageEntryValue)) { |
| 80 | + throw new Error("server.json package entry must be an object"); |
| 81 | +} |
| 82 | + |
| 83 | +const registryType = requireString( |
| 84 | + packageEntryValue.registryType, |
| 85 | + "server.json package entry registryType", |
| 86 | +); |
| 87 | +const identifier = requireString( |
| 88 | + packageEntryValue.identifier, |
| 89 | + "server.json package entry identifier", |
| 90 | +); |
| 91 | + |
| 92 | +if (registryType !== "npm") { |
| 93 | + throw new Error('server.json package entry must use registryType="npm"'); |
| 94 | +} |
| 95 | + |
| 96 | +if (identifier !== packageName) { |
| 97 | + throw new Error( |
| 98 | + `server.json package identifier (${identifier}) must match package.json name (${packageName})`, |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +serverObject.version = packageVersion; |
| 103 | +packageEntryValue.version = packageVersion; |
| 104 | +pluginObject.version = packageVersion; |
| 105 | + |
| 106 | +if (serverVersion !== packageVersion || pluginVersion !== packageVersion) { |
| 107 | + await writeFile(serverPath, stringifyJson(serverObject)); |
| 108 | + await writeFile(pluginPath, stringifyJson(pluginObject)); |
| 109 | +} |
0 commit comments