2026-01-18 03:55:09 +00:00
|
|
|
import { clearActiveProgressLine } from "./terminal/progress-line.js";
|
2026-02-03 06:10:19 +00:00
|
|
|
import { restoreTerminalState } from "./terminal/restore.js";
|
2026-01-18 03:55:09 +00:00
|
|
|
|
2025-11-25 02:16:54 +01:00
|
|
|
export type RuntimeEnv = {
|
2025-11-26 00:53:53 +01:00
|
|
|
log: typeof console.log;
|
|
|
|
|
error: typeof console.error;
|
|
|
|
|
exit: (code: number) => never;
|
2025-11-25 02:16:54 +01:00
|
|
|
};
|
|
|
|
|
|
2026-02-14 01:14:51 +00:00
|
|
|
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";
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 02:16:54 +01:00
|
|
|
export const defaultRuntime: RuntimeEnv = {
|
2026-01-18 03:55:09 +00:00
|
|
|
log: (...args: Parameters<typeof console.log>) => {
|
2026-02-14 01:14:51 +00:00
|
|
|
if (!shouldEmitRuntimeLog()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-18 03:55:09 +00:00
|
|
|
clearActiveProgressLine();
|
|
|
|
|
console.log(...args);
|
|
|
|
|
},
|
|
|
|
|
error: (...args: Parameters<typeof console.error>) => {
|
|
|
|
|
clearActiveProgressLine();
|
|
|
|
|
console.error(...args);
|
|
|
|
|
},
|
2025-11-26 00:53:53 +01:00
|
|
|
exit: (code) => {
|
2026-02-14 20:33:07 +01:00
|
|
|
restoreTerminalState("runtime exit", { resumeStdinIfPaused: false });
|
2025-11-26 00:53:53 +01:00
|
|
|
process.exit(code);
|
|
|
|
|
throw new Error("unreachable"); // satisfies tests when mocked
|
|
|
|
|
},
|
2025-11-25 02:16:54 +01:00
|
|
|
};
|
2026-02-14 01:14:51 +00:00
|
|
|
|
|
|
|
|
export function createNonExitingRuntime(): RuntimeEnv {
|
|
|
|
|
return {
|
|
|
|
|
log: (...args: Parameters<typeof console.log>) => {
|
|
|
|
|
if (!shouldEmitRuntimeLog()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
clearActiveProgressLine();
|
|
|
|
|
console.log(...args);
|
|
|
|
|
},
|
|
|
|
|
error: (...args: Parameters<typeof console.error>) => {
|
|
|
|
|
clearActiveProgressLine();
|
|
|
|
|
console.error(...args);
|
|
|
|
|
},
|
|
|
|
|
exit: (code: number): never => {
|
|
|
|
|
throw new Error(`exit ${code}`);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|