Skip to content

Instantly share code, notes, and snippets.

@gauthierm
Created July 26, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauthierm/8e5f18c78aa3ee9ebd6a63cb592acf8b to your computer and use it in GitHub Desktop.
Save gauthierm/8e5f18c78aa3ee9ebd6a63cb592acf8b to your computer and use it in GitHub Desktop.
const fs = require('fs');
// read
fs.readFile('composer.json', 'utf8', (err, contents) => {
if (err) {
console.log('File could not be read');
} else {
try {
// parse
const json = JSON.parse(contents);
// query + modify
if (json.name) {
console.log(`package name is ${json.name}`);
}
json.name = 'Modified Name';
// write
const newContents = JSON.stringify(json, null, 2);
fs.writeFile('composer.new.json', newContents, function(err) {
if (err) {
return console.log(err);
}
console.log('done');
});
} catch (e) {
console.log(`There was a syntax error in the JSON file ${e.message}`);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment