ng-openapi-gen
Version:
An OpenAPI 3.0 and 3.1 codegen for Angular 16+
67 lines • 2.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Templates = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
/**
* Holds all templates, and know how to apply them
*/
class Templates {
constructor(builtInDir, customDir, handlebars) {
this.templates = {};
this.globals = {};
const builtInTemplates = fs_1.default.readdirSync(builtInDir);
const customTemplates = customDir === '' ? [] : fs_1.default.readdirSync(customDir);
// Read all built-in templates, but taking into account an override, if any
for (const file of builtInTemplates) {
const dir = customTemplates.includes(file) ? customDir : builtInDir;
this.parseTemplate(dir, file, handlebars);
}
// Also read any custom templates which are not built-in
for (const file of customTemplates) {
this.parseTemplate(customDir, file, handlebars);
}
}
parseTemplate(dir, file, handlebars) {
const baseName = this.baseName(file);
if (baseName) {
const text = fs_1.default.readFileSync(path_1.default.join(dir, file), 'utf-8');
const compiled = handlebars.compile(text);
this.templates[baseName] = compiled;
handlebars.registerPartial(baseName, compiled);
}
}
/**
* Sets a global variable, that is, added to the model of all templates
*/
setGlobals(globals) {
for (const name of Object.keys(globals)) {
const value = globals[name];
this.globals[name] = value;
}
}
baseName(file) {
if (!file.endsWith('.handlebars')) {
return null;
}
return file.substring(0, file.length - '.handlebars'.length);
}
/**
* Applies a template with a given model
* @param templateName The template name (file without .handlebars extension)
* @param model The model variables to be passed in to the template
*/
apply(templateName, model) {
const template = this.templates[templateName];
if (!template) {
throw new Error(`Template not found: ${templateName}`);
}
const actualModel = { ...this.globals, ...(model || {}) };
return template(actualModel);
}
}
exports.Templates = Templates;
//# sourceMappingURL=templates.js.map