api-genrtr
Version:
An API generator for NodeJS & Express
100 lines (81 loc) • 3.01 kB
JavaScript
;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
/**
* Class Checker
* Checks if exists a JSON file with the information to build a API structure
*/
var fs = require('fs');
var logSymbols = require('log-symbols');
var FileGenerator = require('./FileGenerator');
var root = process.env.NODE_PATH;
var Checker =
/*#__PURE__*/
function () {
function Checker() {
_classCallCheck(this, Checker);
this.checkJSONExists();
this.checkStructure();
if (this.valid) {
var fileGenerator = new FileGenerator(this.configFile);
}
}
/**
* Method to check if exists config file
* @return {[type]} [description]
*/
_createClass(Checker, [{
key: "checkJSONExists",
value: function checkJSONExists() {
var pathToFile = "".concat(root, "/APIConfig.json");
if (fs.existsSync(pathToFile)) {
var config = fs.readFileSync(pathToFile, 'utf8');
this.configFile = JSON.parse(config);
} else {
console.error('There is not config file at', pathToFile);
}
}
/**
* method to ensure the right API json file configuration
* @param {Boolean} isValid Parameter to show message
* @param {String} what Parameter to describe why
*/
}, {
key: "validate",
value: function validate(isValid, what) {
if (isValid) {
this.valid = true;
console.log(logSymbols.success, "Valid configuration for ".concat(what));
} else {
this.valid = false;
console.error(logSymbols.error, "Invalid configuration for ".concat(what));
process.exit(0);
}
}
/**
* method to iterate over the API json file structure
*/
}, {
key: "checkStructure",
value: function checkStructure() {
var _this = this;
var json = this.configFile;
if (Object.prototype.hasOwnProperty.call(json, 'API')) {
var API = json.API;
var APIKeys = Object.keys(API);
APIKeys.forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(API[key], 'name') && Object.prototype.hasOwnProperty.call(API[key], 'db')) {
_this.validate(true, key);
} else {
_this.validate(false, key);
}
});
} else {
this.validate(false);
}
}
}]);
return Checker;
}();
module.exports = Checker;