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( ...@@ -37,8 +37,6 @@ export async function runUpgrade(
const appPackageJsonPath = path.resolve(process.cwd(), 'package.json') const appPackageJsonPath = path.resolve(process.cwd(), 'package.json')
let appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, 'utf8')) let appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, 'utf8'))
await detectWorkspace(appPackageJson)
let targetNextPackageJson: { let targetNextPackageJson: {
version: string version: string
peerDependencies: Record<string, string> peerDependencies: Record<string, string>
...@@ -53,7 +51,7 @@ export async function runUpgrade( ...@@ -53,7 +51,7 @@ export async function runUpgrade(
) )
} }
const installedNextVersion = await getInstalledNextVersion() const installedNextVersion = getInstalledNextVersion()
const targetNextVersion = targetNextPackageJson.version const targetNextVersion = targetNextPackageJson.version
...@@ -125,41 +123,21 @@ export async function runUpgrade( ...@@ -125,41 +123,21 @@ export async function runUpgrade(
) )
} }
async function detectWorkspace(appPackageJson: any): Promise<void> { function getInstalledNextVersion(): string {
let isWorkspace = try {
appPackageJson.workspaces || return require(
fs.existsSync(path.resolve(process.cwd(), 'pnpm-workspace.yaml')) require.resolve('next/package.json', {
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)
}
}
async function getInstalledNextVersion(): Promise<string> {
const installedNextPackageJsonDir = require.resolve('next/package.json', {
paths: [process.cwd()], paths: [process.cwd()],
}) })
const installedNextPackageJson = JSON.parse( ).version
fs.readFileSync(installedNextPackageJsonDir, 'utf8') } 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,
}
) )
}
return installedNextPackageJson.version
} }
/* /*
......
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