update-json-file.js
safely and conveniently edit the contents of a JSON file
Quick Example
const updateJsonFile = const filePath = '/path/to/file/to/update.json'
API
type Updater = (value: any) => any | Promise<any>
updateJsonFile = (
filePath: string,
updater: Updater,
options?: any
) => Promise<void>
-
options
pass through to write-json-file -
by default, throws an error if the file does not already exist
-
"defaultValue" option swallows load/parse errors and calls updater as though file contained this value
-
"defaultValue" option can be a factory function, to help avoid mutation
-
your updater should avoid mutating the incoming data and return a clone instead (if necessary)
Examples
Avoiding mutation when defaultValue is the same object every time:
const updateJsonFile = const filePath = '/path/to/file/to/update.json'const options = defaultValue: {}
Avoiding mutation by passing a factory function as defaultValue:
const updateJsonFile = const filePath = '/path/to/file/to/update.json'const options = {}