Commit 9609fe45 authored by Xinzi Zhou's avatar Xinzi Zhou Committed by Joe Haddad

Replace fs.exists with fs.access (#7742)

* Replace deprecated fs.exists with fs.access.

* Update packages/next/lib/file-exists.ts
Co-Authored-By: 's avatarJoe Haddad <timer150@gmail.com>

* Update packages/next/lib/file-exists.ts
Co-Authored-By: 's avatarJoe Haddad <timer150@gmail.com>
parent fce7e4ea
......@@ -8,6 +8,7 @@ import path from 'path'
import { promisify } from 'util'
import { recursiveDelete } from '../lib/recursive-delete'
import { fileExists } from '../lib/file-exists'
import * as Log from './output/log'
import { PageInfo } from './utils'
......@@ -17,7 +18,6 @@ const DIR_FILES_NAME = 'files'
const MAX_SHUTTLES = 3
const mkdirp = promisify(mkdirpModule)
const fsExists = promisify(fs.exists)
const fsReadFile = promisify(fs.readFile)
const fsWriteFile = promisify(fs.writeFile)
const fsCopyFile = promisify(fs.copyFile)
......@@ -196,7 +196,7 @@ export class FlyingShuttle {
const found =
this.shuttleBuildId &&
(await fsExists(path.join(this.shuttleDirectory, CHUNK_GRAPH_MANIFEST)))
(await fileExists(path.join(this.shuttleDirectory, CHUNK_GRAPH_MANIFEST)))
if (found) {
Log.info('flying shuttle is docked')
......@@ -260,7 +260,7 @@ export class FlyingShuttle {
await Promise.all(
[...allFiles].map(async file => {
const filePath = path.join(path.dirname(this.pagesDirectory), file)
const exists = await fsExists(filePath)
const exists = await fileExists(filePath)
if (!exists) {
fileChanged.set(file, true)
return
......@@ -310,7 +310,7 @@ export class FlyingShuttle {
DIR_FILES_NAME,
'serverless/pages-manifest.json'
)
if (!(await fsExists(savedPagesManifest))) return
if (!(await fileExists(savedPagesManifest))) return
const saved = JSON.parse(await fsReadFile(savedPagesManifest, 'utf8'))
const currentPagesManifest = path.join(
......@@ -355,7 +355,7 @@ export class FlyingShuttle {
const filesExists = await Promise.all(
files
.map(f => path.join(this.shuttleDirectory, DIR_FILES_NAME, f))
.map(f => fsExists(f))
.map(f => fileExists(f))
)
if (!filesExists.every(Boolean)) {
Log.warn(`unable to locate files for ${page} in shuttle`)
......@@ -368,7 +368,7 @@ export class FlyingShuttle {
files.map(async recallFileName => {
if (!rewriteRegex.test(recallFileName)) {
const recallPath = path.join(this.distDirectory, recallFileName)
const recallPathExists = await fsExists(recallPath)
const recallPathExists = await fileExists(recallPath)
if (!recallPathExists) {
await mkdirp(path.dirname(recallPath))
......@@ -391,7 +391,7 @@ export class FlyingShuttle {
`${this.buildId}/`
)
const recallPath = path.join(this.distDirectory, newFileName)
const recallPathExists = await fsExists(recallPath)
const recallPathExists = await fileExists(recallPath)
if (!recallPathExists) {
await mkdirp(path.dirname(recallPath))
await fsCopyFile(
......@@ -428,7 +428,7 @@ export class FlyingShuttle {
await mkdirp(this.shuttleDirectory)
const nextManifestPath = path.join(this.distDirectory, CHUNK_GRAPH_MANIFEST)
if (!(await fsExists(nextManifestPath))) {
if (!(await fileExists(nextManifestPath))) {
Log.warn('could not find shuttle payload :: shuttle will not be docked')
return
}
......
......@@ -10,10 +10,10 @@ import { promisify } from 'util'
import { isValidElementType } from 'react-is'
import prettyBytes from '../lib/pretty-bytes'
import { recursiveReadDir } from '../lib/recursive-readdir'
import { fileExists } from '../lib/file-exists'
import { getPageChunks } from './webpack/plugins/chunk-graph-plugin'
const fsStat = promisify(fs.stat)
const fsExists = promisify(fs.exists)
const fsReadFile = promisify(fs.readFile)
export function collectPages(
......@@ -228,9 +228,9 @@ export async function getCacheIdentifier({
const yarnLock = path.join(path.dirname(pkgPath), 'yarn.lock')
const packageLock = path.join(path.dirname(pkgPath), 'package-lock.json')
if (await fsExists(yarnLock)) {
if (await fileExists(yarnLock)) {
selectivePageBuildingCacheIdentifier += await fsReadFile(yarnLock, 'utf8')
} else if (await fsExists(packageLock)) {
} else if (await fileExists(packageLock)) {
selectivePageBuildingCacheIdentifier += await fsReadFile(
packageLock,
'utf8'
......
......@@ -8,7 +8,6 @@ import {
} from 'next-server/constants'
import resolve from 'next/dist/compiled/resolve/index.js'
import path from 'path'
import { promisify } from 'util'
import webpack from 'webpack'
import {
......@@ -17,6 +16,7 @@ import {
NEXT_PROJECT_ROOT_DIST_CLIENT,
PAGES_DIR_ALIAS,
} from '../lib/constants'
import { fileExists } from '../lib/file-exists'
import { WebpackEntrypoints } from './entries'
import { AllModulesIdentifiedPlugin } from './webpack/plugins/all-modules-identified-plugin'
import BuildManifestPlugin from './webpack/plugins/build-manifest-plugin'
......@@ -33,8 +33,6 @@ import { ServerlessPlugin } from './webpack/plugins/serverless-plugin'
import { SharedRuntimePlugin } from './webpack/plugins/shared-runtime-plugin'
import { TerserPlugin } from './webpack/plugins/terser-webpack-plugin/src/index'
const fileExists = promisify(fs.exists)
type ExcludesFalse = <T>(x: T | false) => x is T
export default async function getBaseWebpackConfig(
......
import fs from 'fs'
import { promisify } from 'util'
const access = promisify(fs.access)
export async function fileExists(fileName: string): Promise<boolean> {
try {
await access(fileName, fs.constants.F_OK)
return true
} catch (err) {
if (err.code === 'ENOENT') {
return false
}
throw err
}
}
......@@ -4,9 +4,9 @@ import path from 'path'
import chalk from 'chalk'
import { promisify } from 'util'
import { recursiveReadDir } from './recursive-readdir'
import { fileExists } from './file-exists'
import resolve from 'next/dist/compiled/resolve/index.js'
const exists = promisify(fs.exists)
const writeFile = promisify(fs.writeFile)
function writeJson(fileName: string, object: object): Promise<void> {
......@@ -96,8 +96,8 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
const tsConfigPath = path.join(dir, 'tsconfig.json')
const yarnLockFile = path.join(dir, 'yarn.lock')
const hasTsConfig = await exists(tsConfigPath)
const isYarn = await exists(yarnLockFile)
const hasTsConfig = await fileExists(tsConfigPath)
const isYarn = await fileExists(yarnLockFile)
const hasTypeScriptFiles = await hasTypeScript(dir)
let firstTimeSetup = !hasTsConfig && hasTypeScriptFiles
......
......@@ -17,13 +17,12 @@ import { createPagesMapping, createEntrypoints } from '../build/entries'
import { watchCompilers } from '../build/output'
import { findPageFile } from './lib/find-page-file'
import { recursiveDelete } from '../lib/recursive-delete'
import { fileExists } from '../lib/file-exists'
import { promisify } from 'util'
import fs from 'fs'
const access = promisify(fs.access)
const readFile = promisify(fs.readFile)
// eslint-disable-next-line
const fileExists = promisify(fs.exists)
export async function renderScriptError (res, error) {
// Asks CDNs and others to not to cache the errored page
......
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