File size: 23,256 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
import * as os from 'os'
import prompts from 'prompts'
import fs from 'fs'
import {
  satisfies as satisfiesVersionRange,
  compare as compareVersions,
} from 'semver'
import { execSync } from 'child_process'
import path from 'path'
import pc from 'picocolors'
import {
  getPkgManager,
  addPackageDependency,
  runInstallation,
} from '../lib/handle-package'
import { runTransform } from './transform'
import { onCancel, TRANSFORMER_INQUIRER_CHOICES } from '../lib/utils'
import { BadInput } from './shared'

type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun'

const optionalNextjsPackages = [
  'create-next-app',
  'eslint-config-next',
  '@next/bundle-analyzer',
  '@next/codemod',
  '@next/env',
  '@next/eslint-plugin-next',
  '@next/mdx',
  '@next/plugin-storybook',
  '@next/polyfill-module',
  '@next/polyfill-nomodule',
  '@next/swc',
  '@next/react-refresh-utils',
  '@next/third-parties',
]

/**
 * @param query
 * @example loadHighestNPMVersionMatching("react@^18.3.0 || ^19.0.0") === Promise<"19.0.0">
 */
async function loadHighestNPMVersionMatching(query: string) {
  const versionsJSON = execSync(
    `npm --silent view "${query}" --json --field version`,
    { encoding: 'utf-8' }
  )
  const versionOrVersions = JSON.parse(versionsJSON)
  if (versionOrVersions.length < 1) {
    throw new Error(
      `Found no React versions matching "${query}". This is a bug in the upgrade tool.`
    )
  }
  // npm-view returns an array if there are multiple versions matching the query.
  if (Array.isArray(versionOrVersions)) {
    // The last entry will be the latest version published.
    // But we want the highest version.
    versionOrVersions.sort(compareVersions)
    return versionOrVersions[versionOrVersions.length - 1]
  }
  return versionOrVersions
}

function endMessage() {
  console.log()
  console.log(
    pc.white(
      pc.bold(
        `Please review the local changes and read the Next.js 15 migration guide to complete the migration.`
      )
    )
  )
  console.log(
    pc.underline(
      'https://nextjs.org/docs/canary/app/building-your-application/upgrading/version-15'
    )
  )
}

const cwd = process.cwd()

