UNPKG

eunomia-controller

Version:

71 lines (68 loc) 3.01 kB
"use strict"; var fs = require("fs"); var ejs = require("ejs"); var eunomia = require("./../../variables")("./eunomia.json"); var isUndefined = require("../../common").isUndefined; // Generate the HTML content for the browserConfig file var generateHTML = function generateHTML(__ld) { return isUndefined(__ld) ? "" : __ld.map(function (__d) { var body = function body(__bd) { return typeof __bd === 'string' ? __bd : generateHTML(__bd); }; return "<" + __d.id + (typeof __d.attributes === 'undefined' ? "" : " " + __d.attributes.map(function (attr) { return attr[0] + "=\"" + attr[1] + "\""; }).join(" ")) + ">" + body(__d.body) + "</" + __d.id + ">"; }).join(""); }; exports.generate = function (writeToPath) { try { // Is this module enabled if (!eunomia.generate.browserconfig.enabled) { // Create a dummy promise that instant resolves to continue the flow return Promise.resolve().then(function () { return new Promise(function (res, rej) { return res(); }); }); } else { // Retrieve the task function - it create a pretty promise with finish text and time return require("./../../task/index")("browserconfig", function (resolve, reject) { // Make sure the eunomia.json file exists if (!fs.existsSync("./eunomia.json")) { reject("eunomia.json required within the root of the project"); } else { // Make sure the browserconfig json field exists - if not the browserconfig will not be generated if (isUndefined(eunomia.generate.browserconfig)) resolve();else { try { // Full path to the ejs the browserconfig will use var fullPath = __dirname + "/browserconfig.ejs"; // Get the ejs content var template = fs.readFileSync(fullPath, "utf8"); // Render the json content into the ejs content var render = ejs.compile(template, { filename: fullPath }); var output = render({ content: generateHTML(eunomia.generate.browserconfig.body) }); // Write the result of the combined json and ejs into the respective file fs.writeFileSync(writeToPath + "/browserconfig.xml", output, "utf8"); // Resolve the promise to continue the flow resolve(); } catch (e) { reject("Failed to create a browserconfig.xml"); } } } }); } } // If for some reason something goes wrong - most common the chmod permission is incorrect it will fall in this catch catch (e) { console.log("Error: Eunomia - Generating the `browserconfig` file"); return Promise.resolve().then(function () { return new Promise(function (res, rej) { return res(); }); }).catch(function (error) { console.log("Advanced Error: Eunomia - Generating the `browserconfig` file && try, catch Promise"); new Error(error); }); } };