|
| 1 | +import packageJson from "../package.json" with { type: "json" }; |
| 2 | + |
| 3 | +function getSupportedReactRange() { |
| 4 | + const supportedReactRange = packageJson.testRenderer?.supportedReactRange; |
| 5 | + |
| 6 | + if (typeof supportedReactRange !== "string" || supportedReactRange.trim() === "") { |
| 7 | + throw new Error("package.json must define testRenderer.supportedReactRange"); |
| 8 | + } |
| 9 | + |
| 10 | + return supportedReactRange; |
| 11 | +} |
| 12 | + |
| 13 | +function parseSemver(version) { |
| 14 | + const match = /^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/.exec(version); |
| 15 | + |
| 16 | + if (!match) { |
| 17 | + throw new Error(`Unsupported semver format: ${version}`); |
| 18 | + } |
| 19 | + |
| 20 | + return { |
| 21 | + major: Number(match[1]), |
| 22 | + minor: Number(match[2]), |
| 23 | + patch: Number(match[3]), |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function compareSemver(left, right) { |
| 28 | + if (left.major !== right.major) { |
| 29 | + return left.major - right.major; |
| 30 | + } |
| 31 | + |
| 32 | + if (left.minor !== right.minor) { |
| 33 | + return left.minor - right.minor; |
| 34 | + } |
| 35 | + |
| 36 | + return left.patch - right.patch; |
| 37 | +} |
| 38 | + |
| 39 | +function incrementCaretUpperBound(version) { |
| 40 | + if (version.major > 0) { |
| 41 | + return { |
| 42 | + major: version.major + 1, |
| 43 | + minor: 0, |
| 44 | + patch: 0, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + major: 0, |
| 50 | + minor: version.minor + 1, |
| 51 | + patch: 0, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +function incrementTildeUpperBound(version) { |
| 56 | + return { |
| 57 | + major: version.major, |
| 58 | + minor: version.minor + 1, |
| 59 | + patch: 0, |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +function parseExclusiveUpperBounds(range) { |
| 64 | + const upperBounds = []; |
| 65 | + const lessThanMatches = range.matchAll(/<\s*(\d+\.\d+\.\d+(?:[-+][^\s|]+)?)/g); |
| 66 | + |
| 67 | + for (const match of lessThanMatches) { |
| 68 | + upperBounds.push(parseSemver(match[1])); |
| 69 | + } |
| 70 | + |
| 71 | + const caretMatches = range.matchAll(/\^(\d+\.\d+\.\d+(?:[-+][^\s|]+)?)/g); |
| 72 | + for (const match of caretMatches) { |
| 73 | + upperBounds.push(incrementCaretUpperBound(parseSemver(match[1]))); |
| 74 | + } |
| 75 | + |
| 76 | + const tildeMatches = range.matchAll(/~(\d+\.\d+\.\d+(?:[-+][^\s|]+)?)/g); |
| 77 | + for (const match of tildeMatches) { |
| 78 | + upperBounds.push(incrementTildeUpperBound(parseSemver(match[1]))); |
| 79 | + } |
| 80 | + |
| 81 | + return upperBounds; |
| 82 | +} |
| 83 | + |
| 84 | +function getSupportedReactCeiling(range) { |
| 85 | + const upperBounds = parseExclusiveUpperBounds(range); |
| 86 | + |
| 87 | + if (upperBounds.length === 0) { |
| 88 | + throw new Error(`Could not derive a supported React ceiling from range: ${range}`); |
| 89 | + } |
| 90 | + |
| 91 | + return upperBounds.reduce((lowest, candidate) => |
| 92 | + compareSemver(candidate, lowest) < 0 ? candidate : lowest, |
| 93 | + ); |
| 94 | +} |
| 95 | + |
| 96 | +function formatSemver(version) { |
| 97 | + return `${version.major}.${version.minor}.${version.patch}`; |
| 98 | +} |
| 99 | + |
| 100 | +function getSupportedLinesLabel(upperBound) { |
| 101 | + if (upperBound.patch === 0 && upperBound.minor > 0) { |
| 102 | + return `React ${upperBound.major}.0.x through ${upperBound.major}.${upperBound.minor - 1}.x`; |
| 103 | + } |
| 104 | + |
| 105 | + if (upperBound.minor === 0 && upperBound.patch === 0) { |
| 106 | + return `React ${upperBound.major - 1}.x`; |
| 107 | + } |
| 108 | + |
| 109 | + return `React versions <${formatSemver(upperBound)}`; |
| 110 | +} |
| 111 | + |
| 112 | +async function getLatestReactVersion() { |
| 113 | + const overriddenVersion = process.env.REACT_RELEASE_GUARD_LATEST; |
| 114 | + |
| 115 | + if (overriddenVersion) { |
| 116 | + return overriddenVersion; |
| 117 | + } |
| 118 | + |
| 119 | + const response = await fetch("https://registry.npmjs.org/react"); |
| 120 | + |
| 121 | + if (!response.ok) { |
| 122 | + throw new Error( |
| 123 | + `Failed to fetch react package metadata: ${response.status} ${response.statusText}`, |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + const metadata = await response.json(); |
| 128 | + const latestVersion = metadata?.["dist-tags"]?.latest; |
| 129 | + |
| 130 | + if (typeof latestVersion !== "string") { |
| 131 | + throw new Error("Missing dist-tags.latest in react package metadata"); |
| 132 | + } |
| 133 | + |
| 134 | + return latestVersion; |
| 135 | +} |
| 136 | + |
| 137 | +async function main() { |
| 138 | + const latestVersion = await getLatestReactVersion(); |
| 139 | + const parsedLatest = parseSemver(latestVersion); |
| 140 | + const peerRange = packageJson.peerDependencies?.react ?? "<missing>"; |
| 141 | + const supportedReactRange = getSupportedReactRange(); |
| 142 | + const supportedReactCeiling = getSupportedReactCeiling(supportedReactRange); |
| 143 | + const supportedLinesLabel = getSupportedLinesLabel(supportedReactCeiling); |
| 144 | + |
| 145 | + if (compareSemver(parsedLatest, supportedReactCeiling) < 0) { |
| 146 | + console.log( |
| 147 | + `React ${latestVersion} is still within the supported window (${supportedLinesLabel}).`, |
| 148 | + ); |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + const failureLines = [ |
| 153 | + `React ${latestVersion} has been released on npm, but this repository currently supports only ${supportedLinesLabel}.`, |
| 154 | + `A plain npm install of test-renderer is no longer safely constrained for the newest React release.`, |
| 155 | + `Current peerDependencies.react: ${peerRange}`, |
| 156 | + `Configured supportedReactRange: ${supportedReactRange}`, |
| 157 | + `Current derived React ceiling: <${formatSemver(supportedReactCeiling)}`, |
| 158 | + `Update testRenderer.supportedReactRange or tighten peerDependencies.react before relying on latest again.`, |
| 159 | + ]; |
| 160 | + |
| 161 | + throw new Error(failureLines.join("\n")); |
| 162 | +} |
| 163 | + |
| 164 | +main().catch((error) => { |
| 165 | + console.error(error instanceof Error ? error.message : error); |
| 166 | + process.exit(1); |
| 167 | +}); |
0 commit comments