|
1 | | -import * as fs from "fs" |
| 1 | +import { exists, log } from "builder-util" |
2 | 2 | import { PackageJson } from "./types" |
| 3 | +import * as fs from "fs-extra" |
| 4 | +import { resolve } from "path" |
3 | 5 |
|
4 | | -/** |
5 | | - * Unified cache for all file system and module resolution operations |
6 | | - */ |
7 | | -export type ModuleCache = { |
| 6 | +// Type aliases for clarity |
| 7 | +type PackageJsonCache = Record<string, Promise<PackageJson>> |
| 8 | +type RealPathCache = Record<string, Promise<string>> |
| 9 | +type ExistsCache = Record<string, Promise<boolean>> |
| 10 | +type LstatCache = Record<string, Promise<fs.Stats>> |
| 11 | +type RequireResolveCache = Record<string, Promise<string | null>> |
| 12 | + |
| 13 | +export class ModuleCache { |
8 | 14 | /** Cache for package.json contents (readJson/require) */ |
9 | | - packageJson: Map<string, PackageJson> |
| 15 | + readonly packageJson: PackageJsonCache |
10 | 16 | /** Cache for resolved real paths (realpath) */ |
11 | | - realPath: Map<string, string> |
| 17 | + readonly realPath: RealPathCache |
12 | 18 | /** Cache for file/directory existence checks */ |
13 | | - exists: Map<string, boolean> |
| 19 | + readonly exists: ExistsCache |
14 | 20 | /** Cache for lstat results */ |
15 | | - lstat: Map<string, fs.Stats> |
| 21 | + readonly lstat: LstatCache |
16 | 22 | /** Cache for require.resolve results (key: "packageName::fromDir") */ |
17 | | - requireResolve: Map<string, { entry: string; packageDir: string } | null> |
18 | | -} |
| 23 | + readonly requireResolve: RequireResolveCache |
| 24 | + |
| 25 | + private readonly packageJsonMap: Map<string, PackageJson> = new Map() |
| 26 | + private readonly realPathMap: Map<string, string> = new Map() |
| 27 | + private readonly existsMap: Map<string, boolean> = new Map() |
| 28 | + private readonly lstatMap: Map<string, fs.Stats> = new Map() |
| 29 | + private readonly requireResolveMap: Map<string, string | null> = new Map() |
| 30 | + |
| 31 | + constructor() { |
| 32 | + this.packageJson = this.createAsyncProxy(this.packageJsonMap, (path: string) => fs.readJson(path)) |
| 33 | + this.exists = this.createAsyncProxy(this.existsMap, (path: string) => exists(path)) |
| 34 | + this.lstat = this.createAsyncProxy(this.lstatMap, (path: string) => fs.lstat(path)) |
| 35 | + this.requireResolve = this.createAsyncProxy(this.requireResolveMap, (path: string) => require.resolve(path)) |
| 36 | + this.realPath = this.createAsyncProxy(this.realPathMap, async (path: string) => { |
| 37 | + const p = resolve(path) |
| 38 | + try { |
| 39 | + const stats = await this.lstat[p] |
| 40 | + if (stats.isSymbolicLink()) { |
| 41 | + return await fs.realpath(p) |
| 42 | + } |
| 43 | + return p |
| 44 | + } catch (error: any) { |
| 45 | + log.debug({ filePath: p, message: error.message || error.stack }, "error resolving path") |
| 46 | + } |
| 47 | + return p |
| 48 | + }) |
| 49 | + } |
19 | 50 |
|
20 | | -/** |
21 | | - * Creates a new empty ModuleCache instance |
22 | | - */ |
23 | | -export function createModuleCache(): ModuleCache { |
24 | | - return { |
25 | | - packageJson: new Map(), |
26 | | - realPath: new Map(), |
27 | | - exists: new Map(), |
28 | | - lstat: new Map(), |
29 | | - requireResolve: new Map(), |
| 51 | + // this allows dot-notation access while still supporting async retrieval |
| 52 | + // e.g., cache.packageJson[somePath] returns Promise<PackageJson> |
| 53 | + private createAsyncProxy<T>(map: Map<string, T>, compute: (key: string) => T | Promise<T>): Record<string, Promise<T>> { |
| 54 | + return new Proxy({} as Record<string, Promise<T>>, { |
| 55 | + async get(_, key: string) { |
| 56 | + if (map.has(key)) { |
| 57 | + return Promise.resolve(map.get(key)!) |
| 58 | + } |
| 59 | + return await Promise.resolve(compute(key)).then(value => { |
| 60 | + map.set(key, value) |
| 61 | + return value |
| 62 | + }) |
| 63 | + }, |
| 64 | + set(_, key: string, value: T) { |
| 65 | + map.set(key, value) |
| 66 | + return true |
| 67 | + }, |
| 68 | + has(_, key: string) { |
| 69 | + return map.has(key) |
| 70 | + }, |
| 71 | + }) |
30 | 72 | } |
31 | 73 | } |
0 commit comments