@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
61 lines (59 loc) • 2.92 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
const require_helpers = require('./helpers.cjs');
let _stryke_path = require("@stryke/path");
let node_buffer = require("node:buffer");
let node_fs = require("node:fs");
let node_fs_promises = require("node:fs/promises");
//#region src/buffer.ts
/**
* Reads a file from the given path and returns its content as an ArrayBuffer. The file is expected to be located relative to the current directory of this module.
*
* @param filePath - The path to the file to read.
* @returns The content of the file as an ArrayBuffer.
*/
async function readFileBuffer(filePath) {
if (!filePath) throw new Error("No file path provided to read data");
if (!(0, node_fs.existsSync)(filePath)) throw new Error(`File does not exist at path: ${filePath}`);
const b = await (0, node_fs_promises.readFile)(filePath);
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
}
/**
* Reads a file from the given path and returns its content as an ArrayBuffer. The file is expected to be located relative to the current directory of this module.
*
* @param filePath - The path to the file to read.
* @returns The content of the file as an ArrayBuffer.
*/
function readFileBufferSync(filePath) {
if (!filePath) throw new Error("No file path provided to read data");
if (!(0, node_fs.existsSync)(filePath)) throw new Error(`File does not exist at path: ${filePath}`);
const b = (0, node_fs.readFileSync)(filePath);
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
}
/**
* Writes an ArrayBuffer to a file at the specified path.
*
* @param filePath - The path to the file where the data should be written.
* @param data - The ArrayBuffer containing the data to write.
*/
async function writeFileBuffer(filePath, data) {
if (!filePath) throw new Error("No file path provided to write data");
if (!(0, node_fs.existsSync)((0, _stryke_path.findFilePath)(filePath))) await require_helpers.createDirectory((0, _stryke_path.findFilePath)(filePath));
await (0, node_fs_promises.writeFile)(filePath, node_buffer.Buffer.from(data));
}
/**
* Writes an ArrayBuffer to a file at the specified path.
*
* @param filePath - The path to the file where the data should be written.
* @param data - The ArrayBuffer containing the data to write.
*/
function writeFileBufferSync(filePath, data) {
if (!filePath) throw new Error("No file path provided to write data");
if (!(0, node_fs.existsSync)((0, _stryke_path.findFilePath)(filePath))) require_helpers.createDirectorySync((0, _stryke_path.findFilePath)(filePath));
(0, node_fs.writeFileSync)(filePath, node_buffer.Buffer.from(data));
}
//#endregion
exports.readFileBuffer = readFileBuffer;
exports.readFileBufferSync = readFileBufferSync;
exports.writeFileBuffer = writeFileBuffer;
exports.writeFileBufferSync = writeFileBufferSync;