(filePath: string)
| 24 | * @returns File content or null if file doesn't exist |
| 25 | */ |
| 26 | export function getCachedFileContent(filePath: string): string | null { |
| 27 | try { |
| 28 | // Normalize the path for cross-platform compatibility |
| 29 | const normalizedPath = path.resolve(filePath); |
| 30 | |
| 31 | // Check if file exists |
| 32 | if (!existsSync(normalizedPath)) { |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | // Check cache first |
| 37 | const now = Date.now(); |
| 38 | const cached = globalFileCache.get(normalizedPath); |
| 39 | |
| 40 | if (cached) { |
| 41 | // Check if cache is still valid (TTL and file modification time) |
| 42 | const stats = statSync(normalizedPath); |
| 43 | const fileModTime = stats.mtime.getTime(); |
| 44 | |
| 45 | if (now - cached.timestamp < CACHE_TTL && cached.mtime === fileModTime) { |
| 46 | return cached.content; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Read file and update cache |
| 51 | const content = readFileSync(normalizedPath, 'utf-8'); |
| 52 | const stats = statSync(normalizedPath); |
| 53 | |
| 54 | globalFileCache.set(normalizedPath, { |
| 55 | content, |
| 56 | mtime: stats.mtime.getTime(), |
| 57 | timestamp: now |
| 58 | }); |
| 59 | |
| 60 | return content; |
| 61 | |
| 62 | } catch (error) { |
| 63 | return null; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check if a file exists (with caching for stat calls) |
no outgoing calls
no test coverage detected