import { join } from 'path'; import { forkJoin, Observable, of, UnaryFunction } from 'rxjs'; import { catchError, map, mapTo, mergeMap } from 'rxjs/operators'; import { rxStats } from './rx.walk'; /** * Locates the first directory that contains a certain file * * @param aSources the list of source files to check * @param aRelPath the relative path * * @return the observable */ export function rxFindDir(aSources: Observable, aRelPath: string, aMkdirp: UnaryFunction>): Observable { // map each source to a check return aSources.pipe( mergeMap(sources => forkJoin( sources.map(dir => rxStats(join(dir, aRelPath)).pipe(mapTo(dir), catchError(err => of(null)))) ).pipe( map(result => result.filter(Boolean)), map(result => (result.length > 0) ? result[0]! : sources[0]!) )), mergeMap(aMkdirp) ); }