-
-
Notifications
You must be signed in to change notification settings - Fork 933
/
file-based-template-loader.ts
41 lines (36 loc) · 1.28 KB
/
file-based-template-loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { Injector, RequestContext } from '@vendure/core';
import fs from 'fs/promises';
import path from 'path';
import { LoadTemplateInput, Partial } from '../types';
import { TemplateLoader } from './template-loader';
/**
* @description
* Loads email templates from the local file system. This is the default
* loader used by the EmailPlugin.
*
* @docsCategory core plugins/EmailPlugin
* @docsPage TemplateLoader
*/
export class FileBasedTemplateLoader implements TemplateLoader {
constructor(private templatePath: string) {}
async loadTemplate(
_injector: Injector,
_ctx: RequestContext,
{ type, templateName }: LoadTemplateInput,
): Promise<string> {
const templatePath = path.join(this.templatePath, type, templateName);
return fs.readFile(templatePath, 'utf-8');
}
async loadPartials(): Promise<Partial[]> {
const partialsPath = path.join(this.templatePath, 'partials');
const partialsFiles = await fs.readdir(partialsPath);
return Promise.all(
partialsFiles.map(async file => {
return {
name: path.basename(file, '.hbs'),
content: await fs.readFile(path.join(partialsPath, file), 'utf-8'),
};
}),
);
}
}