All files / lib/file-utils file-utils.lib.ts

57.14% Statements 12/21
0% Branches 0/4
33.33% Functions 2/6
64.71% Lines 11/17

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 294x 4x   4x   4x 16x 4x   4x   16x 16x   4x     4x                      
import fs from "fs-extra";
import path from "path";
 
const defaultShouldLoad = (file): boolean => file.indexOf(".") !== 0 && file.slice(-3) === ".js";
 
export const loadFolderFilesSync = (folderPath, shouldLoad = defaultShouldLoad): any => {
  const files = fs.readdirSync(folderPath).filter(file => shouldLoad(file));
  const loadedFiles = {};
 
  for (const file of files) {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const localFile = require(path.join(folderPath, file));
    loadedFiles[file.split(".")[0]] = localFile;
  }
  return loadedFiles;
};
 
export const loadFolderFiles = async (folderPath, shouldLoad = defaultShouldLoad): Promise<any> => {
  const files = await fs.readdir(folderPath).filter(file => file.shouldLoad(file));
  const loadedFiles = {};
 
  for (const file of files) {
    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const localFile = require(path.join(folderPath, file));
    loadedFiles[file.split(".")[0]] = localFile;
  }
  return loadedFiles;
};