Skip to content

Commit be3ee24

Browse files
committed
resume mode
1 parent 351c262 commit be3ee24

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

packages/playground/evaluate/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dirname, join } from "node:path";
2-
import { fileURLToPath } from "url";
2+
import { fileURLToPath } from "node:url";
33

44
export interface Config {
55
/** if `Infinity`, the fun never stops. otherwise it will forcibly halt at a the specified instructions count */
@@ -45,6 +45,7 @@ export const stringResultTypeName = `String${resultTypeName}`;
4545
export const simpleTypeMode = process.argv.includes('--simple');
4646
export const statsOnlyMode = process.argv.includes("--stats-only");
4747
export const reportDiagnosticsMode = process.argv.includes("--report-ts-diagnostics");
48+
export const resumeMode = process.argv.includes("--resume");
4849

4950
const __filename = fileURLToPath(import.meta.url);
5051
const __dirname = dirname(__filename);

packages/playground/evaluate/run.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { join } from "path";
2-
import { statSync, mkdirSync, readdirSync, unlinkSync } from "fs";
1+
import { join } from "node:path";
2+
import { statSync, mkdirSync, readdirSync, unlinkSync } from "node:fs";
33
import {
44
clearStats,
55
encourage,
@@ -8,14 +8,14 @@ import {
88
} from "./stats";
99
import {
1010
config,
11-
startFilePath,
1211
resultsDirectory,
1312
simpleTypeMode,
1413
statsOnlyMode,
1514
reportDiagnosticsMode,
15+
resumeMode,
1616
} from "./config";
1717
import { Meter } from "./metering";
18-
import { getFuncImportLine, printType, programIsComplete, ProgramRun } from './utils';
18+
import { getStartFilePath as getStartFilePath, getFuncImportLine, printType, programIsComplete, ProgramRun } from './utils';
1919
import { createEnv, createNewFile, evaluateType, reportErrors } from "./ts";
2020

2121
const isolatedProgram = async ({
@@ -128,6 +128,7 @@ const clearResults = () => {
128128
};
129129

130130
const bootstrap = async () => {
131+
const startFilePath = getStartFilePath();
131132
const startProgramTime = performance.now();
132133
const env = createEnv(startFilePath);
133134
const program = env.languageService.getProgram();
@@ -191,8 +192,10 @@ const mainLoop = async () => {
191192

192193
const startProgram = async () => {
193194
encourage();
194-
clearResults();
195-
clearStats();
195+
if (!resumeMode) {
196+
clearResults();
197+
clearStats();
198+
}
196199
await mainLoop();
197200
};
198201

packages/playground/evaluate/utils.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

2-
import { readFileSync, writeFileSync } from "node:fs";
2+
import { readdirSync, readFileSync, writeFileSync } from "node:fs";
33
import { Biome, Distribution } from "@biomejs/js-api";
4-
import { config, createResultFilePath, errorFilePath, finalResultPath, formatterErrorFilePath, shouldTakeABreath, startFilePath } from "./config";
4+
import { config, createResultFilePath, errorFilePath, finalResultPath, formatterErrorFilePath, resultsDirectory, resumeMode, shouldTakeABreath, startFilePath } from "./config";
55
import { VirtualTypeScriptEnvironment } from "@typescript/vfs";
66
import ts from "typescript";
77
import { inspect } from "node:util";
8+
import { join } from "node:path";
89

910
export interface ProgramRun {
1011
env: VirtualTypeScriptEnvironment;
@@ -172,7 +173,8 @@ export const finalizeProgram = async ({
172173
resultTypeName: string;
173174
env: VirtualTypeScriptEnvironment;
174175
}) => {
175-
const relativeLastResult = createResultFilePath(lastInstructionCount)
176+
const absoluteLastResult = createResultFilePath(lastInstructionCount)
177+
const relativeLastResult = absoluteLastResult.replace(resultsDirectory, ".");
176178

177179
const readStringImportLine = config.readStringFromMemory
178180
? [
@@ -252,4 +254,27 @@ export const printType = (typeString: string) => {
252254
}
253255

254256
return output;
255-
};
257+
};
258+
259+
export const getStartFilePath = () => {
260+
if (!resumeMode) {
261+
return startFilePath;
262+
}
263+
264+
const files = readdirSync(resultsDirectory);
265+
266+
const resumeIndex = process.argv.indexOf("--resume");
267+
const maybeResumeFile = process.argv[resumeIndex + 1];
268+
if (maybeResumeFile) {
269+
if (!files.includes(maybeResumeFile)) {
270+
throw new Error(`specified resume file '${maybeResumeFile}' not found in ${resultsDirectory}`);
271+
}
272+
return join(resultsDirectory, maybeResumeFile);
273+
}
274+
275+
const filenames = files.filter(file => {
276+
return file.startsWith("result-") && file.endsWith(".ts");
277+
}).sort();
278+
279+
return join(resultsDirectory, filenames[filenames.length - 1]);
280+
}

0 commit comments

Comments
 (0)