All files / scripts/agentic/gate-shared file-walkers.ts

100% Statements 12/12
100% Branches 10/10
100% Functions 1/1
100% Lines 11/11

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                                                19x 19x 19x 18x 18x 117x 117x 7x 110x 106x     18x    
/**
 * @module scripts/agentic/gate-shared/file-walkers
 * @description Shared filesystem walkers used by multiple gate checks
 *              (currently the recursive stub scanner, but extracted here
 *              so any future check needing a recursive `.md` walk can
 *              reuse the same implementation rather than duplicating it).
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import { readdir } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
 
/**
 * Recursively collect all `.md` files under a directory, returning paths
 * relative to `baseDir`. Used by the stub scanner to mirror the canonical
 * gate's recursive grep over the entire analysis tree.
 */
export async function collectMdFilesRecursive(
  baseDir: string,
  prefix: string,
): Promise<string[]> {
  const results: string[] = [];
  const currentDir = prefix ? join(baseDir, prefix) : baseDir;
  if (!existsSync(currentDir)) return results;
  const entries = await readdir(currentDir, { withFileTypes: true });
  for (const entry of entries) {
    const relPath = prefix ? `${prefix}/${entry.name}` : entry.name;
    if (entry.isDirectory()) {
      results.push(...(await collectMdFilesRecursive(baseDir, relPath)));
    } else if (entry.name.endsWith('.md')) {
      results.push(relPath);
    }
  }
  return results;
}