Skip to content

Commit e4a5770

Browse files
authored
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.
1 parent 236e001 commit e4a5770

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

packages/next-codemod/bin/upgrade.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ function endMessage() {
7171
)
7272
}
7373

74+
const cwd = process.cwd()
75+
7476
export async function runUpgrade(
7577
revision: string | undefined,
7678
options: { verbose: boolean }
7779
): Promise<void> {
7880
const { verbose } = options
79-
const appPackageJsonPath = path.resolve(process.cwd(), 'package.json')
81+
const appPackageJsonPath = path.resolve(cwd, 'package.json')
8082
let appPackageJson = JSON.parse(fs.readFileSync(appPackageJsonPath, 'utf8'))
8183

8284
let targetNextPackageJson: {
@@ -125,8 +127,8 @@ export async function runUpgrade(
125127
console.log(` - Next.js: v${installedNextVersion}`)
126128
let shouldStayOnReact18 = false
127129

128-
const usesAppDir = isUsingAppDir(process.cwd())
129-
const usesPagesDir = isUsingPagesDir(process.cwd())
130+
const usesAppDir = isUsingAppDir(cwd)
131+
const usesPagesDir = isUsingPagesDir(cwd)
130132

131133
const isPureAppRouter = usesAppDir && !usesPagesDir
132134
const isMixedApp = usesPagesDir && usesAppDir
@@ -182,7 +184,7 @@ export async function runUpgrade(
182184
installedNextVersion,
183185
targetNextVersion
184186
)
185-
const packageManager: PackageManager = getPkgManager(process.cwd())
187+
const packageManager: PackageManager = getPkgManager(cwd)
186188

187189
let shouldRunReactCodemods = false
188190
let shouldRunReactTypesCodemods = false
@@ -320,7 +322,7 @@ export async function runUpgrade(
320322
runInstallation(packageManager)
321323

322324
for (const codemod of codemods) {
323-
await runTransform(codemod, process.cwd(), { force: true, verbose })
325+
await runTransform(codemod, cwd, { force: true, verbose })
324326
}
325327

326328
// To reduce user-side burden of selecting which codemods to run as it needs additional
@@ -354,12 +356,12 @@ function getInstalledNextVersion(): string {
354356
try {
355357
return require(
356358
require.resolve('next/package.json', {
357-
paths: [process.cwd()],
359+
paths: [cwd],
358360
})
359361
).version
360362
} catch (error) {
361363
throw new BadInput(
362-
`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.`,
364+
`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.`,
363365
{
364366
cause: error,
365367
}
@@ -371,12 +373,12 @@ function getInstalledReactVersion(): string {
371373
try {
372374
return require(
373375
require.resolve('react/package.json', {
374-
paths: [process.cwd()],
376+
paths: [cwd],
375377
})
376378
).version
377379
} catch (error) {
378380
throw new BadInput(
379-
`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.`,
381+
`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.`,
380382
{
381383
cause: error,
382384
}

0 commit comments

Comments
 (0)