esc-get-matching-files-array
Version:
Sync function that returns an array of files matching regex pattern, recursively inside specified directory.
10 lines (7 loc) • 402 B
JavaScript
import fs from 'node:fs';
import path from 'node:path';
export const getMatchingFilesA = (dirPath, filenameRegex = /\.m?js$/) => (fs.readdirSync( dirPath, { withFileTypes: true } ).flatMap( fileO => {
const absPath = path.join( dirPath, fileO.name );
return fileO.isDirectory() ? getFilesA( absPath ) : filenameRegex.test( fileO.name ) ? [ absPath ] : [];
} ));
export default getMatchingFilesA;