Skip to content

Commit 9609fe4

Browse files
imdreamrunnerTimer
andcommitted
Replace fs.exists with fs.access (#7742)
* Replace deprecated fs.exists with fs.access. * Update packages/next/lib/file-exists.ts Co-Authored-By: Joe Haddad <[email protected]> * Update packages/next/lib/file-exists.ts Co-Authored-By: Joe Haddad <[email protected]>
1 parent fce7e4e commit 9609fe4

File tree

6 files changed

+32
-19
lines changed

6 files changed

+32
-19
lines changed

packages/next/build/flying-shuttle.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import path from 'path'
88
import { promisify } from 'util'
99

1010
import { recursiveDelete } from '../lib/recursive-delete'
11+
import { fileExists } from '../lib/file-exists'
1112
import * as Log from './output/log'
1213
import { PageInfo } from './utils'
1314

@@ -17,7 +18,6 @@ const DIR_FILES_NAME = 'files'
1718
const MAX_SHUTTLES = 3
1819

1920
const mkdirp = promisify(mkdirpModule)
20-
const fsExists = promisify(fs.exists)
2121
const fsReadFile = promisify(fs.readFile)
2222
const fsWriteFile = promisify(fs.writeFile)
2323
const fsCopyFile = promisify(fs.copyFile)
@@ -196,7 +196,7 @@ export class FlyingShuttle {
196196

197197
const found =
198198
this.shuttleBuildId &&
199-
(await fsExists(path.join(this.shuttleDirectory, CHUNK_GRAPH_MANIFEST)))
199+
(await fileExists(path.join(this.shuttleDirectory, CHUNK_GRAPH_MANIFEST)))
200200

201201
if (found) {
202202
Log.info('flying shuttle is docked')
@@ -260,7 +260,7 @@ export class FlyingShuttle {
260260
await Promise.all(
261261
[...allFiles].map(async file => {
262262
const filePath = path.join(path.dirname(this.pagesDirectory), file)
263-
const exists = await fsExists(filePath)
263+
const exists = await fileExists(filePath)
264264
if (!exists) {
265265
fileChanged.set(file, true)
266266
return
@@ -310,7 +310,7 @@ export class FlyingShuttle {
310310
DIR_FILES_NAME,
311311
'serverless/pages-manifest.json'
312312
)
313-
if (!(await fsExists(savedPagesManifest))) return
313+
if (!(await fileExists(savedPagesManifest))) return
314314

315315
const saved = JSON.parse(await fsReadFile(savedPagesManifest, 'utf8'))
316316
const currentPagesManifest = path.join(
@@ -355,7 +355,7 @@ export class FlyingShuttle {
355355
const filesExists = await Promise.all(
356356
files
357357
.map(f => path.join(this.shuttleDirectory, DIR_FILES_NAME, f))
358-
.map(f => fsExists(f))
358+
.map(f => fileExists(f))
359359
)
360360
if (!filesExists.every(Boolean)) {
361361
Log.warn(`unable to locate files for ${page} in shuttle`)
@@ -368,7 +368,7 @@ export class FlyingShuttle {
368368
files.map(async recallFileName => {
369369
if (!rewriteRegex.test(recallFileName)) {
370370
const recallPath = path.join(this.distDirectory, recallFileName)
371-
const recallPathExists = await fsExists(recallPath)
371+
const recallPathExists = await fileExists(recallPath)
372372

373373
if (!recallPathExists) {
374374
await mkdirp(path.dirname(recallPath))
@@ -391,7 +391,7 @@ export class FlyingShuttle {
391391
`${this.buildId}/`
392392
)
393393
const recallPath = path.join(this.distDirectory, newFileName)
394-
const recallPathExists = await fsExists(recallPath)
394+
const recallPathExists = await fileExists(recallPath)
395395
if (!recallPathExists) {
396396
await mkdirp(path.dirname(recallPath))
397397
await fsCopyFile(
@@ -428,7 +428,7 @@ export class FlyingShuttle {
428428
await mkdirp(this.shuttleDirectory)
429429

430430
const nextManifestPath = path.join(this.distDirectory, CHUNK_GRAPH_MANIFEST)
431-
if (!(await fsExists(nextManifestPath))) {
431+
if (!(await fileExists(nextManifestPath))) {
432432
Log.warn('could not find shuttle payload :: shuttle will not be docked')
433433
return
434434
}

packages/next/build/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import { promisify } from 'util'
1010
import { isValidElementType } from 'react-is'
1111
import prettyBytes from '../lib/pretty-bytes'
1212
import { recursiveReadDir } from '../lib/recursive-readdir'
13+
import { fileExists } from '../lib/file-exists'
1314
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
1415

1516
const fsStat = promisify(fs.stat)
16-
const fsExists = promisify(fs.exists)
1717
const fsReadFile = promisify(fs.readFile)
1818

1919
export function collectPages(
@@ -228,9 +228,9 @@ export async function getCacheIdentifier({
228228
const yarnLock = path.join(path.dirname(pkgPath), 'yarn.lock')
229229
const packageLock = path.join(path.dirname(pkgPath), 'package-lock.json')
230230

231-
if (await fsExists(yarnLock)) {
231+
if (await fileExists(yarnLock)) {
232232
selectivePageBuildingCacheIdentifier += await fsReadFile(yarnLock, 'utf8')
233-
} else if (await fsExists(packageLock)) {
233+
} else if (await fileExists(packageLock)) {
234234
selectivePageBuildingCacheIdentifier += await fsReadFile(
235235
packageLock,
236236
'utf8'

packages/next/build/webpack-config.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
} from 'next-server/constants'
99
import resolve from 'next/dist/compiled/resolve/index.js'
1010
import path from 'path'
11-
import { promisify } from 'util'
1211
import webpack from 'webpack'
1312

1413
import {
@@ -17,6 +16,7 @@ import {
1716
NEXT_PROJECT_ROOT_DIST_CLIENT,
1817
PAGES_DIR_ALIAS,
1918
} from '../lib/constants'
19+
import { fileExists } from '../lib/file-exists'
2020
import { WebpackEntrypoints } from './entries'
2121
import { AllModulesIdentifiedPlugin } from './webpack/plugins/all-modules-identified-plugin'
2222
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
@@ -33,8 +33,6 @@ import { ServerlessPlugin } from './webpack/plugins/serverless-plugin'
3333
import { SharedRuntimePlugin } from './webpack/plugins/shared-runtime-plugin'
3434
import { TerserPlugin } from './webpack/plugins/terser-webpack-plugin/src/index'
3535

36-
const fileExists = promisify(fs.exists)
37-
3836
type ExcludesFalse = <T>(x: T | false) => x is T
3937

4038
export default async function getBaseWebpackConfig(

packages/next/lib/file-exists.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import fs from 'fs'
2+
import { promisify } from 'util'
3+
4+
const access = promisify(fs.access)
5+
6+
export async function fileExists(fileName: string): Promise<boolean> {
7+
try {
8+
await access(fileName, fs.constants.F_OK)
9+
return true
10+
} catch (err) {
11+
if (err.code === 'ENOENT') {
12+
return false
13+
}
14+
throw err
15+
}
16+
}

packages/next/lib/verifyTypeScriptSetup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import path from 'path'
44
import chalk from 'chalk'
55
import { promisify } from 'util'
66
import { recursiveReadDir } from './recursive-readdir'
7+
import { fileExists } from './file-exists'
78
import resolve from 'next/dist/compiled/resolve/index.js'
89

9-
const exists = promisify(fs.exists)
1010
const writeFile = promisify(fs.writeFile)
1111

1212
function writeJson(fileName: string, object: object): Promise<void> {
@@ -96,8 +96,8 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
9696
const tsConfigPath = path.join(dir, 'tsconfig.json')
9797
const yarnLockFile = path.join(dir, 'yarn.lock')
9898

99-
const hasTsConfig = await exists(tsConfigPath)
100-
const isYarn = await exists(yarnLockFile)
99+
const hasTsConfig = await fileExists(tsConfigPath)
100+
const isYarn = await fileExists(yarnLockFile)
101101
const hasTypeScriptFiles = await hasTypeScript(dir)
102102
let firstTimeSetup = !hasTsConfig && hasTypeScriptFiles
103103

packages/next/server/hot-reloader.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ import { createPagesMapping, createEntrypoints } from '../build/entries'
1717
import { watchCompilers } from '../build/output'
1818
import { findPageFile } from './lib/find-page-file'
1919
import { recursiveDelete } from '../lib/recursive-delete'
20+
import { fileExists } from '../lib/file-exists'
2021
import { promisify } from 'util'
2122
import fs from 'fs'
2223

2324
const access = promisify(fs.access)
2425
const readFile = promisify(fs.readFile)
25-
// eslint-disable-next-line
26-
const fileExists = promisify(fs.exists)
2726

2827
export async function renderScriptError (res, error) {
2928
// Asks CDNs and others to not to cache the errored page

0 commit comments

Comments
 (0)