-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
LichuAcu
committed
Sep 10, 2024
1 parent
fa53822
commit 8c99378
Showing
6 changed files
with
1,129 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Oops, something went wrong.