All files / src/context/directory/handlers emailTemplates.ts

87.8% Statements 36/41
78.57% Branches 11/14
100% Functions 6/6
88.88% Lines 32/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x 1x 1x   1x 1x               54x 54x   3x 11x     3x   3x 8x 8x 8x 8x     3x   4x     4x       4x                           3x           1x   1x     1x 1x 1x   2x 2x 2x     2x 2x       1x         1x  
import fs from 'fs-extra';
import path from 'path';
import { constants, loadFileAndReplaceKeywords } from '../../../tools';
 
import log from '../../../logger';
import { getFiles, existsMustBeDir, dumpJSON, loadJSON } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { Asset, ParsedAsset } from '../../../types';
 
type ParsedEmailTemplates = ParsedAsset<'emailTemplates', Asset[]>;
 
function parse(context: DirectoryContext): ParsedEmailTemplates {
  const emailsFolder = path.join(context.filePath, constants.EMAIL_TEMPLATES_DIRECTORY);
  if (!existsMustBeDir(emailsFolder)) return { emailTemplates: null }; // Skip
 
  const files = getFiles(emailsFolder, ['.json', '.html']).filter(
    (f) => path.basename(f) !== 'provider.json'
  );
 
  const sorted: { meta: string } | { html: string } | {} = {};
 
  files.forEach((file) => {
    const { ext, name } = path.parse(file);
    if (!sorted[name]) sorted[name] = {};
    if (ext === '.json') sorted[name].meta = file;
    if (ext === '.html') sorted[name].html = file;
  });
 
  const emailTemplates = Object.values(sorted).flatMap(
    ({ meta, html }: { meta?: string; html?: string }) => {
      Iif (!meta) {
        log.warn(`Skipping email template file ${html} as missing the corresponding '.json' file`);
        return [];
      } else Iif (!html) {
        log.warn(`Skipping email template file ${meta} as missing corresponding '.html' file`);
        return [];
      } else {
        return {
          ...loadJSON(meta, {
            mappings: context.mappings,
            disableKeywordReplacement: context.disableKeywordReplacement,
          }),
          body: loadFileAndReplaceKeywords(html, {
            mappings: context.mappings,
            disableKeywordReplacement: context.disableKeywordReplacement,
          }),
        };
      }
    }
  );
 
  return {
    emailTemplates,
  };
}
 
async function dump(context: DirectoryContext): Promise<void> {
  const emailTemplates = context.assets.emailTemplates;
 
  Iif (!emailTemplates) return; // Skip, nothing to dump
 
  // Create Templates folder
  const templatesFolder = path.join(context.filePath, constants.EMAIL_TEMPLATES_DIRECTORY);
  fs.ensureDirSync(templatesFolder);
  emailTemplates.forEach((template) => {
    // Dump template html to file
    const templateHtml = path.join(templatesFolder, `${template.template}.html`);
    log.info(`Writing ${templateHtml}`);
    fs.writeFileSync(templateHtml, template.body);
 
    // Dump template metadata
    const templateFile = path.join(templatesFolder, `${template.template}.json`);
    dumpJSON(templateFile, { ...template, body: `./${template.template}.html` });
  });
}
 
const emailTemplatesHandler: DirectoryHandler<ParsedEmailTemplates> = {
  parse,
  dump,
};
 
export default emailTemplatesHandler;