Created
July 26, 2017 19:34
-
-
Save gauthierm/8e5f18c78aa3ee9ebd6a63cb592acf8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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