Skip to content

Commit

Permalink
@next/upgrade package
Browse files Browse the repository at this point in the history
  • Loading branch information
LichuAcu committed Sep 10, 2024
1 parent fa53822 commit 8c99378
Show file tree
Hide file tree
Showing 6 changed files with 1,129 additions and 121 deletions.
15 changes: 15 additions & 0 deletions packages/next-upgrade/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @next/upgrade

Upgrade Next.js apps to newer or beta versions with one command.

To build the package:

```bash
pnpm build
```

To link the package to your local Next.js repo:

```bash
pnpm link --global
```
87 changes: 87 additions & 0 deletions packages/next-upgrade/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env node

import prompts from 'prompts'
import fs from 'fs'
import { execSync } from 'child_process'
import path from 'path'
import { compareVersions } from 'compare-versions'

type Version = 'rc' | 'latest' | 'canary'

interface Response {
version: Version
}

async function run(): Promise<void> {
let version: Version

const specifiedVersion = process.argv[2]?.replace('@', '')
if (['rc', 'latest', 'canary'].includes(specifiedVersion)) {
// Shortcut
version = specifiedVersion as Version
} else {
const response: Response = await prompts({
type: 'select',
name: 'version',
message: 'What version of Next.js do you want to upgrade to?',
choices: [
{ title: 'Latest Release Candidate', value: 'rc' },
{ title: 'Latest Stable', value: 'latest' },
{ title: 'Canary', value: 'canary' },
],
initial: 0,
})
version = response.version
}

// TODO(LichuAcu): support Turborepo
const packageJsonPath = path.resolve(process.cwd(), 'package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))

packageJson.dependencies['next'] = version

try {
const res = await fetch(`https://registry.npmjs.org/next/${version}`)
const data = await res.json()
if (data.peerDependencies) {
packageJson.dependencies['react'] = data.peerDependencies['react']
packageJson.dependencies['react-dom'] = data.peerDependencies['react-dom']
}

if (data.version) {
if (compareVersions(data.version, '15.0.0-canary') >= 0) {
const responseTurbopack = await prompts({
type: 'confirm',
name: 'enable',
message:
'Turbopack is stable for dev mode in this Next.js version. Do you want to enable it?',
initial: true,
})
if (responseTurbopack.enable) {
if (!packageJson.scripts['dev'].includes('--turbo')) {
packageJson.scripts['dev'] += ' --turbo'
}
}
}
}
} catch (error) {
console.error(
'Failed to fetch versions from npm registry. Only `next` version will be updated. You might need to upgrade `react` and `react-dom` manually.'
)
}

// TODO(LichuAcu): keep the original spacing of the file
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
console.log(
'Your package.json has been updated successfully. Dependencies will now be updated.'
)

// TODO(LichuAcu): use the right package manager
execSync('pnpm install', { stdio: 'inherit' })

console.log(
'Your Next.js project has been updated successfully. Time to ship!'
)
}

run().catch(console.error)
34 changes: 34 additions & 0 deletions packages/next-upgrade/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@next/upgrade",
"version": "1.0.0",
"description": "Upgrade Next.js apps to newer or beta versions with one command",
"main": "dist/index.js",
"bin": {
"@next/upgrade": "./dist/index.js"
},
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-upgrade"
},
"keywords": [
"next",
"next.js"
],
"author": "Next.js Team <support@vercel.com>",
"license": "MIT",
"dependencies": {
"compare-versions": "6.1.1",
"prompts": "^2.4.2"
},
"devDependencies": {
"@types/node": "^14.14.31",
"@types/prompts": "^2.0.14",
"typescript": "^4.3.5"
},
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "tsc && node dist/index.js"
},
"type": "module"
}
14 changes: 14 additions & 0 deletions packages/next-upgrade/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "es2018",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist"
},
"include": ["index.ts"],
"exclude": ["node_modules", "test-project"]
}
Loading

0 comments on commit 8c99378

Please sign in to comment.