import fs from "fs" ; import path from "path" ; import { promisify } from "util" ; import { resolve } from "path" ; const readdir = promisify ( fs . readdir ) ; const stat = promisify ( fs . stat ) ; async function getFiles ( dir ) { const subdirs = await readdir ( dir ) ; const files = await Promise . all ( subdirs . map ( async ( subdir ) => { const res = resolve ( dir , subdir ) ; return ( await stat ( res ) ) . isDirectory ( ) ? getFiles ( res ) : res ; } ) ) ; return files . reduce ( ( a , f ) => a . concat ( f ) , [ ] as string [ ] ) ; } export default async function collectFiles ( paths ) { const filesArr = await Promise . all ( paths . include . map ( includeDir => getFiles ( path . join ( paths . rootDir , includeDir ) ) ) ) ; const files = filesArr . reduce ( ( a , f ) => a . concat ( f ) , [ ] as string [ ] ) ; const filesWithExtensions = files . filter ( f => { return paths . extensions . some ( e => f . endsWith ( e ) ) ; } ) ; const filesWithoutExclusions = filesWithExtensions . filter ( f => { return ! paths . exclude . some ( e => f . includes ( e ) ) ; } ) ; return filesWithoutExclusions ; }