diff --git a/packages/next-codemod/bin/next-codemod.ts b/packages/next-codemod/bin/next-codemod.ts index d49ecb8e4762baeb6e02e4dc68f175a1f42de629..367412ee26feeca267d03c2e0050d2ea355fc55b 100644 --- a/packages/next-codemod/bin/next-codemod.ts +++ b/packages/next-codemod/bin/next-codemod.ts @@ -65,6 +65,11 @@ program ) .usage('[revision] [options]') .option('--verbose', 'Verbose output', false) + .option( + '-y, --yes', + 'Skip every interactive prompt and accept its default. Also auto-enabled when stdin is not a TTY (e.g. running under an agent or in CI).', + false + ) .action(async (revision, options) => { try { await runUpgrade(revision, options) diff --git a/packages/next-codemod/bin/upgrade.ts b/packages/next-codemod/bin/upgrade.ts index b7de97ce5cb1640afb7dd2fb93028d6055330801..2cb91c140a49348bb8bbd0335a14021c0441a7cd 100644 --- a/packages/next-codemod/bin/upgrade.ts +++ b/packages/next-codemod/bin/upgrade.ts @@ -112,9 +112,15 @@ function resolveSemanticRevision( export async function runUpgrade( revision: string | undefined, - options: { verbose: boolean } + options: { verbose: boolean; yes?: boolean } ): Promise { const { verbose } = options + const nonInteractive = options.yes === true || !process.stdin.isTTY + if (nonInteractive) { + console.log( + ` Running in non-interactive mode. Every prompt will accept its default.` + ) + } const appPackageJsonPath = path.resolve(cwd, 'package.json') let appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, 'utf8')) @@ -221,22 +227,27 @@ export async function runUpgrade( // 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: - `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', - }, - { onCancel } - ) - shouldStayOnReact18 = shouldStayOnReact18Res.shouldStayOnReact18 + if (nonInteractive) { + // Default: upgrade React past 18. + shouldStayOnReact18 = false + } else { + const shouldStayOnReact18Res = await prompts( + { + type: 'confirm', + name: 'shouldStayOnReact18', + 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', + }, + { onCancel } + ) + shouldStayOnReact18 = shouldStayOnReact18Res.shouldStayOnReact18 + } } // We're resolving a specific version here to avoid including "ugly" version queries @@ -254,12 +265,13 @@ export async function runUpgrade( compareVersions(targetNextVersion, '15.0.0-canary') >= 0 && compareVersions(targetNextVersion, '16.0.0-canary') < 0 ) { - await suggestTurbopack(appPackageJson, targetNextVersion) + await suggestTurbopack(appPackageJson, targetNextVersion, nonInteractive) } const codemods = await suggestCodemods( installedNextVersion, - targetNextVersion + targetNextVersion, + nonInteractive ) const packageManager: PackageManager = getPkgManager(cwd) @@ -272,8 +284,9 @@ export async function runUpgrade( compareVersions(targetReactVersion, '19.0.0-0') >= 0 && compareVersions(installedReactVersion, '19.0.0-0') < 0 ) { - shouldRunReactCodemods = await suggestReactCodemods() - shouldRunReactTypesCodemods = await suggestReactTypesCodemods() + shouldRunReactCodemods = await suggestReactCodemods(nonInteractive) + shouldRunReactTypesCodemods = + await suggestReactTypesCodemods(nonInteractive) execCommand = getNpxCommand(packageManager) } @@ -400,10 +413,12 @@ export async function runUpgrade( // understanding of the codemods, we run all of the applicable codemods. if (shouldRunReactCodemods) { // https://react.dev/blog/2024/04/25/react-19-upgrade-guide#run-all-react-19-codemods + // `--no-interactive` skips the interactive prompt that asks for confirmation + // https://github.com/codemod-com/codemod/blob/c0cf00d13161a0ec0965b6cc6bc5d54076839cc8/apps/cli/src/flags.ts#L160 + // `--allow-dirty` is required because the upgrade above modified package.json + // and the lockfile; the recipe refuses to run on a dirty tree otherwise. execSync( - // `--no-interactive` skips the interactive prompt that asks for confirmation - // https://github.com/codemod-com/codemod/blob/c0cf00d13161a0ec0965b6cc6bc5d54076839cc8/apps/cli/src/flags.ts#L160 - `${execCommand} codemod@latest react/19/migration-recipe --no-interactive`, + `${execCommand} codemod@latest react/19/migration-recipe --no-interactive --allow-dirty`, { stdio: 'inherit' } ) } @@ -486,7 +501,8 @@ function isUsingAppDir(projectPath: string): boolean { */ async function suggestTurbopack( packageJson: any, - targetNextVersion: string + targetNextVersion: string, + nonInteractive: boolean ): Promise { const devScript: string | undefined = packageJson.scripts?.['dev'] // Turbopack flag was changed from `--turbo` to `--turbopack` in v15.0.1-canary.3 @@ -518,17 +534,21 @@ async function suggestTurbopack( return } - const responseTurbopack = await prompts( - { - type: 'confirm', - name: 'enable', - message: `Enable Turbopack for ${pc.bold('next dev')}?`, - initial: true, - }, - { onCancel } - ) + let enable = true + if (!nonInteractive) { + const responseTurbopack = await prompts( + { + type: 'confirm', + name: 'enable', + message: `Enable Turbopack for ${pc.bold('next dev')}?`, + initial: true, + }, + { onCancel } + ) + enable = responseTurbopack.enable + } - if (!responseTurbopack.enable) { + if (!enable) { return } @@ -543,6 +563,12 @@ async function suggestTurbopack( `${pc.yellow('⚠')} Could not find "${pc.bold('next dev')}" in your dev script.` ) + if (nonInteractive) { + // Without a TTY we can't ask the user for a replacement script. + // Keep the existing dev script untouched. + return + } + const responseCustomDevScript = await prompts( { type: 'text', @@ -559,7 +585,8 @@ async function suggestTurbopack( async function suggestCodemods( initialNextVersion: string, - targetNextVersion: string + targetNextVersion: string, + nonInteractive: boolean ): Promise { // example: // codemod version: 15.0.0-canary.45 @@ -594,6 +621,16 @@ async function suggestCodemods( return [] } + if (nonInteractive) { + // Default: apply every recommended codemod, matching `selected: true` below. + const all = relevantCodemods.map(({ value }) => value) + console.log( + ` Applying all ${pc.blue('codemods')} recommended for your upgrade:\n` + + all.map((value) => ` - ${value}`).join('\n') + ) + return all + } + const { codemods } = await prompts( { type: 'multiselect', @@ -614,7 +651,10 @@ async function suggestCodemods( return codemods } -async function suggestReactCodemods(): Promise { +async function suggestReactCodemods(nonInteractive: boolean): Promise { + if (nonInteractive) { + return true + } const { runReactCodemod } = await prompts( { type: 'confirm', @@ -628,7 +668,12 @@ async function suggestReactCodemods(): Promise { return runReactCodemod } -async function suggestReactTypesCodemods(): Promise { +async function suggestReactTypesCodemods( + nonInteractive: boolean +): Promise { + if (nonInteractive) { + return true + } const { runReactTypesCodemod } = await prompts( { type: 'confirm',