Recursively load a memory file and all its @includes.
( filePath: string, type: MemoryFile["type"], processed: Set<string>, depth: number, )
| 71 | |
| 72 | /** Recursively load a memory file and all its @includes. */ |
| 73 | function loadWithIncludes( |
| 74 | filePath: string, |
| 75 | type: MemoryFile["type"], |
| 76 | processed: Set<string>, |
| 77 | depth: number, |
| 78 | ): MemoryFile[] { |
| 79 | const real = safeRealpath(filePath) |
| 80 | if (processed.has(real) || depth >= MAX_INCLUDE_DEPTH) return [] |
| 81 | const raw = readText(filePath) |
| 82 | if (raw === undefined) return [] |
| 83 | processed.add(real) |
| 84 | |
| 85 | const baseDir = path.dirname(filePath) |
| 86 | const includePaths = extractIncludes(raw, baseDir) |
| 87 | const results: MemoryFile[] = [] |
| 88 | |
| 89 | // Includes come first (so they appear before the including file's content). |
| 90 | for (const includePath of includePaths) { |
| 91 | results.push(...loadWithIncludes(includePath, type, processed, depth + 1)) |
| 92 | } |
| 93 | |
| 94 | results.push({ path: filePath, content: raw, type }) |
| 95 | return results |
| 96 | } |
| 97 | |
| 98 | /** realpathSync that never throws (falls back to the input path). */ |
| 99 | function safeRealpath(p: string): string { |
no test coverage detected