rsnext/packages/next/next-server/server/lib/recursive-readdir-sync.ts
Tim Neutkens 2ba352da39 Move next-server back into next package (#8613)
* Initial move

* Make emitting work

* Update paths

* Remove leftover files

* Add correct externals configuration

* Import correct path

* Update path to work with ts-server test

* Update lib directory

* Compile next-server/lib
2019-09-04 10:00:54 -04:00

29 lines
800 B
TypeScript

import fs from 'fs'
import { join } from 'path'
/**
* Recursively read directory
* @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 Array holding all relative paths
*/
export function recursiveReadDirSync(
dir: string,
arr: string[] = [],
rootDir = dir
): string[] {
const result = fs.readdirSync(dir)
result.forEach((part: string) => {
const absolutePath = join(dir, part)
const pathStat = fs.statSync(absolutePath)
if (pathStat.isDirectory()) {
recursiveReadDirSync(absolutePath, arr, rootDir)
return
}
arr.push(absolutePath.replace(rootDir, ''))
})
return arr
}