UNPKG

@stryke/fs

Version:

A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.

2 lines (1 loc) 1.04 kB
import{findFilePath as t}from"@stryke/path";import{Buffer as i}from"node:buffer";import{existsSync as o,readFileSync as f,writeFileSync as n}from"node:fs";import{readFile as a,writeFile as s}from"node:fs/promises";import{createDirectory as d,createDirectorySync as w}from"./helpers";export async function readBufferFile(r){if(!r)throw new Error("No file path provided to read data");if(!o(r))throw new Error(`File does not exist at path: ${r}`);const e=await a(r);return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}export function readBufferFileSync(r){if(!r)throw new Error("No file path provided to read data");if(!o(r))throw new Error(`File does not exist at path: ${r}`);const e=f(r);return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}export async function writeBufferFile(r,e){if(!r)throw new Error("No file path provided to write data");o(t(r))||await d(t(r)),await s(r,i.from(e))}export function writeFileBufferSync(r,e){if(!r)throw new Error("No file path provided to write data");o(t(r))||w(t(r)),n(r,i.from(e))}