UNPKG

430 BJavaScriptView Raw
1"use strict";
2
3const fs = require('fs');
4const writeLazySafeSync = require('./writeLazySafeSync');
5
6function patchJSON(target, cb, src) {
7 var entry = {};
8 try {
9 entry = JSON.parse(fs.readFileSync(src || target));
10 } catch(err) { }
11
12 let returned = cb(entry);
13
14 returned = JSON.stringify((returned === undefined) ? entry : returned, null, 2);
15 writeLazySafeSync(target, returned);
16}
17
18module.exports = patchJSON;