UNPKG

openapi-ts-generator

Version:

Based on swagger-ts-generator, this is a type script model generator specifically for services with OpenApi spec documentation.

44 lines (43 loc) 2.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseGenerator = void 0; var fs_1 = require("fs"); var handlebars_1 = require("handlebars"); var BaseGenerator = /** @class */ (function () { function BaseGenerator(generatorOptions, templateFilePath) { this.generatorOptions = generatorOptions; this.templateFilePath = templateFilePath; this.emptyArrayRegex = /, ]/g; if (templateFilePath) { var templateSource = (0, fs_1.readFileSync)(templateFilePath, { encoding: 'utf8' }); this.template = (0, handlebars_1.compile)(templateSource); } } BaseGenerator.prototype.generateFile = function (outputFilePath, context) { var _a, _b, _c, _d, _e, _f, _g, _h; if (!this.template) { (_a = this.generatorOptions.logger) === null || _a === void 0 ? void 0 : _a.warn("Template for ".concat(this.GeneratorName, " has not been specified.")); } else if (!context) { (_b = this.generatorOptions.logger) === null || _b === void 0 ? void 0 : _b.warn("Context for ".concat(this.GeneratorName, " has not been provided.")); } else { try { var content = this.template(context).replace(this.emptyArrayRegex, ']'); (0, fs_1.writeFileSync)(outputFilePath, content, { encoding: 'utf8' }); return content; } catch (err) { (_c = this.generatorOptions.logger) === null || _c === void 0 ? void 0 : _c.error("Error executing template: ".concat((_d = this.templateFilePath) !== null && _d !== void 0 ? _d : 'undefined', ".")); (_e = this.generatorOptions.logger) === null || _e === void 0 ? void 0 : _e.error("This is likely an issue with the template."); (_f = this.generatorOptions.logger) === null || _f === void 0 ? void 0 : _f.error("Data: ".concat(JSON.stringify(context))); (_g = this.generatorOptions.logger) === null || _g === void 0 ? void 0 : _g.error("Goto: https://github.com/ikemtz/openapi-ts-generator to report an issue if necessary."); (_h = this.generatorOptions.logger) === null || _h === void 0 ? void 0 : _h.error(JSON.stringify(err)); throw err; } } return null; }; return BaseGenerator; }()); exports.BaseGenerator = BaseGenerator;