All files / transpiler/data FileDiscovery.ts

91.52% Statements 54/59
89.47% Branches 34/38
100% Functions 12/12
98.11% Lines 52/53

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226                            15x         15x                                 15x                               1x 1x 1x 1x 1x   1x             298x 298x 298x                                                 27x 27x       27x 1x   26x         27x     27x   27x   27x 29x   29x 1x 1x     28x   28x                 29x 147x               27x 27x 147x 7x   140x 140x                           159x   159x 4x     155x 4x     151x                         4x   4x 7x 7x 4x   3x       4x                   46x             5x             3x 13x            
/**
 * File Discovery
 * Scans directories for source files using fast-glob
 */
 
import fg from "fast-glob";
import { extname, resolve } from "node:path";
import EFileType from "./types/EFileType";
import IDiscoveredFile from "./types/IDiscoveredFile";
import IDiscoveryOptions from "./types/IDiscoveryOptions";
import IFileSystem from "../types/IFileSystem";
import NodeFileSystem from "../NodeFileSystem";
 
/** Default file system instance (singleton for performance) */
const defaultFs = NodeFileSystem.instance;
 
/**
 * Default extensions for each file type
 */
const EXTENSION_MAP: Record<string, EFileType> = {
  ".cnx": EFileType.CNext,
  ".cnext": EFileType.CNext,
  ".h": EFileType.CHeader,
  ".hpp": EFileType.CppHeader,
  ".hxx": EFileType.CppHeader,
  ".hh": EFileType.CppHeader,
  ".c": EFileType.CSource,
  ".cpp": EFileType.CppSource,
  ".cxx": EFileType.CppSource,
  ".cc": EFileType.CppSource,
};
 
/**
 * Default ignore patterns for fast-glob
 * Issue #355: Exclude .pio/build (compiled artifacts) but allow .pio/libdeps (library headers)
 */
const DEFAULT_IGNORE_GLOBS = [
  "**/node_modules/**",
  "**/.git/**",
  "**/.build/**",
  "**/.pio/build/**",
];
 
/**
 * Discovers source files in directories
 */
class FileDiscovery {
  /**
   * Convert RegExp patterns to glob ignore patterns
   */
  private static regexToGlob(pattern: RegExp): string {
    // Convert common patterns
    const src = pattern.source;
    Iif (src === "node_modules") return "**/node_modules/**";
    Iif (src === String.raw`\.git`) return "**/.git/**";
    Iif (src === String.raw`\.build`) return "**/.build/**";
    Iif (src === String.raw`\.pio[/\\]build`) return "**/.pio/build/**";
    // Fallback: wrap in wildcards
    return `**/*${src.replaceAll("\\", "")}*/**`;
  }
 
  /**
   * Classify a file path into a discovered file
   */
  private static classifyFile(filePath: string): IDiscoveredFile {
    const ext = extname(filePath).toLowerCase();
    const type = EXTENSION_MAP[ext] ?? EFileType.Unknown;
    return {
      path: filePath,
      type,
      extension: ext,
    };
  }
 
  /**
   * Discover files in the given directories
   *
   * Issue #331: Uses fast-glob's unique option to avoid duplicates
   * when overlapping directories are provided.
   *
   * Note: fast-glob accesses the filesystem directly; the fs parameter
   * is used only for directory existence checks.
   *
   * @param directories - Directories to scan
   * @param options - Discovery options
   * @param fs - File system abstraction (defaults to NodeFileSystem)
   */
  static discover(
    directories: string[],
    options: IDiscoveryOptions = {},
    fs: IFileSystem = defaultFs,
  ): IDiscoveredFile[] {
    const recursive = options.recursive ?? true;
    const extensions = options.extensions ?? Object.keys(EXTENSION_MAP);
 
    // Build ignore patterns
    let ignorePatterns: string[];
    if (options.excludePatterns) {
      ignorePatterns = options.excludePatterns.map((r) => this.regexToGlob(r));
    } else {
      ignorePatterns = DEFAULT_IGNORE_GLOBS;
    }
 
    // Build glob pattern for extensions
    const extPattern =
      extensions.length === 1
        ? `*${extensions[0]}`
        : `*{${extensions.join(",")}}`;
    const pattern = recursive ? `**/${extPattern}` : extPattern;
 
    const allFiles: IDiscoveredFile[] = [];
 
    for (const dir of directories) {
      const resolvedDir = resolve(dir);
 
      if (!fs.exists(resolvedDir)) {
        console.warn(`Warning: Directory not found: ${dir}`);
        continue;
      }
 
      try {
        // Use fast-glob to find files
        const files = fg.sync(pattern, {
          cwd: resolvedDir,
          absolute: true,
          ignore: ignorePatterns,
          deep: recursive ? Infinity : 1,
          onlyFiles: true,
          followSymbolicLinks: false,
        });
 
        for (const file of files) {
          allFiles.push(this.classifyFile(file));
        }
      } catch {
        console.warn(`Warning: Cannot read directory: ${dir}`);
      }
    }
 
    // Issue #331: Remove duplicates from overlapping directories
    const seenPaths = new Set<string>();
    return allFiles.filter((file) => {
      if (seenPaths.has(file.path)) {
        return false;
      }
      seenPaths.add(file.path);
      return true;
    });
  }
 
  /**
   * Discover a single file
   *
   * @param filePath - Path to the file
   * @param fs - File system abstraction (defaults to NodeFileSystem)
   */
  static discoverFile(
    filePath: string,
    fs: IFileSystem = defaultFs,
  ): IDiscoveredFile | null {
    const resolvedPath = resolve(filePath);
 
    if (!fs.exists(resolvedPath)) {
      return null;
    }
 
    if (!fs.isFile(resolvedPath)) {
      return null;
    }
 
    return this.classifyFile(resolvedPath);
  }
 
  /**
   * Discover multiple specific files
   *
   * @param filePaths - Paths to the files
   * @param fs - File system abstraction (defaults to NodeFileSystem)
   */
  static discoverFiles(
    filePaths: string[],
    fs: IFileSystem = defaultFs,
  ): IDiscoveredFile[] {
    const files: IDiscoveredFile[] = [];
 
    for (const filePath of filePaths) {
      const file = this.discoverFile(filePath, fs);
      if (file) {
        files.push(file);
      } else {
        console.warn(`Warning: File not found: ${filePath}`);
      }
    }
 
    return files;
  }
 
  /**
   * Filter discovered files by type
   */
  static filterByType(
    files: IDiscoveredFile[],
    type: EFileType,
  ): IDiscoveredFile[] {
    return files.filter((f) => f.type === type);
  }
 
  /**
   * Get C-Next files from a list
   */
  static getCNextFiles(files: IDiscoveredFile[]): IDiscoveredFile[] {
    return this.filterByType(files, EFileType.CNext);
  }
 
  /**
   * Get C/C++ header files from a list
   */
  static getHeaderFiles(files: IDiscoveredFile[]): IDiscoveredFile[] {
    return files.filter(
      (f) => f.type === EFileType.CHeader || f.type === EFileType.CppHeader,
    );
  }
}
 
export default FileDiscovery;