All files / executors/prettier/utils find-html-files.util.ts

76.19% Statements 16/21
71.42% Branches 10/14
100% Functions 1/1
73.68% Lines 14/19

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                      2x           5x 2x 2x 2x 2x     3x             3x 3x 3x 3x   3x 2x     3x    
import { joinPathFragments, logger } from '@nx/devkit';
import type { FsTree } from 'nx/src/generators/tree';
 
/**
 * Recursively find all `.html` or `.json` files in a directory and its subdirectories.
 * @param tree file system tree
 * @param src source directory
 * @param filter file extension filter, defaults to `.html`, supports `.json`
 * @param result execution result, required for recursion, defaults to { stderr: '', stdout: '' }
 * @returns execution result
 */
export const findFiles = (
  tree: FsTree,
  src: string,
  filter: '.html' | '.json' = '.html',
  result = { stderr: '', stdout: '' },
): { stderr: string; stdout: string } => {
  if (!tree.exists(src)) {
    const message = `Source directory ${src} does not exist`;
    logger.error(message);
    result.stderr = message;
    return result;
  }
 
  Iif (tree.isFile(src)) {
    const message = `Source directory ${src} is a file`;
    logger.error(message);
    result.stderr = message;
    return result;
  }
 
  const files = tree.children(src);
  for (let i = 0, max = files.length; i < max; i += 1) {
    const filePath = joinPathFragments(src, files[i]);
    Iif (!tree.isFile(filePath) && tree.exists(filePath)) {
      findFiles(tree, filePath, filter, result);
    } else if (filePath.endsWith(filter)) {
      result.stdout += result.stdout.length === 0 ? filePath : ` ${filePath}`;
    }
  }
  return result;
};