So I wrote a script to save data from my project into JSON files, but I've been told it's clunky. I essentially just wrote three nearly-identical writeFile methods as such:
var fs = require('fs');
fs.writeFile('public/game-data.json', JSON.stringify(getGameData()), (e: any) => {
if(e){
console.error(e);
return;
};
console.log("Saved game data to game-data.json.");
});
fs.writeFile('public/shop-items.json', JSON.stringify(getShopData()), (e: any) => {
if(e){
console.error(e);
return;
};
console.log("Saved shop data to shop-data.json.");
});
fs.writeFile('public/hash.json', JSON.stringify(getHashData()), (e: any) => {
if(e){
console.error(e);
return;
};
console.log("Saved hash data to hash.json.");
});
I was recommended to use a loop and a "trustable function" (can't find the definition of that anywhere)... Anybody have any recommendations?
dataandfileName, then refactor all three functions to be calls likemyWritFile(getGameData(), 'game-data.json')?writeData, that you called like this:writeData('game', 'game-data.json', getGameData()), thenwriteData('shop', 'shop-items.json', getShopData()), and again for hash. How would you go about writing it?