UNPKG

@thi.ng/file-io

Version:

Assorted file I/O utils (w/ logging support) for NodeJS/Bun

29 lines (28 loc) 963 B
import { readText, readTextAsync, writeText, writeTextAsync } from "./text.js"; const readJSON = (path, logger) => JSON.parse(readText(path, logger)); const readJSONAsync = async (path, logger) => JSON.parse(await readTextAsync(path, logger)); const readJSONC = (path, logger) => __parseJSONC(readText(path, logger)); const readJSONCAsync = async (path, logger) => __parseJSONC(await readTextAsync(path, logger)); const __parseJSONC = (src) => JSON.parse( src.split(/\r?\n/g).filter((x) => !/^\s*\/\//.test(x)).join("\n") ); const writeJSON = (path, obj, replacer, space, logger, dryRun = false) => writeText( path, JSON.stringify(obj, replacer, space) + "\n", logger, dryRun ); const writeJSONAsync = (path, obj, replacer, space, logger, dryRun = false) => writeTextAsync( path, JSON.stringify(obj, replacer, space) + "\n", logger, dryRun ); export { readJSON, readJSONAsync, readJSONC, readJSONCAsync, writeJSON, writeJSONAsync };