UNPKG

@augment-vir/node

Version:

A collection of augments, helpers types, functions, and classes only for Node.js (backend) JavaScript environments.

19 lines (17 loc) 592 B
import {existsSync} from 'node:fs'; import {readFile} from 'node:fs/promises'; /** * Reads a file if it exists, or just return `undefined`. * * @category Node : File * @category Package : @augment-vir/node * @returns The file contents as a string if the file exists, otherwise `undefined`. * @package [`@augment-vir/node`](https://www.npmjs.com/package/@augment-vir/node) */ export async function readFileIfExists(path: string): Promise<string | undefined> { if (existsSync(path)) { return (await readFile(path)).toString(); } else { return undefined; } }