@tsed/common
Version:
A TypeScript Framework on top of Express
98 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PlatformViews = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@tsed/core");
const di_1 = require("@tsed/di");
const consolidate_1 = tslib_1.__importDefault(require("consolidate"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const path_1 = require("path");
/**
* @platform
*/
let PlatformViews = class PlatformViews {
constructor() {
this.consolidate = consolidate_1.default;
}
$onInit() {
this.extensionsMap = new Map(Object.entries({
hbs: "handlebars",
ejs: "ejs",
...this.extensions
}));
this.loadEngineRequires();
}
loadEngineRequires() {
return Object.keys(this.engineOptions).map((engineType) => {
const options = this.getEngineOptions(engineType);
if (options && options.requires)
this.consolidate.requires[engineType] = options.requires;
});
}
getExtensions() {
return this.extensionsMap;
}
getEngines() {
return Array.from(this.extensionsMap.entries())
.map(([extension, engineType]) => {
const engine = this.getEngine(engineType);
return engine && { extension, engine };
})
.filter(Boolean);
}
getEngine(engineType) {
return this.consolidate[engineType];
}
getEngineOptions(engineType) {
return (engineType && this.engineOptions[engineType]) || {};
}
render(viewPath, options = {}) {
const extension = (path_1.extname(viewPath) || this.viewEngine).replace(/\./, "");
const engineType = this.getExtensions().get(extension);
const engineOptions = this.getEngineOptions(engineType);
const render = this.getEngine(engineType);
if (!engineType || !render) {
throw new Error(`Engine not found for the ".${extension}" file extension`);
}
return render(this.resolve(viewPath), Object.assign({ cache: this.cache || this.env === core_1.Env.PROD }, engineOptions, options));
}
resolve(viewPath) {
const extension = (path_1.extname(viewPath) || this.viewEngine).replace(/\./, "");
viewPath = viewPath.replace(path_1.extname(viewPath), "") + "." + extension;
return ([
viewPath,
path_1.resolve(path_1.join(this.root, viewPath)),
path_1.resolve(path_1.join(process.cwd(), "views", viewPath)),
path_1.resolve(path_1.join(process.cwd(), "public", viewPath))
].find((file) => fs_1.default.existsSync(file)) || viewPath);
}
};
tslib_1.__decorate([
di_1.Constant("env"),
tslib_1.__metadata("design:type", String)
], PlatformViews.prototype, "env", void 0);
tslib_1.__decorate([
di_1.Constant("views.root", `${process.cwd()}/views`),
tslib_1.__metadata("design:type", String)
], PlatformViews.prototype, "root", void 0);
tslib_1.__decorate([
di_1.Constant("views.cache"),
tslib_1.__metadata("design:type", Boolean)
], PlatformViews.prototype, "cache", void 0);
tslib_1.__decorate([
di_1.Constant("views.viewEngine", "ejs"),
tslib_1.__metadata("design:type", String)
], PlatformViews.prototype, "viewEngine", void 0);
tslib_1.__decorate([
di_1.Constant("views.extensions", {}),
tslib_1.__metadata("design:type", Object)
], PlatformViews.prototype, "extensions", void 0);
tslib_1.__decorate([
di_1.Constant("views.options", {}),
tslib_1.__metadata("design:type", Object)
], PlatformViews.prototype, "engineOptions", void 0);
PlatformViews = tslib_1.__decorate([
di_1.Injectable()
], PlatformViews);
exports.PlatformViews = PlatformViews;
//# sourceMappingURL=PlatformViews.js.map