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