@visulima/fs
Version:
Human friendly file system utilities for Node.js
34 lines (30 loc) • 1.23 kB
JavaScript
;
const node_fs = require('node:fs');
const index = require('./index-ngvEMjBI.cjs');
const F_OK = require('./F_OK-CWSqQIdF.cjs');
const isAccessibleSync = require('./isAccessibleSync-VWjQa-US.cjs');
const writeFileSync = require('./writeFileSync-Db6pmY0q.cjs');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const writeJsonSync = /* @__PURE__ */ __name((path, data, options = {}) => {
const { detectIndent, indent: indentOption, replacer, stringify = JSON.stringify, ...writeOptions } = { indent: " ", ...options };
let indent = indentOption;
let trailingNewline = "\n";
if (isAccessibleSync(path, F_OK.R_OK)) {
try {
const file = node_fs.readFileSync(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);
writeFileSync(path, `${json}${trailingNewline}`, writeOptions);
}, "writeJsonSync");
module.exports = writeJsonSync;