UNPKG

eunomia-controller

Version:

76 lines (67 loc) 3.65 kB
"use strict"; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var fs = require("fs"); var ejs = require("ejs"); var system = require("./../../variables")("./eunomia.json"); var isMissing = function isMissing(data, message) { return typeof data === 'undefined'; }; exports.generate = function (writeToPath) { return require("./../../task/index")("humans", function (resolve, reject) { if (!fs.existsSync("./eunomia.json")) { reject("eunomia.json required within the root of the project"); } else { var eunomia = system; if (!eunomia.generate.humans.enabled) { resolve(); return false; } // Structure Test if (isMissing(eunomia.generate)) { reject("The `eunomia.json` requires a `generate` json field"); } // Structure Humans Field Test else if (typeof eunomia.generate.humans === 'undefined') { reject("The `eunomia.json` requires a `generate.humans` json field"); } else if (_typeof(eunomia.generate.humans) !== 'object') { reject("The `generate.humans` in the `eunomia.json` needs to be a json object"); } // Company field within the humans json else if (isMissing(eunomia.generate.humans.company)) { reject("The `eunomia.json` requires a `generate.humans.company` json field"); } else if (typeof eunomia.generate.humans.company !== 'string') { reject("The `generate.humans.company` in the `eunomia.json` needs to be a string"); } // Developers field within the humans json else if (isMissing(eunomia.generate.humans.developers)) { reject("The `eunomia.json` requires a `generate.humans.developers` json field"); } else if (_typeof(eunomia.generate.humans.developers) !== 'object') { reject("The `generate.humans.developers` in the `eunomia.json` needs to be a json object"); } else if (eunomia.generate.humans.developers.filter(function (developer) { return typeof developer.title == 'undefined' || typeof developer.name == 'undefined' || typeof developer.age == 'undefined' || typeof developer.website == 'undefined'; }).length > 0) { reject("The `generate.humans.developers` array may only contains json objects that yields: title, name, age & website"); } // Company field within the humans json else if (isMissing(eunomia.generate.humans.technologies)) { reject("The `eunomia.json` requires a `generate.humans.technologies` json field"); } else if (_typeof(eunomia.generate.humans.technologies) !== 'object') { reject("The `generate.humans.technologies` in the `eunomia.json` needs to be a json object"); } else { try { var fullPath = __dirname + "/humans.ejs"; var template = fs.readFileSync(fullPath, "utf8"); var render = ejs.compile(template, { filename: fullPath }); var output = render({ eunomia: eunomia }); fs.writeFileSync(writeToPath + "/humans.txt", output, "utf8"); resolve(); } catch (e) { console.log(e); reject("Failed to create a humans.txt"); } } } }); };