UNPKG

buffer-apg-js

Version:

JavaScript APG, an ABNF Parser Generator

45 lines (40 loc) 1.56 kB
// Reads a JSON file defining the apglib.css colors and class names // Produces a JavaScript file which defines the class names // for use in utilities.js and other HTML output functions. module.exports = function (inFile, outFile) { "use strict"; function capFirst(str) { return str.charAt(0).toUpperCase() + str.slice(1); } function getFileExtension(filename) { return filename.slice(((filename.lastIndexOf(".") - 1) >>> 0) + 2); } let fs = require("fs"); try { /* validate arguments */ if (!inFile) { throw new Error("missing input JSON file (name.json)"); } if (getFileExtension(inFile) !== "json") { throw new Error("input JSON file (" + inFile + ") must have .json extension"); } if (!outFile) { throw new Error("missing output JavaScript file (name.js)"); } if (getFileExtension(outFile) !== "js") { throw new Error("output JavaScript file (" + outFile + ") must have .js extension"); } let json = fs.readFileSync(inFile, "utf8"); let obj = JSON.parse(json); let js = "module.exports = {\n"; js += "\n // Generated by apglib/style.js \n"; for (let key in obj.classes) { js += " CLASS_" + key.toUpperCase() + ": '" + obj.classes[key] + "',\n"; } js += "}\n"; fs.writeFileSync(outFile, js); } catch (e) { console.log("jsonToCLASS_.js: EXCEPTION: \n"); console.log(e.message); } };