All files / generators/module-boundaries/utils find-files.util.ts

100% Statements 12/12
75% Branches 6/8
100% Functions 1/1
100% Lines 10/10

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                      1x 3x 1x 1x     2x 2x 1x 1x 1x     2x    
import { existsSync, readdirSync } from 'fs';
import path from 'path';
 
/**
 * Find all JavaScript files in a directory.
 * @param tree the file system tree
 * @param src the source directory
 * @param filter the file extension filter, defaults to .html
 * @param result the execution result, is required for recursion, defaults to { stderr: '', stdout: '' }
 * @returns execution result
 */
export const findFiles = (src: string, filter = '.js', result = { stderr: '', stdout: '' }): { stderr: string; stdout: string } => {
  if (!existsSync(src)) {
    result.stderr = `Source directory ${src} does not exist`;
    return result;
  }
 
  const files = readdirSync(src);
  for (let i = 0, max = files.length; i < max; i += 1) {
    const filePath = path.join(src, files[i]);
    Eif (filePath.endsWith(filter)) {
      result.stdout += result.stdout.length === 0 ? filePath : ` ${filePath}`;
    }
  }
  return result;
};