@visulima/fs
Version:
Human friendly file system utilities for Node.js
34 lines (30 loc) • 1.22 kB
JavaScript
;
const promises = require('node:fs/promises');
const index = require('./index-ngvEMjBI.cjs');
const F_OK = require('./F_OK-CWSqQIdF.cjs');
const isAccessible = require('./isAccessible-ZNVGsSb7.cjs');
const writeFile = require('./writeFile-DKu5-M8x.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const writeJson = /* @__PURE__ */ __name(async (path, data, options = {}) => {
const { detectIndent, indent: indentOption, replacer, stringify = JSON.stringify, ...writeOptions } = { indent: " ", ...options };
let indent = indentOption;
let trailingNewline = "\n";
if (await isAccessible(path, F_OK.R_OK)) {
try {
const file = await promises.readFile(path, "utf8");
if (detectIndent) {
const { indent: dIndent } = index.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");
module.exports = writeJson;