rsnext/packages/next/lib/file-exists.ts
Tobias Koppers d78bb6fe46
upgrade to typescript 4.4.3 (#29112)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2021-09-16 18:06:57 +02:00

28 lines
668 B
TypeScript

import { constants, promises } from 'fs'
import isError from './is-error'
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 (
isError(err) &&
(err.code === 'ENOENT' || err.code === 'ENAMETOOLONG')
) {
return false
}
throw err
}
}