UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

32 lines (29 loc) 1.21 kB
import { readFile } from 'node:fs/promises'; import { d as detectIndent } from './index-ajT9ecry.mjs'; import { R_OK } from './F_OK-JER1LjUr.mjs'; import isAccessible from './isAccessible-Bonsm1Ez.mjs'; import writeFile from './writeFile-B7dhfseN.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const writeJson = /* @__PURE__ */ __name(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); }, "writeJson"); export { writeJson as default };