import * as fs from 'fs' ; import * as path from 'path' ; param targetDir returns export function deepReadDirSync ( targetDir ) { const targetDirAbsPath = path . resolve ( targetDir ) ; if ( ! fs . existsSync ( targetDirAbsPath ) ) { throw new Error ( `Cannot read contents of ${ targetDirAbsPath }. Directory does not exist.` ) ; } if ( ! fs . statSync ( targetDirAbsPath ) . isDirectory ( ) ) { throw new Error ( `Cannot read contents of ${ targetDirAbsPath }, because it is not a directory.` ) ; } const deepReadCurrentDir = ( currentDirAbsPath ) : string [ ] => { return fs . readdirSync ( currentDirAbsPath ) . reduce ( ( absPaths , itemName ) => { const itemAbsPath = path . join ( currentDirAbsPath , itemName ) ; if ( fs . statSync ( itemAbsPath ) . isDirectory ( ) ) { return [ ... absPaths , ... deepReadCurrentDir ( itemAbsPath ) ] ; } return [ ... absPaths , itemAbsPath ] ; } , [ ] ) ; } ; return deepReadCurrentDir ( targetDirAbsPath ) . map ( absPath => path . relative ( targetDirAbsPath , absPath ) ) ; }