Unverified Commit b60c9437 authored by Nick Kelly's avatar Nick Kelly Committed by GitHub

Remove extraneous fs.stat calls (#27779)

## Performance improvement

- [x] Performance improvement

Uses `fs.readdir`'s  `withFileTypes` option, added in NodeJS version 10,  to simultaneously read the target directories files and their stats, removing the need to call `fs.stat` on each of the files individually.

Similar to #27769, applies the same change to `recursiveCopy` and `recursiveDelete`.
parent c6ba688c
import path from 'path'
import { promises, constants } from 'fs'
import { promises, constants, Dirent, Stats } from 'fs'
import { Sema } from 'next/dist/compiled/async-sema'
const COPYFILE_EXCL = constants.COPYFILE_EXCL
......@@ -23,13 +23,28 @@ export async function recursiveCopy(
const sema = new Sema(concurrency)
async function _copy(item: string): Promise<void> {
// deep copy the file/directory
async function _copy(item: string, lstats?: Stats | Dirent): Promise<void> {
const target = item.replace(from, to)
const stats = await promises.stat(item)
await sema.acquire()
if (stats.isDirectory()) {
if (!lstats) {
// after lock on first run
lstats = await promises.lstat(from)
}
// readdir & lstat do not follow symbolic links
// if part is a symbolic link, follow it with stat
let isFile = lstats.isFile()
let isDirectory = lstats.isDirectory()
if (lstats.isSymbolicLink()) {
const stats = await promises.stat(item)
isFile = stats.isFile()
isDirectory = stats.isDirectory()
}
if (isDirectory) {
try {
await promises.mkdir(target)
} catch (err) {
......@@ -39,10 +54,12 @@ export async function recursiveCopy(
}
}
sema.release()
const files = await promises.readdir(item)
await Promise.all(files.map((file) => _copy(path.join(item, file))))
const files = await promises.readdir(item, { withFileTypes: true })
await Promise.all(
files.map((file) => _copy(path.join(item, file.name), file))
)
} else if (
stats.isFile() &&
isFile &&
// before we send the path to filter
// we remove the base path (from) and replace \ by / (windows)
filter(item.replace(from, '').replace(/\\/g, '/'))
......@@ -53,6 +70,8 @@ export async function recursiveCopy(
overwrite ? undefined : COPYFILE_EXCL
)
sema.release()
} else {
sema.release()
}
}
......
import { promises } from 'fs'
import { Dirent, promises } from 'fs'
import { join } from 'path'
import { promisify } from 'util'
......@@ -41,7 +41,7 @@ export async function recursiveDelete(
): Promise<void> {
let result
try {
result = await promises.readdir(dir)
result = await promises.readdir(dir, { withFileTypes: true })
} catch (e) {
if (e.code === 'ENOENT') {
return
......@@ -50,17 +50,19 @@ export async function recursiveDelete(
}
await Promise.all(
result.map(async (part: string) => {
const absolutePath = join(dir, part)
const pathStat = await promises.stat(absolutePath).catch((e) => {
if (e.code !== 'ENOENT') throw e
})
if (!pathStat) {
return
result.map(async (part: Dirent) => {
const absolutePath = join(dir, part.name)
// readdir does not follow symbolic links
// if part is a symbolic link, follow it using stat
let isDirectory = part.isDirectory()
if (part.isSymbolicLink()) {
const stats = await promises.stat(absolutePath)
isDirectory = stats.isDirectory()
}
const pp = join(previousPath, part)
if (pathStat.isDirectory() && (!exclude || !exclude.test(pp))) {
const pp = join(previousPath, part.name)
if (isDirectory && (!exclude || !exclude.test(pp))) {
await recursiveDelete(absolutePath, exclude, pp)
return promises.rmdir(absolutePath)
}
......
......@@ -23,7 +23,15 @@ export async function recursiveReadDir(
const absolutePath = join(dir, part.name)
if (ignore && ignore.test(part.name)) return
if (part.isDirectory()) {
// readdir does not follow symbolic links
// if part is a symbolic link, follow it using stat
let isDirectory = part.isDirectory()
if (part.isSymbolicLink()) {
const stats = await promises.stat(absolutePath)
isDirectory = stats.isDirectory()
}
if (isDirectory) {
await recursiveReadDir(absolutePath, filter, ignore, arr, rootDir)
return
}
......
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