| 30 | type RootFunction = (file: string, ctx: InstanceContext) => Promise<string | undefined> |
| 31 | |
| 32 | const NearestRoot = (includePatterns: string[], excludePatterns?: string[]): RootFunction => { |
| 33 | return async (file, ctx) => { |
| 34 | if (excludePatterns) { |
| 35 | const excludedFiles = Filesystem.up({ |
| 36 | targets: excludePatterns, |
| 37 | start: path.dirname(file), |
| 38 | stop: ctx.directory, |
| 39 | }) |
| 40 | const excluded = await excludedFiles.next() |
| 41 | await excludedFiles.return() |
| 42 | if (excluded.value) return undefined |
| 43 | } |
| 44 | const files = Filesystem.up({ |
| 45 | targets: includePatterns, |
| 46 | start: path.dirname(file), |
| 47 | stop: ctx.directory, |
| 48 | }) |
| 49 | const first = await files.next() |
| 50 | await files.return() |
| 51 | if (!first.value) return ctx.directory |
| 52 | return path.dirname(first.value) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | const StrictNearestRoot = (includePatterns: string[], excludePatterns?: string[]): RootFunction => { |
| 57 | return async (file, ctx) => { |