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

90% Statements 36/40
66.66% Branches 8/12
100% Functions 6/6
91.66% Lines 33/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 84 85 86 871x 1x 1x   1x 1x       1x         54x 54x   3x 7x     3x 4x         4x 4x 2x 2x     2x   2x       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';
import { existsSync } from 'fs';
 
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 jsonFiles = getFiles(emailsFolder, ['.json']).filter(
    (f) => path.basename(f) !== 'provider.json'
  );
 
  const emailTemplates = jsonFiles.flatMap((filePath: string) => {
    const meta: { body: string | undefined } = loadJSON(filePath, {
      mappings: context.mappings,
      disableKeywordReplacement: context.disableKeywordReplacement,
    });
 
    const templateFilePath = (() => {
      if (meta.body !== undefined) {
        const explicitlyDefinedPath = path.join(emailsFolder, meta.body);
        Eif (existsSync(explicitlyDefinedPath)) return explicitlyDefinedPath;
      }
 
      const defaultPath = path.join(emailsFolder, path.parse(filePath).name + '.html');
 
      Eif (existsSync(defaultPath)) return defaultPath;
      return null;
    })();
 
    Iif (templateFilePath === null) {
      log.warn(
        `Skipping email template file ${meta.body} as missing the corresponding '.json' file`
      );
      return [];
    }
 
    return {
      ...meta,
      body: loadFileAndReplaceKeywords(templateFilePath, {
        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;