UNPKG

@visulima/fs

Version:

Human friendly file system utilities for Node.js

32 lines (29 loc) 1.22 kB
import { readFileSync } from 'node:fs'; import { d as detectIndent } from './index-ajT9ecry.mjs'; import { R_OK } from './F_OK-JER1LjUr.mjs'; import isAccessibleSync from './isAccessibleSync-3gnUWUwX.mjs'; import writeFileSync from './writeFileSync-mMGYqOc8.mjs'; var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const writeJsonSync = /* @__PURE__ */ __name((path, data, options = {}) => { const { detectIndent: detectIndent$1, indent: indentOption, replacer, stringify = JSON.stringify, ...writeOptions } = { indent: " ", ...options }; let indent = indentOption; let trailingNewline = "\n"; if (isAccessibleSync(path, R_OK)) { try { const file = readFileSync(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); writeFileSync(path, `${json}${trailingNewline}`, writeOptions); }, "writeJsonSync"); export { writeJsonSync as default };