export async function runUpgrade(
  revision: string | undefined,
  options: { verbose: boolean }
): Promise<void> {
  const { verbose } = options
  const appPackageJsonPath = path.resolve(cwd, 'package.json')
  let appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, 'utf8'))

  let targetNextPackageJson: {
    version: string
    peerDependencies: Record<string, string>
  }

  try {
    const targetNextPackage = execSync(
      `npm --silent view "next@${revision}" --json`,
      { encoding: 'utf-8' }
    )
    targetNextPackageJson = JSON.parse(targetNextPackage)
  } catch {}

  const validRevision =
    targetNextPackageJson !== null &&
    typeof targetNextPackageJson === 'object' &&
    'version' in targetNextPackageJson &&
    'peerDependencies' in targetNextPackageJson
  if (!validRevision) {
    throw new BadInput(
      `Invalid revision provided: "${revision}". Please provide a valid Next.js version or dist-tag (e.g. "latest", "canary", "rc", or "15.0.0").\nCheck available versions at https://www.npmjs.com/package/next?activeTab=versions.`
    )
  }

  const installedNextVersion = getInstalledNextVersion()

  const targetNextVersion = targetNextPackageJson.version

  if (compareVersions(installedNextVersion, targetNextVersion) === 0) {
    console.log(
      `${pc.green('βœ“')} Current Next.js version is already on the target version "v${targetNextVersion}".`
    )
    endMessage()
    return
  }
  if (compareVersions(installedNextVersion, targetNextVersion) > 0) {
    console.log(
      `${pc.green('βœ“')} Current Next.js version is higher than the target version "v${targetNextVersion}".`
    )
    endMessage()
    return
  }

  const installedReactVersion = getInstalledReactVersion()
  // Align the prefix spaces
  console.log(`  Detected installed versions:`)
  console.log(`  - React: v${installedReactVersion}`)
  console.log(`  - Next.js: v${installedNextVersion}`)
  let shouldStayOnReact18 = false

  const usesAppDir = isUsingAppDir(cwd)
  const usesPagesDir = isUsingPagesDir(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
    // if they still want to stay on React 18 after the upgrade.
    // IF THE USER USES APP ROUTER, we expect them to upgrade React to > 19.0.0-beta.0,
    // we should only let the user stay on React 18 if they are using pure Pages Router.
    // 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') &&
    // 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:
          `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
  // in the manifest.
  // E.g. in peerDependencies we could have `^18.2.0 || ^19.0.0 || 20.0.0-canary`
  // If we'd just `npm add` that, the manifest would read the same version query.
  // This is basically a `npm --save-exact react@$versionQuery` that works for every package manager.
  const targetReactVersion = shouldStayOnReact18
    ? '18.3.1'
    : await loadHighestNPMVersionMatching(
        `react@${targetNextPackageJson.peerDependencies['react']}`
      )

  if (compareVersions(targetNextVersion, '15.0.0-canary') >= 0) {
    await suggestTurbopack(appPackageJson, targetNextVersion)
  }

  const codemods = await suggestCodemods(
    installedNextVersion,
    targetNextVersion
  )
  const packageManager: PackageManager = getPkgManager(cwd)

  let shouldRunReactCodemods = false
  let shouldRunReactTypesCodemods = false
  let execCommand = 'npx'
  // The following React codemods are for React 19
  if (
    !shouldStayOnReact18 &&
    compareVersions(targetReactVersion, '19.0.0-0') >= 0 &&
    compareVersions(installedReactVersion, '19.0.0-0') < 0
  ) {
    shouldRunReactCodemods = await suggestReactCodemods()
    shouldRunReactTypesCodemods = await suggestReactTypesCodemods()

    const execCommandMap = {
      yarn: 'yarn dlx',
      pnpm: 'pnpx',
      bun: 'bunx',
      npm: 'npx',
    }
    execCommand = execCommandMap[packageManager]
  }

  fs.writeFileSync(appPackageJsonPath, JSON.stringify(appPackageJson, null, 2))

  const dependenciesToInstall: [string, string][] = []
  const devDependenciesToInstall: [string, string][] = []

  const allDependencies = {
    ...appPackageJson.dependencies,
    ...appPackageJson.devDependencies,
  }

  const versionMapping: Record<string, { version: string; required: boolean }> =
    {
      next: { version: targetNextVersion, required: true },
      react: { version: targetReactVersion, required: true },
      'react-dom': { version: targetReactVersion, required: true },
      'react-is': { version: targetReactVersion, required: false },
    }
  for (const optionalNextjsPackage of optionalNextjsPackages) {
    versionMapping[optionalNextjsPackage] = {
      version: targetNextVersion,
      required: false,
    }
  }

  if (
    targetReactVersion.startsWith('19.0.0-canary') ||
    targetReactVersion.startsWith('19.0.0-beta') ||
    targetReactVersion.startsWith('19.0.0-rc')
  ) {
    const [targetReactTypesVersion, targetReactDOMTypesVersion] =
      await Promise.all([
        loadHighestNPMVersionMatching(`types-react@rc`),
        loadHighestNPMVersionMatching(`types-react-dom@rc`),
      ])
    if (allDependencies['@types/react']) {
      versionMapping['@types/react'] = {
        version: `npm:types-react@${targetReactTypesVersion}`,
        required: false,
      }
    }
    if (allDependencies['@types/react-dom']) {
      versionMapping['@types/react-dom'] = {
        version: `npm:types-react-dom@${targetReactDOMTypesVersion}`,
        required: false,
      }
    }
  } else {
    const [targetReactTypesVersion, targetReactDOMTypesVersion] =
      await Promise.all([
        loadHighestNPMVersionMatching(
          `@types/react@${targetNextPackageJson.peerDependencies['react']}`
        ),
        loadHighestNPMVersionMatching(
          `@types/react-dom@${targetNextPackageJson.peerDependencies['react']}`
        ),
      ])

    if (allDependencies['@types/react']) {
      versionMapping['@types/react'] = {
        version: targetReactTypesVersion,
        required: false,
      }
    }
    if (allDependencies['@types/react-dom']) {
      versionMapping['@types/react-dom'] = {
        version: targetReactDOMTypesVersion,
        required: false,
      }
    }
  }

  // Even though we only need those if we alias `@types/react` to types-react,
  // we still do it out of safety due to https://github.com/microsoft/DefinitelyTyped-tools/issues/433.
  const overrides: Record<string, string> = {}

  if (allDependencies['@types/react']) {
    overrides['@types/react'] = versionMapping['@types/react'].version
  }
  if (allDependencies['@types/react-dom']) {
    overrides['@types/react-dom'] = versionMapping['@types/react-dom'].version
  }

  writeOverridesField(appPackageJson, packageManager, overrides)

  for (const [packageName, { version, required }] of Object.entries(
    versionMapping
  )) {
    if (appPackageJson.devDependencies?.[packageName]) {
      devDependenciesToInstall.push([packageName, version])
    } else if (required || appPackageJson.dependencies?.[packageName]) {
      dependenciesToInstall.push([packageName, version])
    }
  }

  console.log(
    `Upgrading your project to ${pc.blue('Next.js ' + targetNextVersion)}...`
  )

  for (const [dep, version] of dependenciesToInstall) {
    addPackageDependency(appPackageJson, dep, version, false)
  }
  for (const [dep, version] of devDependenciesToInstall) {
    addPackageDependency(appPackageJson, dep, version, true)
  }

  fs.writeFileSync(
    appPackageJsonPath,
    JSON.stringify(appPackageJson, null, 2) +
      // Common IDE formatters would add a newline as well.
      os.EOL
  )

  runInstallation(packageManager, { cwd })

  for (const codemod of codemods) {
    await runTransform(codemod, cwd, { force: true, verbose })
  }

  // To reduce user-side burden of selecting which codemods to run as it needs additional
  // 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
    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`,
      { stdio: 'inherit' }
    )
  }

  if (shouldRunReactTypesCodemods) {
    // https://react.dev/blog/2024/04/25/react-19-upgrade-guide#typescript-changes
    // `--yes` skips prompts and applies all codemods automatically
    // https://github.com/eps1lon/types-react-codemod/blob/8463103233d6b70aad3cd6bee1814001eae51b28/README.md?plain=1#L52
    execSync(`${execCommand} types-react-codemod@latest --yes preset-19 .`, {
      stdio: 'inherit',
    })
  }
  console.log() // new line
  if (codemods.length > 0) {
    console.log(`${pc.green('βœ”')} Codemods have been applied successfully.`)
  }

  warnDependenciesOutOfRange(appPackageJson, versionMapping)

  endMessage()
}

