From 6a8a51868ea5dd474f9b9e05f9887300bb5bf8da Mon Sep 17 00:00:00 2001 From: eps1lon Date: Fri, 18 Oct 2024 17:26:02 +0200 Subject: [PATCH 1/2] Avoid turbo prompt in unrelated test fixtures --- .../react-18-installed-mixed-router/package.json | 2 +- .../react-18-installed-pure-app-router/package.json | 2 +- .../react-18-installed-pure-pages-router/package.json | 2 +- .../react-19-installed-mixed-router/package.json | 2 +- .../react-19-installed-pure-app-router/package.json | 2 +- .../react-19-installed-pure-pages-router/package.json | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/next-codemod/bin/__testfixtures__/react-18-installed-mixed-router/package.json b/packages/next-codemod/bin/__testfixtures__/react-18-installed-mixed-router/package.json index f0e0da30ae636..d4ec0876ef911 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-18-installed-mixed-router/package.json +++ b/packages/next-codemod/bin/__testfixtures__/react-18-installed-mixed-router/package.json @@ -1,7 +1,7 @@ { "name": "react-18-installed-mixed-router", "scripts": { - "dev": "next dev" + "dev": "next dev --turbo" }, "dependencies": { "next": "14.3.0-canary.44", diff --git a/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-app-router/package.json b/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-app-router/package.json index 28587f48b3f75..a6d6e35851ba9 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-app-router/package.json +++ b/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-app-router/package.json @@ -1,7 +1,7 @@ { "name": "react-18-installed-pure-app-router", "scripts": { - "dev": "next dev" + "dev": "next dev --turbo" }, "dependencies": { "next": "14.3.0-canary.44", diff --git a/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-pages-router/package.json b/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-pages-router/package.json index 7ca58bd1489bb..04f4796eb7a1d 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-pages-router/package.json +++ b/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-pages-router/package.json @@ -1,7 +1,7 @@ { "name": "react-18-installed-pure-pages-router", "scripts": { - "dev": "next dev" + "dev": "next dev --turbo" }, "dependencies": { "next": "14.3.0-canary.44", diff --git a/packages/next-codemod/bin/__testfixtures__/react-19-installed-mixed-router/package.json b/packages/next-codemod/bin/__testfixtures__/react-19-installed-mixed-router/package.json index a3ee83b34a39b..6ce3e10669465 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-19-installed-mixed-router/package.json +++ b/packages/next-codemod/bin/__testfixtures__/react-19-installed-mixed-router/package.json @@ -1,7 +1,7 @@ { "name": "react-19-installed-mixed-router", "scripts": { - "dev": "next dev" + "dev": "next dev --turbo" }, "dependencies": { "next": "14.3.0-canary.45", diff --git a/packages/next-codemod/bin/__testfixtures__/react-19-installed-pure-app-router/package.json b/packages/next-codemod/bin/__testfixtures__/react-19-installed-pure-app-router/package.json index 9031ec28ac412..16c7f293d4b98 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-19-installed-pure-app-router/package.json +++ b/packages/next-codemod/bin/__testfixtures__/react-19-installed-pure-app-router/package.json @@ -1,7 +1,7 @@ { "name": "react-19-installed-pure-app-router", "scripts": { - "dev": "next dev" + "dev": "next dev --turbo" }, "dependencies": { "next": "14.3.0-canary.45", diff --git a/packages/next-codemod/bin/__testfixtures__/react-19-installed-pure-pages-router/package.json b/packages/next-codemod/bin/__testfixtures__/react-19-installed-pure-pages-router/package.json index 881881779fbd0..507c4a3f5e97c 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-19-installed-pure-pages-router/package.json +++ b/packages/next-codemod/bin/__testfixtures__/react-19-installed-pure-pages-router/package.json @@ -1,7 +1,7 @@ { "name": "react-19-installed-pure-pages-router", "scripts": { - "dev": "next dev" + "dev": "next dev --turbo" }, "dependencies": { "next": "14.3.0-canary.45", From 6a901f7ab2383b8de90ce0dc0de99c74392ccdff Mon Sep 17 00:00:00 2001 From: eps1lon Date: Fri, 18 Oct 2024 17:48:50 +0200 Subject: [PATCH 2/2] next-upgrade: Stop prompting for React 19 upgrade on pure App Router apps --- .../react-18-installed-mixed-router/README.md | 2 +- .../README.md | 2 +- .../README.md | 2 +- packages/next-codemod/bin/upgrade.ts | 31 +++++++++++++++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/next-codemod/bin/__testfixtures__/react-18-installed-mixed-router/README.md b/packages/next-codemod/bin/__testfixtures__/react-18-installed-mixed-router/README.md index d2c6dad29bc78..d3946453fe111 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-18-installed-mixed-router/README.md +++ b/packages/next-codemod/bin/__testfixtures__/react-18-installed-mixed-router/README.md @@ -1 +1 @@ -Prompts for React 19 upgrade +Prompts for React 19 upgrade with a recommendation to do so diff --git a/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-app-router/README.md b/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-app-router/README.md index d2c6dad29bc78..00219f3f72152 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-app-router/README.md +++ b/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-app-router/README.md @@ -1 +1 @@ -Prompts for React 19 upgrade +Upgrades to React 19 automatically diff --git a/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-pages-router/README.md b/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-pages-router/README.md index d2c6dad29bc78..f4bff8c3bee6c 100644 --- a/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-pages-router/README.md +++ b/packages/next-codemod/bin/__testfixtures__/react-18-installed-pure-pages-router/README.md @@ -1 +1 @@ -Prompts for React 19 upgrade +Prompts for React 19 upgrade without any recommendation diff --git a/packages/next-codemod/bin/upgrade.ts b/packages/next-codemod/bin/upgrade.ts index 17f637963f52d..49e195d57c66c 100644 --- a/packages/next-codemod/bin/upgrade.ts +++ b/packages/next-codemod/bin/upgrade.ts @@ -125,6 +125,12 @@ export async function runUpgrade( console.log(` - React: v${installedReactVersion}`) console.log(` - Next.js: v${installedNextVersion}`) let shouldStayOnReact18 = false + + const usesAppDir = isUsingAppDir(process.cwd()) + const usesPagesDir = isUsingPagesDir(process.cwd()) + + const isPureAppRouter = usesAppDir && !usesPagesDir + const isMixedApp = usesPagesDir && usesAppDir if ( // From release v14.3.0-canary.45, Next.js expects the React version to be 19.0.0-beta.0 // If the user is on a version higher than this but is still on React 18, we ask them @@ -134,13 +140,21 @@ export async function runUpgrade( // x-ref(PR): https://github.com/vercel/next.js/pull/65058 // x-ref(release): https://github.com/vercel/next.js/releases/tag/v14.3.0-canary.45 compareVersions(targetNextVersion, '14.3.0-canary.45') >= 0 && - installedReactVersion.startsWith('18') + installedReactVersion.startsWith('18') && + // Pure App Router always uses React 19 + // The mixed case is tricky to handle from a types perspective. + // We'll recommend to upgrade in the prompt but users can decide to try 18. + !isPureAppRouter ) { const shouldStayOnReact18Res = await prompts( { type: 'confirm', name: 'shouldStayOnReact18', - message: `Are you using ${pc.underline('only the Pages Router')} (no App Router) and prefer to stay on React 18?`, + message: + `Do you prefer to stay on React 18?` + + (isMixedApp + ? " Since you're using both pages/ and app/, we recommend upgrading React to use a consistent version throughout your app." + : ''), initial: false, active: 'Yes', inactive: 'No', @@ -371,6 +385,19 @@ function getInstalledReactVersion(): string { } } +function isUsingPagesDir(projectPath: string): boolean { + return ( + fs.existsSync(path.resolve(projectPath, 'pages')) || + fs.existsSync(path.resolve(projectPath, 'src/pages')) + ) +} +function isUsingAppDir(projectPath: string): boolean { + return ( + fs.existsSync(path.resolve(projectPath, 'app')) || + fs.existsSync(path.resolve(projectPath, 'src/app')) + ) +} + /* * Heuristics are used to determine whether to Turbopack is enabled or not and * to determine how to update the dev script.