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

57.14% Statements 8/14
50% Branches 1/2
25% Functions 1/4
53.84% Lines 7/13

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 421x 1x   1x               62x 62x                                             1x         1x  
import path from 'path';
import { constants } from '../../../tools';
 
import { getFiles, existsMustBeDir, loadJSON } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { Asset, ParsedAsset } from '../../../types';
 
type ParsedRulesConfigs = ParsedAsset<'rulesConfigs', Asset[]>;
 
function parse(context: DirectoryContext): ParsedRulesConfigs {
  const rulesConfigsFolder = path.join(context.filePath, constants.RULES_CONFIGS_DIRECTORY);
  Eif (!existsMustBeDir(rulesConfigsFolder)) return { rulesConfigs: null }; // Skip
 
  const foundFiles: string[] = getFiles(rulesConfigsFolder, ['.json']);
 
  const rulesConfigs = foundFiles
    .map((f) =>
      loadJSON(f, {
        mappings: context.mappings,
        disableKeywordReplacement: context.disableKeywordReplacement,
      })
    )
    .filter((p) => Object.keys(p).length > 0); // Filter out empty rulesConfigs
 
  return {
    rulesConfigs,
  };
}
 
async function dump(): Promise<void> {
  // do not export rulesConfigs as its values cannot be extracted
  return;
}
 
const rulesConfigsHandler: DirectoryHandler<ParsedRulesConfigs> = {
  parse,
  dump,
};
 
export default rulesConfigsHandler;