UNPKG

@stryke/fs

Version:

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

88 lines (86 loc) 3.21 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); const require_read_file = require('./read-file.cjs'); const require_write_file = require('./write-file.cjs'); let _stryke_path = require("@stryke/path"); let _stryke_type_checks = require("@stryke/type-checks"); let _stryke_json_storm_json = require("@stryke/json/storm-json"); let _stryke_type_checks_is_error = require("@stryke/type-checks/is-error"); //#region src/json.ts /** * Reads a JSON file and returns the object the JSON content represents. * * @param path - A path to a file. * @param options - JSON parse options * @returns Object the JSON content of the file represents */ function readJsonFileSync(path, options) { const content = require_read_file.readFileSync(path); try { return _stryke_json_storm_json.StormJSON.parse((0, _stryke_type_checks.isSetString)(content) ? content.trim() : "{}", { expectComments: (0, _stryke_path.findFileExtension)(path) === "jsonc", ...options }); } catch (error) { if ((0, _stryke_type_checks_is_error.isError)(error)) { error.message = error.message.replace("JSON", path); throw error; } throw new Error(`Failed to parse JSON: ${path}`); } } /** * Reads a JSON file and returns the object the JSON content represents. * * @param path - A path to a file. * @param options - JSON parse options * @returns Object the JSON content of the file represents */ async function readJsonFile(path, options = {}) { const content = await require_read_file.readFile(path); try { return _stryke_json_storm_json.StormJSON.parse((0, _stryke_type_checks.isSetString)(content) ? content.trim() : "{}", { expectComments: (0, _stryke_path.findFileExtension)(path) === "jsonc", ...options }); } catch (error) { if ((0, _stryke_type_checks_is_error.isError)(error)) { error.message = error.message.replace("JSON", path); throw error; } throw new Error(`Failed to parse JSON: ${path}`); } } /** * Serializes the given data to JSON and writes it to a file. * * @param path - A path to a file. * @param data - data which should be serialized to JSON and written to the file * @param options - JSON serialize options */ function writeJsonFileSync(path, data, options = {}) { const serializedJson = _stryke_json_storm_json.StormJSON.stringify(data, { spaces: 2, ...options }); return require_write_file.writeFileSync(path, options?.appendNewLine ? `${serializedJson}\n` : serializedJson); } /** * Serializes the given data to JSON and writes it to a file asynchronously. * * @param path - A path to a file. * @param data - data which should be serialized to JSON and written to the file * @param options - JSON serialize options */ async function writeJsonFile(path, data, options = {}) { const serializedJson = _stryke_json_storm_json.StormJSON.stringify(data, { spaces: 2, ...options }); return require_write_file.writeFile(path, options?.appendNewLine ? `${serializedJson}\n` : serializedJson); } //#endregion exports.readJsonFile = readJsonFile; exports.readJsonFileSync = readJsonFileSync; exports.writeJsonFile = writeJsonFile; exports.writeJsonFileSync = writeJsonFileSync;