function getInstalledNextVersion(): string {
  try {
    return require(
      require.resolve('next/package.json', {
        paths: [cwd],
      })
    ).version
  } catch (error) {
    throw new BadInput(
      `Failed to get the installed Next.js version at "${cwd}".\nIf you're using a monorepo, please run this command from the Next.js app directory.`,
      {
        cause: error,
      }
    )
  }
}

function getInstalledReactVersion(): string {
  try {
    return require(
      require.resolve('react/package.json', {
        paths: [cwd],
      })
    ).version
  } catch (error) {
    throw new BadInput(
      `Failed to detect the installed React version in "${cwd}".\nIf you're working in a monorepo, please run this command from the Next.js app directory.`,
      {
        cause: error,
      }
    )
  }
}

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.
 *
 * 1. If the dev script contains `--turbopack` option, we assume that Turbopack is
 *    already enabled.
 * 2. If the dev script contains the string `next dev`, we replace it to
 *    `next dev --turbopack`.
 * 3. Otherwise, we ask the user to manually add `--turbopack` to their dev command,
 *    showing the current dev command as the initial value.
 */
async function suggestTurbopack(
  packageJson: any,
  targetNextVersion: string
): Promise<void> {
  const devScript: string | undefined = packageJson.scripts?.['dev']
  // Turbopack flag was changed from `--turbo` to `--turbopack` in v15.0.1-canary.3
  // PR: https://github.com/vercel/next.js/pull/71657
  // Release: https://github.com/vercel/next.js/releases/tag/v15.0.1-canary.3
  const isAfterTurbopackFlagChange =
    compareVersions(targetNextVersion, '15.0.1-canary.3') >= 0
  const turboPackFlag = isAfterTurbopackFlagChange ? '--turbopack' : '--turbo'

  if (!devScript) {
    console.log(
      `${pc.yellow('⚠')} No "dev" script found in your package.json. Skipping Turbopack suggestion.`
    )
    return
  }

  if (devScript.includes('next dev')) {
    // covers "--turbopack" as well
    if (devScript.includes('--turbo')) {
      if (isAfterTurbopackFlagChange && !devScript.includes('--turbopack')) {
        console.log() // new line
        console.log(
          `${pc.green('βœ”')} Replaced "--turbo" with "--turbopack" in your dev script.`
        )
        console.log() // new line
        packageJson.scripts['dev'] = devScript.replace('--turbo', '--turbopack')
        return
      }
      return
    }

    const responseTurbopack = await prompts(
      {
        type: 'confirm',
        name: 'enable',
        message: `Enable Turbopack for ${pc.bold('next dev')}?`,
        initial: true,
      },
      { onCancel }
    )

    if (!responseTurbopack.enable) {
      return
    }

    packageJson.scripts['dev'] = devScript.replace(
      'next dev',
      `next dev ${turboPackFlag}`
    )
    return
  }

  console.log(
    `${pc.yellow('⚠')} Could not find "${pc.bold('next dev')}" in your dev script.`
  )

  const responseCustomDevScript = await prompts(
    {
      type: 'text',
      name: 'customDevScript',
      message: `Please manually add "${turboPackFlag}" to your dev command.`,
      initial: devScript,
    },
    { onCancel }
  )

  packageJson.scripts['dev'] =
    responseCustomDevScript.customDevScript || devScript
}

