template-loader.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import { Injector, RequestContext } from '@vendure/core';
  2. import fs from 'fs/promises';
  3. import path from 'path';
  4. import { LoadTemplateInput, Partial, TemplateLoader } from './types';
  5. /**
  6. * Loads email templates according to the configured TemplateConfig values.
  7. */
  8. export class FileBasedTemplateLoader implements TemplateLoader {
  9. constructor(private templatePath: string) { }
  10. async loadTemplate(
  11. _injector: Injector,
  12. _ctx: RequestContext,
  13. { type, templateName }: LoadTemplateInput,
  14. ): Promise<string> {
  15. const templatePath = path.join(this.templatePath, type, templateName);
  16. return fs.readFile(templatePath, 'utf-8');
  17. }
  18. async loadPartials(): Promise<Partial[]> {
  19. const partialsPath = path.join(this.templatePath, 'partials');
  20. const partialsFiles = await fs.readdir(partialsPath);
  21. return Promise.all(partialsFiles.map(async (file) => {
  22. return {
  23. name: path.basename(file, '.hbs'),
  24. content: await fs.readFile(path.join(partialsPath, file), 'utf-8')
  25. }
  26. }));
  27. }
  28. }