File size: 8,287 Bytes
4514571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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<void> {
   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<void> {
   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<string[]> {
   // 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<boolean> {
+async function suggestReactCodemods(nonInteractive: boolean): Promise<boolean> {
+  if (nonInteractive) {
+    return true
+  }
   const { runReactCodemod } = await prompts(
     {
       type: 'confirm',
@@ -628,7 +668,12 @@ async function suggestReactCodemods(): Promise<boolean> {
   return runReactCodemod
 }
 
-async function suggestReactTypesCodemods(): Promise<boolean> {
+async function suggestReactTypesCodemods(
+  nonInteractive: boolean
+): Promise<boolean> {
+  if (nonInteractive) {
+    return true
+  }
   const { runReactTypesCodemod } = await prompts(
     {
       type: 'confirm',