To run in Terminal: node path/to/converter.js path/to/source
fs = require("fs"); //Load two npm packages
path = require("path");
sourcePath = process.argv[2]; //Set filepaths
formattedPath = path.join(path.dirname(sourcePath), path.parse(sourcePath).name + "-Formatted" + path.extname(sourcePath));
talliedPath = path.join(path.dirname(sourcePath), path.parse(sourcePath).name + "-Tallied" + path.extname(sourcePath));
nameTally = {};
content = fs.readFileSync(sourcePath).toString().trim().split("\n\n"); //get, process, and write formatted content
content.forEach((thisEntry, entryCount) => {
thisEntry = thisEntry.split("\n");
let thisName = thisEntry[0];
nameTally[thisName] = nameTally[thisName] ? nameTally[thisName] + 1 : 1;
content[entryCount] = thisEntry.join(" -- ");
});
fs.writeFileSync(formattedPath, content.join("\n"));
nameTallyOutput = []; //prep & write the nameTally file
for (thisName in nameTally) content.push(thisName + ": " + nameTally[thisName]);
fs.writeFileSync(talliedPath, content.join("\n"));