Files
pnpm/deps/status/src/safeStat.ts
Zoltan Kochan 5d5818e44f style: enforce node: protocol for builtin imports (#10951)
Add n/prefer-node-protocol rule and autofix all bare builtin imports
to use the node: prefix. Simplify the simple-import-sort builtins
pattern to just ^node: since all imports now use the prefix.
2026-03-13 07:59:51 +01:00

25 lines
624 B
TypeScript

import fs from 'node:fs'
import util from 'node:util'
export async function safeStat (filePath: string): Promise<fs.Stats | undefined> {
try {
return await fs.promises.stat(filePath)
} catch (error) {
if (util.types.isNativeError(error) && 'code' in error && error.code === 'ENOENT') {
return undefined
}
throw error
}
}
export function safeStatSync (filePath: string): fs.Stats | undefined {
try {
return fs.statSync(filePath)
} catch (error) {
if (util.types.isNativeError(error) && 'code' in error && error.code === 'ENOENT') {
return undefined
}
throw error
}
}