Unverified Commit e4a57703 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

misc(next-upgrade): reuse process.cwd() value (#71558)

### Why?

Reuse `process.cwd` value. This is good for slight perf and future extensible when we add `cwd` option.
parent 236e0014
...@@ -71,12 +71,14 @@ function endMessage() { ...@@ -71,12 +71,14 @@ function endMessage() {
) )
} }
const cwd = process.cwd()
export async function runUpgrade( export async function runUpgrade(
revision: string | undefined, revision: string | undefined,
options: { verbose: boolean } options: { verbose: boolean }
): Promise<void> { ): Promise<void> {
const { verbose } = options const { verbose } = options
const appPackageJsonPath = path.resolve(process.cwd(), 'package.json') const appPackageJsonPath = path.resolve(cwd, 'package.json')
let appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, 'utf8')) let appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, 'utf8'))
let targetNextPackageJson: { let targetNextPackageJson: {
...@@ -125,8 +127,8 @@ export async function runUpgrade( ...@@ -125,8 +127,8 @@ export async function runUpgrade(
console.log(` - Next.js: v${installedNextVersion}`) console.log(` - Next.js: v${installedNextVersion}`)
let shouldStayOnReact18 = false let shouldStayOnReact18 = false
const usesAppDir = isUsingAppDir(process.cwd()) const usesAppDir = isUsingAppDir(cwd)
const usesPagesDir = isUsingPagesDir(process.cwd()) const usesPagesDir = isUsingPagesDir(cwd)
const isPureAppRouter = usesAppDir && !usesPagesDir const isPureAppRouter = usesAppDir && !usesPagesDir
const isMixedApp = usesPagesDir && usesAppDir const isMixedApp = usesPagesDir && usesAppDir
...@@ -182,7 +184,7 @@ export async function runUpgrade( ...@@ -182,7 +184,7 @@ export async function runUpgrade(
installedNextVersion, installedNextVersion,
targetNextVersion targetNextVersion
) )
const packageManager: PackageManager = getPkgManager(process.cwd()) const packageManager: PackageManager = getPkgManager(cwd)
let shouldRunReactCodemods = false let shouldRunReactCodemods = false
let shouldRunReactTypesCodemods = false let shouldRunReactTypesCodemods = false
...@@ -320,7 +322,7 @@ export async function runUpgrade( ...@@ -320,7 +322,7 @@ export async function runUpgrade(
runInstallation(packageManager) runInstallation(packageManager)
for (const codemod of codemods) { for (const codemod of codemods) {
await runTransform(codemod, process.cwd(), { force: true, verbose }) await runTransform(codemod, cwd, { force: true, verbose })
} }
// To reduce user-side burden of selecting which codemods to run as it needs additional // To reduce user-side burden of selecting which codemods to run as it needs additional
...@@ -354,12 +356,12 @@ function getInstalledNextVersion(): string { ...@@ -354,12 +356,12 @@ function getInstalledNextVersion(): string {
try { try {
return require( return require(
require.resolve('next/package.json', { require.resolve('next/package.json', {
paths: [process.cwd()], paths: [cwd],
}) })
).version ).version
} catch (error) { } catch (error) {
throw new BadInput( throw new BadInput(
`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.`, `Failed to get the installed Next.js version at "${cwd}".\nIf you're using a monorepo, please run this command from the Next.js app directory.`,
{ {
cause: error, cause: error,
} }
...@@ -371,12 +373,12 @@ function getInstalledReactVersion(): string { ...@@ -371,12 +373,12 @@ function getInstalledReactVersion(): string {
try { try {
return require( return require(
require.resolve('react/package.json', { require.resolve('react/package.json', {
paths: [process.cwd()], paths: [cwd],
}) })
).version ).version
} catch (error) { } catch (error) {
throw new BadInput( throw new BadInput(
`Failed to detect the installed React version in "${process.cwd()}".\nIf you're working in a monorepo, please run this command from the Next.js app directory.`, `Failed to detect the installed React version in "${cwd}".\nIf you're working in a monorepo, please run this command from the Next.js app directory.`,
{ {
cause: error, cause: error,
} }
......
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