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

96.77% Statements 30/31
80% Branches 8/10
100% Functions 4/4
100% Lines 30/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 741x 1x 1x 1x 1x                     41x 41x   41x 40x     1x     3x 1x   1x 1x       3x               5x   5x 5x   5x     6x 5x 5x 5x   5x 5x 5x   5x     6x           1x         1x  
import path from 'path';
import fs from 'fs-extra';
import { constants } from '../../../tools';
import log from '../../../logger';
import { isFile, sanitize, clearClientArrays } from '../../../utils';
import { YAMLHandler } from '.';
import YAMLContext from '..';
import { ParsedAsset } from '../../../types';
import { Client } from '../../../tools/auth0/handlers/clients';
 
type ParsedClients = ParsedAsset<'clients', Client[]>;
 
async function parse(context: YAMLContext): Promise<ParsedClients> {
  // Load the HTML file for custom_login_page
 
  const { clients } = context.assets;
  const clientsFolder = path.join(context.basePath, constants.CLIENTS_DIRECTORY);
 
  if (!clients) {
    return { clients: null };
  }
 
  return {
    clients: [
      ...clients.map((client) => {
        if (client.custom_login_page) {
          const htmlFileName = path.join(clientsFolder, client.custom_login_page);
 
          Eif (isFile(htmlFileName)) {
            client.custom_login_page = context.loadFile(htmlFileName);
          }
        }
 
        return client;
      }),
    ],
  };
}
 
async function dump(context: YAMLContext): Promise<ParsedClients> {
  // Save custom_login_page to a separate html file
  const clientsFolder = path.join(context.basePath, constants.CLIENTS_DIRECTORY);
 
  const { clients } = context.assets;
  Iif (!clients) return { clients: null };
 
  return {
    clients: [
      ...clients.map((client) => {
        if (client.custom_login_page) {
          const clientName = sanitize(client.name);
          const html = client.custom_login_page;
          const customLoginHtml = path.join(clientsFolder, `${clientName}_custom_login_page.html`);
 
          log.info(`Writing ${customLoginHtml}`);
          fs.ensureDirSync(clientsFolder);
          fs.writeFileSync(customLoginHtml, html);
 
          client.custom_login_page = `./${clientName}_custom_login_page.html`;
        }
 
        return clearClientArrays(client) as Client;
      }),
    ],
  };
}
 
const clientsHandler: YAMLHandler<ParsedClients> = {
  parse,
  dump,
};
 
export default clientsHandler;