2020-06-10 22:35:34 +02:00
|
|
|
import { constants, promises } from 'fs'
|
2019-07-17 08:01:21 +02:00
|
|
|
|
2020-06-10 22:35:34 +02:00
|
|
|
export async function fileExists(
|
|
|
|
fileName: string,
|
|
|
|
type?: 'file' | 'directory'
|
|
|
|
): Promise<boolean> {
|
2019-07-17 08:01:21 +02:00
|
|
|
try {
|
2020-06-10 22:35:34 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-07-17 08:01:21 +02:00
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|