rsnext/packages/next/lib/recursive-readdir.ts
Admon Sasson 634b138b40 Make recursiveReadDir return sorted array (#7658)
This function is used to return the /pages folder content.
The way it is today is that every run might result in a different order of files, which makes the whole webpack chunks be different.
An ordered list of files will result in a deterministic build, which will improve caching.
2019-07-08 21:06:04 -04:00

46 lines
1.3 KiB
TypeScript

import fs from 'fs'
import { join } from 'path'
import { promisify } from 'util'
const readdir = promisify(fs.readdir)
const stat = promisify(fs.stat)
/**
* Recursively read directory
* @param {string} dir Directory to read
* @param {RegExp} filter Filter for the file name, only the name part is considered, not the full path
* @param {string[]=[]} arr This doesn't have to be provided, it's used for the recursion
* @param {string=dir`} rootDir Used to replace the initial path, only the relative path is left, it's faster than path.relative.
* @returns Promise array holding all relative paths
*/
export async function recursiveReadDir(
dir: string,
filter: RegExp,
ignore?: RegExp,
arr: string[] = [],
rootDir: string = dir
): Promise<string[]> {
const result = await readdir(dir)
await Promise.all(
result.map(async (part: string) => {
const absolutePath = join(dir, part)
if (ignore && ignore.test(part)) return
const pathStat = await stat(absolutePath)
if (pathStat.isDirectory()) {
await recursiveReadDir(absolutePath, filter, ignore, arr, rootDir)
return
}
if (!filter.test(part)) {
return
}
arr.push(absolutePath.replace(rootDir, ''))
})
)
return arr.sort()
}