Unverified Commit 58438e9e authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

refactor(next-upgrade): remove monorepo detect, add to error message when fail…

refactor(next-upgrade): remove monorepo detect, add to error message when fail to get next version (#70929)

### Why?

Instead of validating workspace, just look for next version on cwd. If fails, give warn to run on the nextjs app dir if using monorepo.
parent 64c414fd
......@@ -37,8 +37,6 @@ export async function runUpgrade(
const appPackageJsonPath = path.resolve(process.cwd(), 'package.json')
let appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, 'utf8'))
await detectWorkspace(appPackageJson)
let targetNextPackageJson: {
version: string
peerDependencies: Record<string, string>
......@@ -53,7 +51,7 @@ export async function runUpgrade(
)
}
const installedNextVersion = await getInstalledNextVersion()
const installedNextVersion = getInstalledNextVersion()
const targetNextVersion = targetNextPackageJson.version
......@@ -125,43 +123,23 @@ export async function runUpgrade(
)
}
async function detectWorkspace(appPackageJson: any): Promise<void> {
let isWorkspace =
appPackageJson.workspaces ||
fs.existsSync(path.resolve(process.cwd(), 'pnpm-workspace.yaml'))
if (!isWorkspace) return
console.log(
`${chalk.red('⚠️')} You seem to be in the root of a monorepo. ${chalk.blue('@next/upgrade')} should be run in a specific app directory within the monorepo.`
)
const response = await prompts(
{
type: 'confirm',
name: 'value',
message: 'Do you still want to continue?',
initial: false,
},
{ onCancel: () => process.exit(0) }
)
if (!response.value) {
process.exit(0)
function getInstalledNextVersion(): string {
try {
return require(
require.resolve('next/package.json', {
paths: [process.cwd()],
})
).version
} catch (error) {
throw new Error(
`Failed to get the installed Next.js version at "${process.cwd()}".\nIf you're using a monorepo, please run this command from the Next.js app directory.`,
{
cause: error,
}
)
}
}
async function getInstalledNextVersion(): Promise<string> {
const installedNextPackageJsonDir = require.resolve('next/package.json', {
paths: [process.cwd()],
})
const installedNextPackageJson = JSON.parse(
fs.readFileSync(installedNextPackageJsonDir, 'utf8')
)
return installedNextPackageJson.version
}
/*
* Heuristics are used to determine whether to Turbopack is enabled or not and
* to determine how to update the dev script.
......
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