import fs from 'fs';
import path from 'path';

/** Racine des sources : `src/`. */
export const SRC_ROOT = path.join(__dirname, '..');

const SCANNED_EXTENSIONS = ['.ts', '.tsx', '.css'];

/**
 * Liste récursivement les fichiers source (`.ts`, `.tsx`, `.css`) sous `directory`,
 * pour les tests qui verrouillent une convention sur l'ensemble du code.
 */
export function collectSourceFiles(
  directory: string = SRC_ROOT,
  ignoredDirectories: string[] = ['node_modules']
): string[] {
  return fs.readdirSync(directory, {withFileTypes: true}).flatMap(entry => {
    const fullPath = path.join(directory, entry.name);

    if (entry.isDirectory()) {
      return ignoredDirectories.includes(entry.name)
        ? []
        : collectSourceFiles(fullPath, ignoredDirectories);
    }

    return SCANNED_EXTENSIONS.includes(path.extname(entry.name)) ? [fullPath] : [];
  });
}
