Skip to main content

FAQ

How do I apply the replace changes listed by the JSON output of diff command ?

Simply with a Nodejs script :

const _ = require('lodash');
const path = require('path');
const fs = require('fs').promises;

try {
// TODO replace with path to your file in develop or whatever branch
let originalFilePath = path.resolve(__dirname, "fr.json");
let jsonData = await fs.readFile(originalFilePath, 'utf8');
let currentObj = JSON.parse(jsonData);

// TODO replace with path to the file generated by diff command
let changesFilePath = path.resolve(__dirname, "diff_fr.json");
let jsonData2 = await fs.readFile(changesFilePath, 'utf8');
let changesFile = JSON.parse(jsonData2);
let changes = changesFile.changes;

// Proper variable for that
let result = Object.assign({}, currentObj);

// Add changed values
// (Feel free to edit the file generated by diff command if you want to override some changes)
for(let modifiedField of changes.filter(c => ["REPLACED"].includes(c.type)) ) {
_.set(result, modifiedField.key, modifiedField.newValue);
}

// write result
// TODO Add a path for destination
await fs.writeFile("", JSON.stringify(result, null, 4));

} catch(err) {
console.warn("Something bad happend");
console.error(err);
process.exit(1);
}