|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | +import { fileURLToPath } from "url"; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | + |
| 10 | +/** |
| 11 | + * Checks version consistency across all package.json files in the monorepo |
| 12 | + * Exits with code 1 if versions are inconsistent |
| 13 | + * Usage: node scripts/check-version-consistency.js |
| 14 | + */ |
| 15 | + |
| 16 | +console.log("🔍 Checking version consistency across packages...\n"); |
| 17 | + |
| 18 | +// List of package.json files to check |
| 19 | +const packagePaths = [ |
| 20 | + "package.json", |
| 21 | + "client/package.json", |
| 22 | + "server/package.json", |
| 23 | + "cli/package.json", |
| 24 | +]; |
| 25 | + |
| 26 | +const versions = new Map(); |
| 27 | +const errors = []; |
| 28 | + |
| 29 | +// Read version from each package.json |
| 30 | +packagePaths.forEach((packagePath) => { |
| 31 | + const fullPath = path.join(__dirname, "..", packagePath); |
| 32 | + |
| 33 | + if (!fs.existsSync(fullPath)) { |
| 34 | + console.warn(`⚠️ Skipping ${packagePath} - file not found`); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + const packageJson = JSON.parse(fs.readFileSync(fullPath, "utf8")); |
| 40 | + const version = packageJson.version; |
| 41 | + const packageName = packageJson.name || packagePath; |
| 42 | + |
| 43 | + versions.set(packagePath, { |
| 44 | + name: packageName, |
| 45 | + version: version, |
| 46 | + dependencies: packageJson.dependencies || {}, |
| 47 | + }); |
| 48 | + |
| 49 | + console.log(`📦 ${packagePath}:`); |
| 50 | + console.log(` Name: ${packageName}`); |
| 51 | + console.log(` Version: ${version}`); |
| 52 | + } catch (error) { |
| 53 | + errors.push(`Failed to read ${packagePath}: ${error.message}`); |
| 54 | + } |
| 55 | +}); |
| 56 | + |
| 57 | +if (errors.length > 0) { |
| 58 | + console.error("\n❌ Errors occurred while reading package files:"); |
| 59 | + errors.forEach((error) => console.error(` - ${error}`)); |
| 60 | + process.exit(1); |
| 61 | +} |
| 62 | + |
| 63 | +// Check if all versions match |
| 64 | +const allVersions = Array.from(versions.values()).map((v) => v.version); |
| 65 | +const uniqueVersions = [...new Set(allVersions)]; |
| 66 | + |
| 67 | +console.log("\n📊 Version Summary:"); |
| 68 | +console.log(` Total packages: ${versions.size}`); |
| 69 | +console.log(` Unique versions: ${uniqueVersions.length}`); |
| 70 | + |
| 71 | +if (uniqueVersions.length > 1) { |
| 72 | + console.error("\n❌ Version mismatch detected!"); |
| 73 | + console.error(" Found versions: " + uniqueVersions.join(", ")); |
| 74 | + |
| 75 | + console.error("\n Package versions:"); |
| 76 | + versions.forEach((info, path) => { |
| 77 | + console.error(` - ${path}: ${info.version}`); |
| 78 | + }); |
| 79 | +} else { |
| 80 | + console.log(` ✅ All packages are at version: ${uniqueVersions[0]}`); |
| 81 | +} |
| 82 | + |
| 83 | +// Check workspace dependencies in root package.json |
| 84 | +const rootPackage = versions.get("package.json"); |
| 85 | +if (rootPackage) { |
| 86 | + console.log("\n🔗 Checking workspace dependencies..."); |
| 87 | + const expectedVersion = rootPackage.version; |
| 88 | + let dependencyErrors = false; |
| 89 | + |
| 90 | + Object.entries(rootPackage.dependencies).forEach(([dep, version]) => { |
| 91 | + if (dep.startsWith("@modelcontextprotocol/inspector-")) { |
| 92 | + const expectedDepVersion = `^${expectedVersion}`; |
| 93 | + if (version !== expectedDepVersion) { |
| 94 | + console.error( |
| 95 | + ` ❌ ${dep}: ${version} (expected ${expectedDepVersion})`, |
| 96 | + ); |
| 97 | + dependencyErrors = true; |
| 98 | + } else { |
| 99 | + console.log(` ✅ ${dep}: ${version}`); |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + if (dependencyErrors) { |
| 105 | + errors.push("Workspace dependency versions do not match package versions"); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Check if package-lock.json is up to date |
| 110 | +console.log("\n🔒 Checking package-lock.json..."); |
| 111 | +const lockPath = path.join(__dirname, "..", "package-lock.json"); |
| 112 | +let lockFileError = false; |
| 113 | + |
| 114 | +if (!fs.existsSync(lockPath)) { |
| 115 | + console.error(" ❌ package-lock.json not found"); |
| 116 | + lockFileError = true; |
| 117 | +} else { |
| 118 | + try { |
| 119 | + const lockFile = JSON.parse(fs.readFileSync(lockPath, "utf8")); |
| 120 | + const lockVersion = lockFile.version; |
| 121 | + const expectedVersion = rootPackage?.version || uniqueVersions[0]; |
| 122 | + |
| 123 | + if (lockVersion !== expectedVersion) { |
| 124 | + console.error( |
| 125 | + ` ❌ package-lock.json version (${lockVersion}) does not match package.json version (${expectedVersion})`, |
| 126 | + ); |
| 127 | + lockFileError = true; |
| 128 | + } else { |
| 129 | + console.log(` ✅ package-lock.json version matches: ${lockVersion}`); |
| 130 | + } |
| 131 | + |
| 132 | + // Check workspace package versions in lock file |
| 133 | + if (lockFile.packages) { |
| 134 | + const workspacePackages = [ |
| 135 | + { path: "client", name: "@modelcontextprotocol/inspector-client" }, |
| 136 | + { path: "server", name: "@modelcontextprotocol/inspector-server" }, |
| 137 | + { path: "cli", name: "@modelcontextprotocol/inspector-cli" }, |
| 138 | + ]; |
| 139 | + |
| 140 | + workspacePackages.forEach(({ path, name }) => { |
| 141 | + const lockPkgPath = lockFile.packages[path]; |
| 142 | + if (lockPkgPath && lockPkgPath.version !== expectedVersion) { |
| 143 | + console.error( |
| 144 | + ` ❌ ${name} in lock file: ${lockPkgPath.version} (expected ${expectedVersion})`, |
| 145 | + ); |
| 146 | + lockFileError = true; |
| 147 | + } |
| 148 | + }); |
| 149 | + } |
| 150 | + } catch (error) { |
| 151 | + console.error(` ❌ Failed to parse package-lock.json: ${error.message}`); |
| 152 | + lockFileError = true; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// Final result |
| 157 | +console.log("\n🎯 Result:"); |
| 158 | +if (uniqueVersions.length === 1 && errors.length === 0 && !lockFileError) { |
| 159 | + console.log(" ✅ Version consistency check passed!"); |
| 160 | + process.exit(0); |
| 161 | +} else { |
| 162 | + console.error(" ❌ Version consistency check failed!"); |
| 163 | + if (uniqueVersions.length > 1) { |
| 164 | + console.error(" - Package versions are not consistent"); |
| 165 | + } |
| 166 | + if (errors.length > 0) { |
| 167 | + console.error(" - " + errors.join("\n - ")); |
| 168 | + } |
| 169 | + if (lockFileError) { |
| 170 | + console.error(" - package-lock.json is out of sync"); |
| 171 | + } |
| 172 | + console.error( |
| 173 | + '\n💡 Run "npm run update-version <new-version>" to fix version inconsistencies', |
| 174 | + ); |
| 175 | + console.error(' or run "npm install" to update package-lock.json'); |
| 176 | + process.exit(1); |
| 177 | +} |
0 commit comments