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

90.9% Statements 20/22
75% Branches 6/8
100% Functions 4/4
90.9% Lines 20/22

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 531x 1x 1x         1x         50x 50x 48x     2x 2x       2x   3x         2x       2x 2x       2x 2x   2x 3x       1x         1x  
import path from 'path';
import { ensureDirSync } from 'fs-extra';
import { getFiles, dumpJSON, loadJSON, existsMustBeDir } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { ParsedAsset } from '../../../types';
import { Theme } from '../../../tools/auth0/handlers/themes';
import { constants } from '../../../tools';
 
type ParsedThemes = ParsedAsset<'themes', Theme[]>;
 
function parse(context: DirectoryContext): ParsedThemes {
  const baseFolder = path.join(context.filePath, constants.THEMES_DIRECTORY);
  if (!existsMustBeDir(baseFolder)) {
    return { themes: null };
  }
 
  const themeDefinitionsFiles = getFiles(baseFolder, ['.json']);
  Iif (!themeDefinitionsFiles.length) {
    return { themes: [] };
  }
 
  const themes = themeDefinitionsFiles.map(
    (themeDefinitionsFile) =>
      loadJSON(themeDefinitionsFile, {
        mappings: context.mappings,
        disableKeywordReplacement: context.disableKeywordReplacement,
      }) as Theme
  );
  return { themes };
}
 
async function dump(context: DirectoryContext): Promise<void> {
  const { themes } = context.assets;
  Iif (!themes) {
    return;
  }
 
  const baseFolder = path.join(context.filePath, constants.THEMES_DIRECTORY);
  ensureDirSync(baseFolder);
 
  themes.forEach((themeDefinition, i) => {
    dumpJSON(path.join(baseFolder, `theme${i ? i : ''}.json`), themeDefinition);
  });
}
 
const themesHandler: DirectoryHandler<ParsedThemes> = {
  parse,
  dump,
};
 
export default themesHandler;