|
1 | 1 | import { readdir, stat as statAsync } from "fs/promises"; |
2 | | -import { join } from "path"; |
| 2 | +import { join, resolve } from "path"; |
3 | 3 |
|
4 | 4 | // Efficient parallel folder traversal to find node_modules directories |
5 | 5 | async function findNodeModulesDirs( |
@@ -88,7 +88,7 @@ async function findDenoRescriptRuntime(nodeModulesPath: string) { |
88 | 88 | return results; |
89 | 89 | } |
90 | 90 |
|
91 | | -export async function findRuntime(project: string) { |
| 91 | +async function findRuntimePath(project: string) { |
92 | 92 | // Find all node_modules directories using efficient traversal |
93 | 93 | const node_modules = await findNodeModulesDirs(project); |
94 | 94 |
|
@@ -117,5 +117,24 @@ export async function findRuntime(project: string) { |
117 | 117 | }), |
118 | 118 | ).then((results) => results.flatMap((x) => x)); |
119 | 119 |
|
120 | | - return rescriptRuntimeDirs; |
| 120 | + return rescriptRuntimeDirs.map((runtime) => resolve(runtime)); |
121 | 121 | } |
| 122 | + |
| 123 | +function findRuntimeCached() { |
| 124 | + const cache = new Map<string, string[]>(); |
| 125 | + return async (project: string) => { |
| 126 | + if (cache.has(project)) { |
| 127 | + return cache.get(project)!; |
| 128 | + } |
| 129 | + const runtimes = await findRuntimePath(project); |
| 130 | + cache.set(project, runtimes); |
| 131 | + return runtimes; |
| 132 | + }; |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Find all installed @rescript/runtime directories in the given project path. |
| 137 | + * In a perfect world, there should be exactly one. |
| 138 | + * This function is cached per project path. |
| 139 | + */ |
| 140 | +export const findRescriptRuntimesInProject = findRuntimeCached(); |
0 commit comments