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

97.14% Statements 34/35
80% Branches 8/10
100% Functions 5/5
100% Lines 33/33

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 861x 1x 1x   1x 1x                                 59x 59x   2x   2x   4x         4x 1x   1x 1x             4x   4x   2x           2x   2x   2x 2x   2x 5x 5x   5x 1x 1x   1x 1x   1x   5x       1x         1x  
import fs from 'fs-extra';
import path from 'path';
import { constants, loadFileAndReplaceKeywords } from '../../../tools';
 
import log from '../../../logger';
import {
  isFile,
  getFiles,
  existsMustBeDir,
  dumpJSON,
  loadJSON,
  sanitize,
  clearClientArrays,
} from '../../../utils';
import { ParsedAsset } from '../../../types';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { Client } from '../../../tools/auth0/handlers/clients';
 
type ParsedClients = ParsedAsset<'clients', Client[]>;
 
function parse(context: DirectoryContext): ParsedClients {
  const clientsFolder = path.join(context.filePath, constants.CLIENTS_DIRECTORY);
  if (!existsMustBeDir(clientsFolder)) return { clients: null }; // Skip
 
  const foundFiles = getFiles(clientsFolder, ['.json']);
 
  const clients = foundFiles
    .map((f) => {
      const client = loadJSON(f, {
        mappings: context.mappings,
        disableKeywordReplacement: context.disableKeywordReplacement,
      });
 
      if (client.custom_login_page) {
        const htmlFileName = path.join(clientsFolder, client.custom_login_page);
 
        Eif (isFile(htmlFileName)) {
          client.custom_login_page = loadFileAndReplaceKeywords(htmlFileName, {
            mappings: context.mappings,
            disableKeywordReplacement: context.disableKeywordReplacement,
          });
        }
      }
 
      return client;
    })
    .filter((p) => Object.keys(p).length > 0); // Filter out empty clients
 
  return {
    clients,
  };
}
 
async function dump(context: DirectoryContext): Promise<void> {
  const { clients } = context.assets;
 
  Iif (!clients) return; // Skip, nothing to dump
 
  const clientsFolder = path.join(context.filePath, constants.CLIENTS_DIRECTORY);
  fs.ensureDirSync(clientsFolder);
 
  clients.forEach((client) => {
    const clientName = sanitize(client.name);
    const clientFile = path.join(clientsFolder, `${clientName}.json`);
 
    if (client.custom_login_page) {
      const html = client.custom_login_page;
      const customLoginHtml = path.join(clientsFolder, `${clientName}_custom_login_page.html`);
 
      log.info(`Writing ${customLoginHtml}`);
      fs.writeFileSync(customLoginHtml, html);
 
      client.custom_login_page = `./${clientName}_custom_login_page.html`;
    }
    dumpJSON(clientFile, clearClientArrays(client));
  });
}
 
const clientsHandler: DirectoryHandler<ParsedClients> = {
  parse,
  dump,
};
 
export default clientsHandler;