Skip to content

Commit

Permalink
refactor(next-upgrade): remove monorepo detect, add to error message …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
devjiwonchoi authored Oct 8, 2024
1 parent 64c414f commit 58438e9
Showing 1 changed file with 15 additions and 37 deletions.
52 changes: 15 additions & 37 deletions packages/next-codemod/bin/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -53,7 +51,7 @@ export async function runUpgrade(
)
}

const installedNextVersion = await getInstalledNextVersion()
const installedNextVersion = getInstalledNextVersion()

const targetNextVersion = targetNextPackageJson.version

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 58438e9

Please sign in to comment.