UNPKG

@stryke/fs

Version:

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

41 lines (39 loc) 1.98 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); const require_exists = require('./exists.cjs'); const require_helpers = require('./helpers.cjs'); let node_fs = require("node:fs"); let node_fs_promises = require("node:fs/promises"); let _stryke_path_correct_path = require("@stryke/path/correct-path"); let _stryke_path_file_path_fns = require("@stryke/path/file-path-fns"); //#region src/write-file.ts /** * Write the given content to the given file path * * @param filePath - The file path to write to * @param content - The content to write to the file */ const writeFileSync = (filePath, content = "", options = {}) => { if (!filePath) throw new Error("No file path provided to write data"); const directory = (0, _stryke_path_file_path_fns.findFilePath)((0, _stryke_path_correct_path.correctPath)(filePath)); if (!require_exists.existsSync(directory)) if (options.createDirectory !== false) require_helpers.createDirectorySync(directory); else throw new Error(`Directory ${directory} does not exist`); (0, node_fs.writeFileSync)(filePath, content || "", options); }; /** * Read the given content to the given file path * * @param filePath - The file path to read to * @param content - The content to write to the file * @returns The content of the file */ const writeFile = async (filePath, content = "", options = {}) => { if (!filePath) throw new Error("No file path provided to read data"); const directory = (0, _stryke_path_file_path_fns.findFilePath)((0, _stryke_path_correct_path.correctPath)(filePath)); if (!require_exists.existsSync(directory)) if (options.createDirectory !== false) await require_helpers.createDirectory(directory); else throw new Error(`Directory ${directory} does not exist`); return (0, node_fs_promises.writeFile)(filePath, content || "", options); }; //#endregion exports.writeFile = writeFile; exports.writeFileSync = writeFileSync;