| 21 | type RootFunction = (file: string) => Promise<string | undefined> |
| 22 | |
| 23 | const NearestRoot = (includePatterns: string[], excludePatterns?: string[]): RootFunction => { |
| 24 | return async (file) => { |
| 25 | if (excludePatterns) { |
| 26 | const excludedFiles = Filesystem.up({ |
| 27 | targets: excludePatterns, |
| 28 | start: path.dirname(file), |
| 29 | stop: Instance.directory, |
| 30 | }) |
| 31 | const excluded = await excludedFiles.next() |
| 32 | await excludedFiles.return() |
| 33 | if (excluded.value) return undefined |
| 34 | } |
| 35 | const files = Filesystem.up({ |
| 36 | targets: includePatterns, |
| 37 | start: path.dirname(file), |
| 38 | stop: Instance.directory, |
| 39 | }) |
| 40 | const first = await files.next() |
| 41 | await files.return() |
| 42 | if (!first.value) return Instance.directory |
| 43 | return path.dirname(first.value) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | export interface Info { |
| 48 | id: string |