async function suggestCodemods(
  initialNextVersion: string,
  targetNextVersion: string
): Promise<string[]> {
  // example:
  // codemod version: 15.0.0-canary.45
  // 14.3             -> 15.0.0-canary.45: apply
  // 14.3             -> 15.0.0-canary.44: don't apply
  // 15.0.0-canary.44 -> 15.0.0-canary.45: apply
  // 15.0.0-canary.45 -> 15.0.0-canary.46: don't apply
  // 15.0.0-canary.45 -> 15.0.0          : don't apply
  // 15.0.0-canary.44 -> 15.0.0          : apply
  const initialVersionIndex = TRANSFORMER_INQUIRER_CHOICES.findIndex(
    (codemod) => {
      return compareVersions(codemod.version, initialNextVersion) > 0
    }
  )
  if (initialVersionIndex === -1) {
    return []
  }

  let targetVersionIndex = TRANSFORMER_INQUIRER_CHOICES.findIndex(
    (codemod) => compareVersions(codemod.version, targetNextVersion) > 0
  )
  if (targetVersionIndex === -1) {
    targetVersionIndex = TRANSFORMER_INQUIRER_CHOICES.length
  }

  const relevantCodemods = TRANSFORMER_INQUIRER_CHOICES.slice(
    initialVersionIndex,
    targetVersionIndex
  )

  if (relevantCodemods.length === 0) {
    return []
  }

  const { codemods } = await prompts(
    {
      type: 'multiselect',
      name: 'codemods',
      message: `The following ${pc.blue('codemods')} are recommended for your upgrade. Select the ones to apply.`,
      choices: relevantCodemods.reverse().map(({ title, value, version }) => {
        return {
          title: `(v${version}) ${value}`,
          description: title,
          value,
          selected: true,
        }
      }),
    },
    { onCancel }
  )

  return codemods
}

async function suggestReactCodemods(): Promise<boolean> {
  const { runReactCodemod } = await prompts(
    {
      type: 'confirm',
      name: 'runReactCodemod',
      message: 'Would you like to run the React 19 upgrade codemod?',
      initial: true,
    },
    { onCancel }
  )

  return runReactCodemod
}

async function suggestReactTypesCodemods(): Promise<boolean> {
  const { runReactTypesCodemod } = await prompts(
    {
      type: 'confirm',
      name: 'runReactTypesCodemod',
      message: 'Would you like to run the React 19 Types upgrade codemod?',
      initial: true,
    },
    { onCancel }
  )

  return runReactTypesCodemod
}

