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

92.59% Statements 50/54
73.68% Branches 14/19
100% Functions 8/8
97.91% Lines 47/48

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1311x 1x 1x 1x               50x 50x   50x   3x 3x 2x           1x     3x         3x   2x 2x 2x       2x             2x     2x                 1x   1x   1x   1x     1x 1x       1x   1x         1x   1x 1x 1x 1x                     1x 1x             1x 1x   1x   1x 1x 1x 1x     1x   1x   1x   1x     1x         1x  
import fs from 'fs-extra';
import path from 'path';
import { constants, loadFileAndReplaceKeywords } from '../../../tools';
import { dumpJSON, existsMustBeDir, getFiles, isFile, loadJSON } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { Asset, ParsedAsset } from '../../../types';
 
type ParsedBranding = ParsedAsset<'branding', Asset>;
 
function parse(context: DirectoryContext): ParsedBranding {
  const brandingDirectory = path.join(context.filePath, constants.BRANDING_DIRECTORY);
  const brandingFile = path.join(brandingDirectory, 'branding.json');
 
  if (!existsMustBeDir(brandingDirectory)) return { branding: null };
 
  const brandingSettings = (() => {
    if (isFile(brandingFile)) {
      return loadJSON(brandingFile, {
        mappings: context.mappings,
        disableKeywordReplacement: context.disableKeywordReplacement,
      });
    }
 
    return null;
  })();
 
  const brandingTemplatesFolder = path.join(
    brandingDirectory,
    constants.BRANDING_TEMPLATES_DIRECTORY
  );
 
  if (!existsMustBeDir(brandingTemplatesFolder)) return { branding: brandingSettings };
 
  const templatesDefinitionFiles = getFiles(brandingTemplatesFolder, ['.json']);
  const templates = templatesDefinitionFiles.map((templateDefinitionFile) => {
    const definition = loadJSON(templateDefinitionFile, {
      mappings: context.mappings,
      disableKeywordReplacement: context.disableKeywordReplacement,
    });
    definition.body = loadFileAndReplaceKeywords(
      path.join(brandingTemplatesFolder, definition.body),
      {
        mappings: context.mappings,
        disableKeywordReplacement: context.disableKeywordReplacement,
      }
    );
    return definition;
  }, {});
 
  return {
    branding: {
      ...brandingSettings,
      templates,
    },
  };
}
 
async function dump(context: DirectoryContext) {
  const { branding } = context.assets;
 
  Iif (!branding) return;
 
  dumpBranding(context);
 
  Eif (!!branding.templates) dumpBrandingTemplates(context);
}
 
const dumpBrandingTemplates = ({ filePath, assets }: DirectoryContext): void => {
  Iif (!assets.branding || !assets.branding.templates) return;
 
  const {
    branding: { templates = [] },
  } = assets;
 
  const brandingTemplatesFolder = path.join(
    filePath,
    constants.BRANDING_DIRECTORY,
    constants.BRANDING_TEMPLATES_DIRECTORY
  );
  fs.ensureDirSync(brandingTemplatesFolder);
 
  templates.forEach((templateDefinition) => {
    const markup = templateDefinition.body;
    try {
      fs.writeFileSync(
        path.join(brandingTemplatesFolder, `${templateDefinition.template}.html`),
        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}${templateDefinition.template}.html`;
    dumpJSON(
      path.join(brandingTemplatesFolder, `${templateDefinition.template}.json`),
      templateDefinition
    );
  });
};
 
const dumpBranding = ({ filePath, assets }: DirectoryContext): void => {
  Iif (!assets || !assets.branding) return;
 
  const { branding } = assets;
 
  const brandingWithoutTemplates = (() => {
    const newBranding = { ...branding };
    delete newBranding.templates;
    return newBranding;
  })();
 
  const brandingDirectory = path.join(filePath, constants.BRANDING_DIRECTORY);
 
  fs.ensureDirSync(brandingDirectory);
 
  const brandingFilePath = path.join(brandingDirectory, 'branding.json');
 
  dumpJSON(brandingFilePath, brandingWithoutTemplates);
};
 
const brandingHandler: DirectoryHandler<ParsedBranding> = {
  parse,
  dump,
};
 
export default brandingHandler;