Unverified Commit 15879783 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[next-upgrade] Add `next upgrade` (#86120)

parent a63f1596
...@@ -12,10 +12,16 @@ related: ...@@ -12,10 +12,16 @@ related:
## Latest version ## Latest version
To update to the latest version of Next.js, you can use the `upgrade` codemod: To update to the latest version of Next.js, you can use the `upgrade` command:
```bash filename="Terminal" ```bash filename="Terminal"
npx @next/codemod@latest upgrade latest next upgrade
```
Next.js 15 and earlier do not support the `upgrade` command and need to use a separate package instead:
```bash filename="Terminal"
npx @next/codemod@canary upgrade latest
``` ```
If you prefer to upgrade manually, install the latest Next.js and React versions: If you prefer to upgrade manually, install the latest Next.js and React versions:
......
...@@ -34,6 +34,7 @@ The following commands are available: ...@@ -34,6 +34,7 @@ The following commands are available:
| [`info`](#next-info-options) | Prints relevant details about the current system which can be used to report Next.js bugs. | | [`info`](#next-info-options) | Prints relevant details about the current system which can be used to report Next.js bugs. |
| [`telemetry`](#next-telemetry-options) | Allows you to enable or disable Next.js' completely anonymous telemetry collection. | | [`telemetry`](#next-telemetry-options) | Allows you to enable or disable Next.js' completely anonymous telemetry collection. |
| [`typegen`](#next-typegen-options) | Generates TypeScript definitions for routes, pages, layouts, and route handlers without running a full build. | | [`typegen`](#next-typegen-options) | Generates TypeScript definitions for routes, pages, layouts, and route handlers without running a full build. |
| [`upgrade`](#next-upgrade-options) | Upgrades your Next.js application to the latest version. |
> **Good to know**: Running `next` without a command is an alias for `next dev`. > **Good to know**: Running `next` without a command is an alias for `next dev`.
...@@ -186,6 +187,19 @@ To ensure `next-env.d.ts` is present before type-checking run `next typegen`. Th ...@@ -186,6 +187,19 @@ To ensure `next-env.d.ts` is present before type-checking run `next typegen`. Th
> **Good to know**: `next typegen` loads your Next.js config (`next.config.js`, `next.config.mjs`, or `next.config.ts`) using the production build phase. Ensure any required environment variables and dependencies are available so the config can load correctly. > **Good to know**: `next typegen` loads your Next.js config (`next.config.js`, `next.config.mjs`, or `next.config.ts`) using the production build phase. Ensure any required environment variables and dependencies are available so the config can load correctly.
## `next upgrade` options
`next upgrade` upgrades your Next.js application to the latest version.
The following options are available for the `next upgrade` command:
| Option | Description |
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- |
| `-h, --help` | Show all available options. |
| `[directory]` | A directory with the Next.js application to upgrade. If not provided, the current directory will be used. |
| `--revision <revision>` | Specify a Next.js version or tag to upgrade to (e.g., `latest`, `canary`, `15.0.0`). Defaults to the release channel you have currently installed. |
| `--verbose` | Show verbose output during the upgrade process. |
## Examples ## Examples
### Debugging prerender errors ### Debugging prerender errors
......
...@@ -58,6 +58,8 @@ program ...@@ -58,6 +58,8 @@ program
.description( .description(
'Upgrade Next.js apps to desired versions with a single command.' 'Upgrade Next.js apps to desired versions with a single command.'
) )
// The CLI interface must be backwards compatible at all times so that older
// versions of Next.js can still run the codemod successfully.
.argument( .argument(
'[revision]', '[revision]',
'Specify the target Next.js version using an NPM dist tag (e.g. "latest", "canary", "rc", "beta") or an exact version number (e.g. "15.0.0").', 'Specify the target Next.js version using an NPM dist tag (e.g. "latest", "canary", "rc", "beta") or an exact version number (e.g. "15.0.0").',
......
...@@ -411,6 +411,36 @@ program ...@@ -411,6 +411,36 @@ program
) )
.usage('[directory] [options]') .usage('[directory] [options]')
const nextVersion = process.env.__NEXT_VERSION || 'unknown'
program
.command('upgrade')
.description(
'Upgrade Next.js apps to desired versions with a single command.'
)
.argument(
'[directory]',
`A Next.js project directory to upgrade. ${italic(
'If no directory is provided, the current directory will be used.'
)}`
)
.usage('[directory] [options]')
.option(
'--revision <revision>',
'Specify the target Next.js version using an NPM dist tag (e.g. "latest", "canary", "rc", "beta") or an exact version number (e.g. "15.0.0").',
nextVersion.includes('-canary.')
? 'canary'
: nextVersion.includes('-rc.')
? 'rc'
: nextVersion.includes('-beta.')
? 'beta'
: 'latest'
)
.option('--verbose', 'Verbose output', false)
.action(async (directory, options) => {
const mod = await import('../cli/next-upgrade.js')
mod.spawnNextUpgrade(directory, options)
})
program program
.command('experimental-test') .command('experimental-test')
.description( .description(
......
import { spawn } from 'child_process'
import { getProjectDir } from '../lib/get-project-dir'
import { getNpxCommand } from '../lib/helpers/get-npx-command'
interface NextUpgradeOptions {
revision: string
verbose: boolean
}
export function spawnNextUpgrade(
directory: string | undefined,
options: NextUpgradeOptions
) {
const baseDir = getProjectDir(directory)
const [upgradeProcessCommand, ...upgradeProcessDefaultArgs] =
getNpxCommand(baseDir).split(' ')
const upgradeProcessCommandArgs = [
...upgradeProcessDefaultArgs,
// Needs to be bleeding edge (canary) to pick up latest codemods.
'@next/codemod@canary',
'upgrade',
options.revision,
]
if (options.verbose) {
upgradeProcessCommandArgs.push('--verbose')
}
const upgradeProcess = spawn(
upgradeProcessCommand,
upgradeProcessCommandArgs,
{
stdio: 'inherit',
cwd: baseDir,
}
)
upgradeProcess.on('close', (code) => {
process.exitCode = code ?? 0
})
}
...@@ -3,13 +3,13 @@ import { getPkgManager } from './get-pkg-manager' ...@@ -3,13 +3,13 @@ import { getPkgManager } from './get-pkg-manager'
export function getNpxCommand(baseDir: string) { export function getNpxCommand(baseDir: string) {
const pkgManager = getPkgManager(baseDir) const pkgManager = getPkgManager(baseDir)
let command = 'npx' let command = 'npx --yes'
if (pkgManager === 'pnpm') { if (pkgManager === 'pnpm') {
command = 'pnpm dlx' command = 'pnpm --silent dlx'
} else if (pkgManager === 'yarn') { } else if (pkgManager === 'yarn') {
try { try {
execSync('yarn dlx --help', { stdio: 'ignore' }) execSync('yarn dlx --help', { stdio: 'ignore' })
command = 'yarn dlx' command = 'yarn --quiet dlx'
} catch {} } catch {}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment