UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

51 lines (44 loc) 1.93 kB
import { createRequire as __cjs_createRequire } from "node:module"; const __cjs_require = __cjs_createRequire(import.meta.url); const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process; const __cjs_getBuiltinModule = (module) => { // Check if we're in Node.js and version supports getBuiltinModule if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) { const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number); // Node.js 20.16.0+ and 22.3.0+ if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) { return __cjs_getProcess.getBuiltinModule(module); } } // Fallback to createRequire return __cjs_require(module); }; const { readFile } = __cjs_getBuiltinModule("node:fs/promises"); import { d as detectIndent } from './index-CHM-in-V.js'; import { R_OK } from './F_OK-BalxCn9n.js'; import isAccessible from './isAccessible-iOp0Bou6.js'; import writeFile from './writeFile-NT1fLEEe.js'; const writeJson = async (path, data, options = {}) => { const { detectIndent: detectIndent$1, indent: indentOption, replacer, stringify = JSON.stringify, ...writeOptions } = { indent: " ", ...options }; let indent = indentOption; let trailingNewline = "\n"; if (await isAccessible(path, R_OK)) { try { const file = await readFile(path, "utf8"); if (detectIndent$1) { const { indent: dIndent } = detectIndent(file); indent = dIndent; } trailingNewline = file.endsWith("\n") ? "\n" : ""; } catch (error) { if (error.code !== "ENOENT") { throw error; } } } const json = stringify(data, replacer, indent); await writeFile(path, `${json}${trailingNewline}`, writeOptions); }; export { writeJson as default };