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

40.9% Statements 9/22
25% Branches 1/4
20% Functions 1/5
40% Lines 8/20

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 551x 1x 1x 1x                 59x 59x                                                                     1x         1x  
import fs from 'fs-extra';
import path from 'path';
import { constants } from '../../../tools';
import { getFiles, existsMustBeDir, dumpJSON, loadJSON, sanitize } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { ParsedAsset } from '../../../types';
import { ResourceServer } from '../../../tools/auth0/handlers/resourceServers';
 
type ParsedResourceServers = ParsedAsset<'resourceServers', ResourceServer[]>;
 
function parse(context: DirectoryContext): ParsedResourceServers {
  const resourceServersFolder = path.join(context.filePath, constants.RESOURCE_SERVERS_DIRECTORY);
  Eif (!existsMustBeDir(resourceServersFolder)) return { resourceServers: null }; // Skip
 
  const foundFiles = getFiles(resourceServersFolder, ['.json']);
 
  const resourceServers = foundFiles
    .map((f) =>
      loadJSON(f, {
        mappings: context.mappings,
        disableKeywordReplacement: context.disableKeywordReplacement,
      })
    )
    .filter((p) => Object.keys(p).length > 0); // Filter out empty resourceServers
 
  return {
    resourceServers,
  };
}
 
async function dump(context: DirectoryContext): Promise<void> {
  const { resourceServers } = context.assets;
 
  if (!resourceServers) return; // Skip, nothing to dump
 
  const resourceServersFolder = path.join(context.filePath, constants.RESOURCE_SERVERS_DIRECTORY);
  fs.ensureDirSync(resourceServersFolder);
 
  resourceServers.forEach((resourceServer) => {
    const resourceServerFile = path.join(
      resourceServersFolder,
      sanitize(`${resourceServer.name}.json`)
    );
    dumpJSON(resourceServerFile, resourceServer);
  });
}
 
const resourceServersHandler: DirectoryHandler<ParsedResourceServers> = {
  parse,
  dump,
};
 
export default resourceServersHandler;