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

95.65% Statements 22/23
87.5% Branches 7/8
100% Functions 4/4
95.45% Lines 21/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 53 54 55 56 57 58 59 60 611x 1x   1x                   41x   41x   2x   5x                 8x   8x       8x 8x   8x 11x 2x     9x 9x 9x 9x           8x     1x         1x  
import path from 'path';
import fs from 'fs-extra';
 
import log from '../../../logger';
import { YAMLHandler } from '.';
import YAMLContext from '..';
import { ParsedAsset } from '../../../types';
import { Page } from '../../../tools/auth0/handlers/pages';
 
type ParsedPages = ParsedAsset<'pages', Page[]>;
 
async function parse(context: YAMLContext): Promise<ParsedPages> {
  // Load the HTML file for each page
  const { pages } = context.assets;
 
  if (!pages) return { pages: null };
 
  return {
    pages: [
      ...pages.map((page) => ({
        ...page,
        html: page.html ? context.loadFile(page.html) : '',
      })),
    ],
  };
}
 
async function dump(context: YAMLContext): Promise<ParsedPages> {
  let pages = context.assets.pages;
 
  Iif (!pages) {
    return { pages: null };
  }
 
  const pagesFolder = path.join(context.basePath, 'pages');
  fs.ensureDirSync(pagesFolder);
 
  pages = pages.map((page) => {
    if (page.html === undefined) {
      return page;
    }
 
    const htmlFile = path.join(pagesFolder, `${page.name}.html`);
    log.info(`Writing ${htmlFile}`);
    fs.writeFileSync(htmlFile, page.html);
    return {
      ...page,
      html: `./pages/${page.name}.html`,
    };
  });
 
  return { pages };
}
 
const pagesHandler: YAMLHandler<ParsedPages> = {
  parse,
  dump,
};
 
export default pagesHandler;