rsnext/packages/next/lib/file-exists.ts
Joe Haddad 91664d1d02
Eliminate public folder enumeration (#14042)
This builds off of @timneutkens's PR instead of updating it because it's his `canary` branch.

Updated to still `stat`, as it's the only way to test the difference between a file and directory. 

---

Closes #13506
Fixes #12235
2020-06-10 20:35:34 +00:00

24 lines
568 B
TypeScript

import { constants, promises } from 'fs'
export async function fileExists(
fileName: string,
type?: 'file' | 'directory'
): Promise<boolean> {
try {
if (type === 'file') {
const stats = await promises.stat(fileName)
return stats.isFile()
} else if (type === 'directory') {
const stats = await promises.stat(fileName)
return stats.isDirectory()
} else {
await promises.access(fileName, constants.F_OK)
}
return true
} catch (err) {
if (err.code === 'ENOENT') {
return false
}
throw err
}
}