Skip to content

Commit b0ec8de

Browse files
committed
Cache runtime results
1 parent c50424d commit b0ec8de

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

scripts/find-runtime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Examples:
1919

2020
const project = args[args.length - 1];
2121

22-
import { findRuntime } from "../server/src/find-runtime.ts";
22+
import { findRescriptRuntimesInProject } from "../server/src/find-runtime.ts";
2323

24-
const runtimes = await findRuntime(project);
24+
const runtimes = await findRescriptRuntimesInProject(project);
2525

2626
console.log("Found @rescript/runtime directories:", runtimes);
2727

server/src/bsc-args/rewatch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
IncrementallyCompiledFileInfo,
88
} from "../incrementalCompilation";
99
import type { projectFiles } from "../projectFiles";
10-
import { findRuntime } from "../find-runtime";
10+
import { findRescriptRuntimesInProject } from "../find-runtime";
1111

1212
export type RewatchCompilerArgs = {
1313
compiler_args: Array<string>;
@@ -93,8 +93,9 @@ export async function getRewatchBscArgs(
9393
// TODO: We should check a potential configured value
9494
// Users should be able to provide this themselves if they like.
9595

96-
// TODO: We should also cache this value if we found it.
97-
const rescriptRuntimes = await findRuntime(entry.project.workspaceRootPath);
96+
const rescriptRuntimes = await findRescriptRuntimesInProject(
97+
entry.project.workspaceRootPath,
98+
);
9899

99100
if (debug()) {
100101
if (rescriptRuntimes.length === 0) {

server/src/find-runtime.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { readdir, stat as statAsync } from "fs/promises";
2-
import { join } from "path";
2+
import { join, resolve } from "path";
33

44
// Efficient parallel folder traversal to find node_modules directories
55
async function findNodeModulesDirs(
@@ -88,7 +88,7 @@ async function findDenoRescriptRuntime(nodeModulesPath: string) {
8888
return results;
8989
}
9090

91-
export async function findRuntime(project: string) {
91+
async function findRuntimePath(project: string) {
9292
// Find all node_modules directories using efficient traversal
9393
const node_modules = await findNodeModulesDirs(project);
9494

@@ -117,5 +117,24 @@ export async function findRuntime(project: string) {
117117
}),
118118
).then((results) => results.flatMap((x) => x));
119119

120-
return rescriptRuntimeDirs;
120+
return rescriptRuntimeDirs.map((runtime) => resolve(runtime));
121121
}
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

Comments
 (0)