@brilab-mailer/template-handlebars 0.0.1-beta.50 → 0.0.1-beta.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,17 +3,17 @@ import { Inject, Injectable } from '@nestjs/common';
|
|
|
3
3
|
import { MAILER_TEMPLATE_ENGINE_OPTIONS, } from '@brilab-mailer/contracts';
|
|
4
4
|
import * as fs from 'fs';
|
|
5
5
|
import * as path from 'path';
|
|
6
|
-
import
|
|
6
|
+
import { compile } from 'handlebars';
|
|
7
7
|
let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
|
|
8
8
|
options;
|
|
9
9
|
templatesDir;
|
|
10
10
|
layoutsDir;
|
|
11
11
|
constructor(options) {
|
|
12
12
|
this.options = options;
|
|
13
|
-
const rootDir = this.options
|
|
13
|
+
const rootDir = this.options?.rootDir ?? process.cwd();
|
|
14
14
|
const baseDir = path.join(rootDir, 'templates', 'emails');
|
|
15
15
|
this.templatesDir = baseDir;
|
|
16
|
-
const layoutsFolder = options
|
|
16
|
+
const layoutsFolder = options?.folders?.layouts || 'layouts';
|
|
17
17
|
this.layoutsDir = path.join(baseDir, layoutsFolder);
|
|
18
18
|
}
|
|
19
19
|
loadTemplate(key) {
|
|
@@ -22,14 +22,14 @@ let HandlebarsTemplateEngine = class HandlebarsTemplateEngine {
|
|
|
22
22
|
throw new Error(`Template not found: ${filePath}`);
|
|
23
23
|
}
|
|
24
24
|
const source = fs.readFileSync(filePath, 'utf8');
|
|
25
|
-
return
|
|
25
|
+
return compile(source);
|
|
26
26
|
}
|
|
27
27
|
loadLayout(key) {
|
|
28
28
|
const filePath = path.join(this.layoutsDir, `${key}.hbs`);
|
|
29
29
|
if (!fs.existsSync(filePath))
|
|
30
30
|
return null;
|
|
31
31
|
const source = fs.readFileSync(filePath, 'utf8');
|
|
32
|
-
return
|
|
32
|
+
return compile(source);
|
|
33
33
|
}
|
|
34
34
|
async render(templateKey, context = {}, options = { layoutKey: 'default' }) {
|
|
35
35
|
const template = this.loadTemplate(templateKey);
|