function writeOverridesField(
  packageJson: any,
  packageManager: PackageManager,
  overrides: Record<string, string>
) {
  const entries = Object.entries(overrides)
  // Avoids writing an empty overrides field into package.json
  // which would be an unnecessary diff.
  if (entries.length === 0) {
    return
  }

  if (packageManager === 'npm') {
    if (!packageJson.overrides) {
      packageJson.overrides = {}
    }
    for (const [key, value] of entries) {
      packageJson.overrides[key] = value
    }
  } else if (packageManager === 'pnpm') {
    // pnpm supports pnpm.overrides and pnpm.resolutions
    if (packageJson.resolutions) {
      for (const [key, value] of entries) {
        packageJson.resolutions[key] = value
      }
    } else {
      if (!packageJson.pnpm) {
        packageJson.pnpm = {}
      }
      if (!packageJson.pnpm.overrides) {
        packageJson.pnpm.overrides = {}
      }
      for (const [key, value] of entries) {
        packageJson.pnpm.overrides[key] = value
      }
    }
  } else if (packageManager === 'yarn') {
    if (!packageJson.resolutions) {
      packageJson.resolutions = {}
    }
    for (const [key, value] of entries) {
      packageJson.resolutions[key] = value
    }
  } else if (packageManager === 'bun') {
    // bun supports both overrides and resolutions
    // x-ref: https://bun.sh/docs/install/overrides
    if (packageJson.resolutions) {
      for (const [key, value] of entries) {
        packageJson.resolutions[key] = value
      }
    } else {
      // add overrides field if it's missing and add overrides
      if (!packageJson.overrides) {
        packageJson.overrides = {}
      }
      for (const [key, value] of entries) {
        packageJson.overrides[key] = value
      }
    }
  }
}

function warnDependenciesOutOfRange(
  appPackageJson: any,
  versionMapping: Record<string, { version: string; required: boolean }>
) {
  const allDirectDependencies = {
    ...appPackageJson.dependencies,
    ...appPackageJson.devDependencies,
  }

  const dependenciesOutOfRange = new Map<
    string,
    {
      [dependency: string]: {
        currentVersion: string
        expectedVersionRange: string
      }
    }
  >()

  const resolvedDependencyVersions = new Map<string, string>()
  for (const dependency of Object.keys(allDirectDependencies)) {
    let pkgJson

    // TODO: Asking package manager for the installed version is most robust e.g. `pnpm why ${dependency}`
    // require.resolve(`${dependency}/package.json`, { paths: [cwd] }) results in previously installed version being used in PNPM
    let pkgJsonFromNodeModules
    try {
      pkgJsonFromNodeModules = path.join(
        cwd,
        'node_modules',
        dependency,
        'package.json'
      )

      pkgJson = JSON.parse(fs.readFileSync(pkgJsonFromNodeModules, 'utf8'))
    } catch {
      console.warn(
        `${pc.yellow('⚠')} Could not find package.json for dependency "${dependency}" at "${pkgJsonFromNodeModules}". This may affect peer dependency checks.`
      )
      continue
    }

    resolvedDependencyVersions.set(dependency, pkgJson.version)

    if ('peerDependencies' in pkgJson) {
      const peerDeps = pkgJson.peerDependencies
      const peerDepsNames = Object.keys(peerDeps)
      const depsToCheck = Object.keys(versionMapping).filter(
        (versionMappingKey) => peerDepsNames.includes(versionMappingKey)
      )

      for (const depName of depsToCheck) {
        const expectedVersionRange = peerDeps[depName]
        const { version: currentVersion } = versionMapping[depName]
        if (
          !satisfiesVersionRange(currentVersion, expectedVersionRange, {
            includePrerelease: true,
          })
        ) {
          dependenciesOutOfRange.set(dependency, {
            ...dependenciesOutOfRange.get(dependency),
            [depName]: {
              currentVersion,
              expectedVersionRange,
            },
          })
        }
      }
    }
  }

  const size = dependenciesOutOfRange.size
  if (size > 0) {
    console.log(
      `${pc.yellow('⚠')} Found ${size} ${
        size === 1 ? 'dependency' : 'dependencies'
      } that seem incompatible with the upgraded package versions.\n` +
        'You may have to update these packages to their latest version or file an issue to ask for support of the upgraded libraries.'
    )
    dependenciesOutOfRange.forEach((deps, packageName) => {
      console.log(
        `${packageName} ${pc.gray(resolvedDependencyVersions.get(packageName))}`
      )
      Object.entries(deps).forEach(([depName, value], index, depsArray) => {
        const prefix = index === depsArray.length - 1 ? '  └── ' : '  β”œβ”€β”€ '
        console.log(
          `${prefix}${pc.yellow('βœ• unmet peer')} ${depName}@"${value.expectedVersionRange}": found ${value.currentVersion}`
        )
      })
    })
  }
}