All files / src/context/yaml/handlers branding.ts

91.42% Statements 32/35
80% Branches 8/10
100% Functions 4/4
93.33% Lines 28/30

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 87 88 89 90 91 92 93 94 95 96 97 981x 1x 1x                                     41x     1x 1x   1x       1x   1x 1x                   1x                 5x   5x   5x     5x 1x       1x   1x 1x 1x 1x 1x 1x               1x       1x       5x     1x         1x  
import fs from 'fs-extra';
import path from 'path';
import { constants, loadFileAndReplaceKeywords } from '../../../tools';
 
import { YAMLHandler } from '.';
import YAMLContext from '..';
import { Asset, ParsedAsset } from '../../../types';
 
type BrandingTemplate = {
  template: string;
  body: string;
};
 
type ParsedBranding = ParsedAsset<
  'branding',
  { [key: string]: Asset } & {
    templates?: BrandingTemplate[];
  }
>;
 
async function parse(context: YAMLContext): Promise<ParsedBranding> {
  if (!context.assets.branding) return { branding: null };
 
  const {
    branding: { templates, ...branding },
  } = context.assets;
 
  Iif (!templates) {
    return { branding: { ...branding } };
  }
 
  const parsedTemplates: BrandingTemplate[] = templates.map(
    (templateDefinition: BrandingTemplate): BrandingTemplate => {
      const markupFile = path.join(context.basePath, templateDefinition.body);
      return {
        template: templateDefinition.template,
        body: loadFileAndReplaceKeywords(markupFile, {
          mappings: context.mappings,
          disableKeywordReplacement: context.disableKeywordReplacement,
        }),
      };
    }
  );
 
  return {
    branding: {
      ...branding,
      templates: parsedTemplates,
    },
  };
}
 
async function dump(context: YAMLContext): Promise<ParsedBranding> {
  Iif (!context.assets.branding) return { branding: null };
 
  const { templates: templateConfig, ...branding } = context.assets.branding;
 
  let templates = templateConfig || [];
 
  // create templates folder
  if (templates.length) {
    const brandingTemplatesFolder = path.join(
      context.basePath,
      constants.BRANDING_TEMPLATES_YAML_DIRECTORY
    );
    fs.ensureDirSync(brandingTemplatesFolder);
 
    templates = templates.map((templateDefinition) => {
      const file = `${templateDefinition.template}.html`;
      const templateMarkupFile = path.join(brandingTemplatesFolder, file);
      const markup = templateDefinition.body;
      try {
        fs.writeFileSync(templateMarkupFile, markup);
      } catch (e) {
        throw new Error(
          `Error writing template file: ${templateDefinition.template}, because: ${e.message}`
        );
      }
 
      // save the location as relative file.
      templateDefinition.body = `.${path.sep}${path.join(
        constants.BRANDING_TEMPLATES_YAML_DIRECTORY,
        file
      )}`;
      return templateDefinition;
    });
  }
 
  return { branding: { templates, ...branding } };
}
 
const brandingHandler: YAMLHandler<ParsedBranding> = {
  parse,
  dump,
};
 
export default brandingHandler;