Skip to content

Commit

Permalink
sync-react: Add affordance to stop syncing Pages Router (#73533)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon authored Dec 5, 2024
1 parent 6b39347 commit ca99500
Showing 1 changed file with 45 additions and 20 deletions.
65 changes: 45 additions & 20 deletions scripts/sync-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const repoOwner = 'vercel'
const repoName = 'next.js'
const pullRequestLabels = ['type: react-sync']
const pullRequestReviewers = ['eps1lon']
/**
* Set to `null` to automatically sync the React version of Pages Router with App Router React version.
* Set to a specific version to override the Pages Router React version e.g. `^19.0.0`.
* @type {string | null}
*/
const pagesRouterReact = null

const filesReferencingReactPeerDependencyVersion = [
'run-tests.js',
Expand Down Expand Up @@ -187,7 +193,14 @@ async function main() {

async function commitEverything(message) {
await execa('git', ['add', '-A'])
await execa('git', ['commit', '--message', message, '--no-verify'])
await execa('git', [
'commit',
'--message',
message,
'--no-verify',
// Some steps can be empty, e.g. when we don't sync Pages router
'--allow-empty',
])
}

if (createPull && !actor) {
Expand Down Expand Up @@ -306,22 +319,29 @@ Or, run this command with no arguments to use the most recently published versio
)
}

const syncPagesRouterReact = pagesRouterReact === null
const pagesRouterReactVersion = syncPagesRouterReact
? newVersionStr
: pagesRouterReact
const { sha: baseSha, dateString: baseDateString } = baseVersionInfo
for (const fileName of filesReferencingReactPeerDependencyVersion) {
const filePath = path.join(cwd, fileName)
const previousSource = await fsp.readFile(filePath, 'utf-8')
const updatedSource = previousSource.replace(
`const nextjsReactPeerVersion = "${baseVersionStr}";`,
`const nextjsReactPeerVersion = "${newVersionStr}";`
)
if (updatedSource === previousSource) {
errors.push(
new Error(
`${fileName}: Failed to update ${baseVersionStr} to ${newVersionStr}. Is this file still referencing the React peer dependency version?`
)

if (syncPagesRouterReact) {
for (const fileName of filesReferencingReactPeerDependencyVersion) {
const filePath = path.join(cwd, fileName)
const previousSource = await fsp.readFile(filePath, 'utf-8')
const updatedSource = previousSource.replace(
`const nextjsReactPeerVersion = "${baseVersionStr}";`,
`const nextjsReactPeerVersion = "${pagesRouterReactVersion}";`
)
} else {
await fsp.writeFile(filePath, updatedSource)
if (pagesRouterReact === null && updatedSource === previousSource) {
errors.push(
new Error(
`${fileName}: Failed to update ${baseVersionStr} to ${pagesRouterReactVersion}. Is this file still referencing the React peer dependency version?`
)
)
} else {
await fsp.writeFile(filePath, updatedSource)
}
}
}

Expand All @@ -330,10 +350,10 @@ Or, run this command with no arguments to use the most recently published versio
const packageJson = await fsp.readFile(packageJsonPath, 'utf-8')
const manifest = JSON.parse(packageJson)
if (manifest.dependencies['react']) {
manifest.dependencies['react'] = newVersionStr
manifest.dependencies['react'] = pagesRouterReactVersion
}
if (manifest.dependencies['react-dom']) {
manifest.dependencies['react-dom'] = newVersionStr
manifest.dependencies['react-dom'] = pagesRouterReactVersion
}
await fsp.writeFile(
packageJsonPath,
Expand All @@ -352,10 +372,12 @@ Or, run this command with no arguments to use the most recently published versio
const packageJson = await fsp.readFile(packageJsonPath, 'utf-8')
const manifest = JSON.parse(packageJson)
if (manifest.peerDependencies['react']) {
manifest.peerDependencies['react'] = `^18.2.0 || ${newVersionStr}`
manifest.peerDependencies['react'] =
`^18.2.0 || ${pagesRouterReactVersion}`
}
if (manifest.peerDependencies['react-dom']) {
manifest.peerDependencies['react-dom'] = `^18.2.0 || ${newVersionStr}`
manifest.peerDependencies['react-dom'] =
`^18.2.0 || ${pagesRouterReactVersion}`
}
await fsp.writeFile(
packageJsonPath,
Expand Down Expand Up @@ -417,7 +439,10 @@ Or, run this command with no arguments to use the most recently published versio
console.log()
}

let prDescription = `**breaking change for canary users: Bumps peer dependency of React from \`${baseVersionStr}\` to \`${newVersionStr}\`**\n\n`
let prDescription = ''
if (syncPagesRouterReact) {
prDescription += `**breaking change for canary users: Bumps peer dependency of React from \`${baseVersionStr}\` to \`${pagesRouterReactVersion}\`**\n\n`
}

// Fetch the changelog from GitHub and print it to the console.
prDescription += `[diff facebook/react@${baseSha}...${newSha}](https://github.com/facebook/react/compare/${baseSha}...${newSha})\n\n`
Expand Down

0 comments on commit ca99500

Please sign in to comment.