Created
December 18, 2019 23:53
-
-
Save mcnuttandrew/79302bd8025973d10d5bc30fc5520db4 to your computer and use it in GitHub Desktop.
Modify all json files in a folder
This file contains hidden or 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 {executeCommandLineCmd, getFile, writeFile} = require('hoopoe'); | |
// imagine you have a number of json files in a folder called showcase/data/ | |
executeCommandLineCmd('ls -a showcase/data').then(({stdout}) => { | |
// Build a list of desired file names | |
const fileNames = stdout.split('\n').filter(name => name.includes('.json')); | |
// Execute your actions over each of them | |
fileNames.forEach(fileName => { | |
// get the file | |
getFile(`./showcase/data/${fileName}`) | |
// parse it | |
.then(d => JSON.parse(d)) | |
.then(d => { | |
// make your modifications | |
d.EXAMPLE_PROPERTY = 'WOWZA!'; | |
// write the file back to where it was | |
writeFile(`./showcase/data/${fileName}`, JSON.stringify(d, null, 2)); | |
}); | |
}); | |
}); | |
// CAVEAT: this executes everything simulatenously, | |
// if you want do it in series then you can make use of hoopoe's executePromisesInSeries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment