var FileHasher = require("./FileHasher");
if (process.argv.length < 3) {
console.log("Program file and one argument are required for this program.");
process.exit(1);
}
var filename = process.argv[2];
var fs = require('fs');
var file = fs.readFileSync(filename, "UTF-8"); // get file contents
var data = fs.readFileSync("../sha1.txt", "UTF-8"); // read current key-value pairs
console.log(data); // print all current key-value pairs
var filesha1 = FileHasher(file);
console.log(filesha1);
var object = {
name: filename,
value: filesha1
};
console.log(object);
fs.appendFile("../sha1.txt", JSON.stringify(object), function(err) {
console.log(err);
console.log(object); // prints object so I know it was stored correctly
});
Right now everything works fine. However, when saving the object to the text file, instead of getting this format:
{
"name":"sha1.js",
"value":"6d358b6f267e22e327c1028e79a5c8b200bf453d"
};
I get this:
{"name":"sha1.js","value":"6d358b6f267e22e327c1028e79a5c8b200bf453d"}
I want to change the spacing and add the semicolon when the object is saved. In the future I also plan on parsing this list to check whether or not a file already exists, and to change the value of the value upon an update in the sha1 of the file. A guiding hand towards the right way to format my code or object, or perhaps a different function I should be using, would be much appreciated!