forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.ts
More file actions
53 lines (48 loc) · 1.4 KB
/
runtime.ts
File metadata and controls
53 lines (48 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { clearActiveProgressLine } from "./terminal/progress-line.js";
import { restoreTerminalState } from "./terminal/restore.js";
export type RuntimeEnv = {
log: (...args: unknown[]) => void;
error: (...args: unknown[]) => void;
exit: (code: number) => void;
};
function shouldEmitRuntimeLog(env: NodeJS.ProcessEnv = process.env): boolean {
if (env.VITEST !== "true") {
return true;
}
if (env.OPENCLAW_TEST_RUNTIME_LOG === "1") {
return true;
}
const maybeMockedLog = console.log as unknown as { mock?: unknown };
return typeof maybeMockedLog.mock === "object";
}
function createRuntimeIo(): Pick<RuntimeEnv, "log" | "error"> {
return {
log: (...args: Parameters<typeof console.log>) => {
if (!shouldEmitRuntimeLog()) {
return;
}
clearActiveProgressLine();
console.log(...args);
},
error: (...args: Parameters<typeof console.error>) => {
clearActiveProgressLine();
console.error(...args);
},
};
}
export const defaultRuntime: RuntimeEnv = {
...createRuntimeIo(),
exit: (code) => {
restoreTerminalState("runtime exit", { resumeStdinIfPaused: false });
process.exit(code);
throw new Error("unreachable"); // satisfies tests when mocked
},
};
export function createNonExitingRuntime(): RuntimeEnv {
return {
...createRuntimeIo(),
exit: (code: number) => {
throw new Error(`exit ${code}`);
},
};
}