deepify
Version:
DEEP Development Tools
169 lines (135 loc) • 3.98 kB
JavaScript
/**
* Created by CCristi <ccovali@mitocgroup.com> on 4/25/16.
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.AbstractGenerator = undefined;
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _deepCore = require('deep-core');
var _deepCore2 = _interopRequireDefault(_deepCore);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _joi = require('joi');
var _joi2 = _interopRequireDefault(_joi);
var _TwigEngine = require('./TemplatingEngine/TwigEngine');
var _InvalidGenerationSchema = require('./Exception/InvalidGenerationSchema');
var _MissingTemplateException = require('./Exception/MissingTemplateException');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Abstract Generator
*/
class AbstractGenerator extends _deepCore2.default.OOP.Interface {
/**
* @param {Object} templatingEngine
* @param {String} skeletonsDirectory
*/
constructor(templatingEngine = AbstractGenerator.TWIG_TEMPLATING, skeletonsDirectory = AbstractGenerator.DEFAULT_SKELETONS_DIR) {
super('validationSchema', '_generate');
this._templatingEngine = templatingEngine;
this._skeletonsDirectory = skeletonsDirectory;
this._targetPath = null;
this._generationSchema = null;
}
/**
* @returns {String}
*/
get skeletonsDirectory() {
return this._skeletonsDirectory;
}
/**
* @returns {Object}
*/
get templatingEngine() {
return this._templatingEngine;
}
/**
* @returns {String|null}
*/
get targetPath() {
return this._targetPath;
}
/**
* @returns {Object|null}
*/
get generationSchema() {
return this._generationSchema;
}
/**
* @param {String} template
* @param {Object} params
* @returns {Object}
*/
render(template, params = {}) {
if (!this.templateExists(template)) {
throw new _MissingTemplateException.MissingTemplateException(template);
}
let templateContent = _fs2.default.readFileSync(this.templatePath(template)).toString();
return this.templatingEngine.render(templateContent, params);
}
/**
* @param {String} template
* @param {String} targetFile
* @param {Object} params
*/
renderFile(template, targetFile, params = {}) {
_fs2.default.writeFileSync(targetFile, this.render(template, params));
}
/**
* @param {String} targetDir
* @param {Object} generationSchema
* @param {Function} cb
*/
generate(targetDir, generationSchema, cb = () => {}) {
let validationResult = _joi2.default.validate(generationSchema, this.validationSchema(), {
stripUnknown: true,
convert: true,
abortEarly: false
});
if (validationResult.error) {
cb(new _InvalidGenerationSchema.InvalidGenerationSchema(this.constructor.name, validationResult.error));
return;
}
this._targetPath = targetDir;
this._generationSchema = validationResult.value;
this._generate(cb);
}
/**
* @param {String} template
* @returns {String}
*/
templatePath(template) {
return _path2.default.join(this._skeletonsDirectory, `${template}${this._templatingEngine.extension()}`);
}
/**
* @param {String} template
* @returns {Boolean}
*/
templateExists(template) {
let fullPath = this.templatePath(template);
return _fs2.default.existsSync(fullPath) && _fs2.default.lstatSync(fullPath).isFile();
}
/**
* @returns {TwigEngine}
*/
static get TWIG_TEMPLATING() {
return new _TwigEngine.TwigEngine();
}
/**
* @note: If you want to change this update hooks/install_templates.sh also
* @returns {String}
*/
static get DEFAULT_SKELETONS_DIR() {
return _path2.default.join(__dirname, '../../resources/skeletons');
}
/**
* @returns {RegExp}
*/
static get DEEP_NAME_REGEXP() {
return (/^[a-zA-Z0-9_\-]{2,}$/
);
}
}
exports.AbstractGenerator = AbstractGenerator;