2026-01-18 18:21:13 +00:00
|
|
|
#!/usr/bin/env node
|
2026-02-14 01:18:20 +00:00
|
|
|
import { spawn, spawnSync } from "node:child_process";
|
2026-01-31 21:21:09 +09:00
|
|
|
import fs from "node:fs";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
import process from "node:process";
|
2026-02-14 15:20:30 +00:00
|
|
|
import { pathToFileURL } from "node:url";
|
2026-01-18 18:21:13 +00:00
|
|
|
|
chore: Migrate to tsdown, speed up JS bundling by ~10x (thanks @hyf0).
The previous migration to tsdown was reverted because it caused a ~20x slowdown when running OpenClaw from the repo. @hyf0 investigated and found that simply renaming the `dist` folder also caused the same slowdown. It turns out the Plugin script loader has a bunch of voodoo vibe logic to determine if it should load files from source and compile them, or if it should load them from dist. When building with tsdown, the filesystem layout is different (bundled), and so some files weren't in the right location, and the Plugin script loader decided to compile source files from scratch using Jiti.
The new implementation uses tsdown to embed `NODE_ENV: 'production'`, which we now use to determine if we are running OpenClaw from a "production environmen" (ie. from dist). This removes the slop in favor of a deterministic toggle, and doesn't rely on directory names or similar.
There is some code reaching into `dist` to load specific modules, primarily in the voice-call extension, which I simplified into loading an "officially" exported `extensionAPI.js` file. With tsdown, entry points need to be explicitly configured, so we should be able to avoid sloppy code reaching into internals from now on. This might break some existing users, but if it does, it's because they were using "private" APIs.
2026-02-02 17:20:24 +09:00
|
|
|
const compiler = "tsdown";
|
2026-02-06 01:14:00 -05:00
|
|
|
const compilerArgs = ["exec", compiler, "--no-clean"];
|
2026-01-18 18:21:13 +00:00
|
|
|
|
2026-02-14 01:18:20 +00:00
|
|
|
const gitWatchedPaths = ["src", "tsconfig.json", "package.json"];
|
2026-01-18 18:21:13 +00:00
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const statMtime = (filePath, fsImpl = fs) => {
|
2026-01-19 13:12:33 -06:00
|
|
|
try {
|
2026-02-14 15:20:30 +00:00
|
|
|
return fsImpl.statSync(filePath).mtimeMs;
|
2026-01-19 13:12:33 -06:00
|
|
|
} catch {
|
|
|
|
|
return null;
|
2026-01-18 18:21:13 +00:00
|
|
|
}
|
2026-01-19 13:12:33 -06:00
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const isExcludedSource = (filePath, srcRoot) => {
|
2026-01-19 13:12:33 -06:00
|
|
|
const relativePath = path.relative(srcRoot, filePath);
|
2026-01-31 21:29:14 +09:00
|
|
|
if (relativePath.startsWith("..")) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-01-19 13:12:33 -06:00
|
|
|
return (
|
2026-01-31 21:21:09 +09:00
|
|
|
relativePath.endsWith(".test.ts") ||
|
|
|
|
|
relativePath.endsWith(".test.tsx") ||
|
2026-01-19 13:12:33 -06:00
|
|
|
relativePath.endsWith(`test-helpers.ts`)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const findLatestMtime = (dirPath, shouldSkip, deps) => {
|
2026-01-19 13:12:33 -06:00
|
|
|
let latest = null;
|
|
|
|
|
const queue = [dirPath];
|
|
|
|
|
while (queue.length > 0) {
|
|
|
|
|
const current = queue.pop();
|
2026-01-31 21:29:14 +09:00
|
|
|
if (!current) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-19 13:12:33 -06:00
|
|
|
let entries = [];
|
|
|
|
|
try {
|
2026-02-14 15:20:30 +00:00
|
|
|
entries = deps.fs.readdirSync(current, { withFileTypes: true });
|
2026-01-19 13:12:33 -06:00
|
|
|
} catch {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
|
const fullPath = path.join(current, entry.name);
|
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
|
queue.push(fullPath);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-31 21:29:14 +09:00
|
|
|
if (!entry.isFile()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (shouldSkip?.(fullPath)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-02-14 15:20:30 +00:00
|
|
|
const mtime = statMtime(fullPath, deps.fs);
|
2026-01-31 21:29:14 +09:00
|
|
|
if (mtime == null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-19 13:12:33 -06:00
|
|
|
if (latest == null || mtime > latest) {
|
|
|
|
|
latest = mtime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return latest;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const runGit = (gitArgs, deps) => {
|
2026-02-14 01:18:20 +00:00
|
|
|
try {
|
2026-02-14 15:20:30 +00:00
|
|
|
const result = deps.spawnSync("git", gitArgs, {
|
|
|
|
|
cwd: deps.cwd,
|
2026-02-14 01:18:20 +00:00
|
|
|
encoding: "utf8",
|
|
|
|
|
stdio: ["ignore", "pipe", "ignore"],
|
|
|
|
|
});
|
|
|
|
|
if (result.status !== 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return (result.stdout ?? "").trim();
|
|
|
|
|
} catch {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const resolveGitHead = (deps) => {
|
|
|
|
|
const head = runGit(["rev-parse", "HEAD"], deps);
|
2026-02-14 01:18:20 +00:00
|
|
|
return head || null;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const hasDirtySourceTree = (deps) => {
|
|
|
|
|
const output = runGit(
|
|
|
|
|
["status", "--porcelain", "--untracked-files=normal", "--", ...gitWatchedPaths],
|
|
|
|
|
deps,
|
|
|
|
|
);
|
2026-02-14 01:18:20 +00:00
|
|
|
if (output === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return output.length > 0;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const readBuildStamp = (deps) => {
|
|
|
|
|
const mtime = statMtime(deps.buildStampPath, deps.fs);
|
2026-02-14 01:18:20 +00:00
|
|
|
if (mtime == null) {
|
|
|
|
|
return { mtime: null, head: null };
|
|
|
|
|
}
|
|
|
|
|
try {
|
2026-02-14 15:20:30 +00:00
|
|
|
const raw = deps.fs.readFileSync(deps.buildStampPath, "utf8").trim();
|
2026-02-14 01:18:20 +00:00
|
|
|
if (!raw.startsWith("{")) {
|
|
|
|
|
return { mtime, head: null };
|
|
|
|
|
}
|
|
|
|
|
const parsed = JSON.parse(raw);
|
|
|
|
|
const head = typeof parsed?.head === "string" && parsed.head.trim() ? parsed.head.trim() : null;
|
|
|
|
|
return { mtime, head };
|
|
|
|
|
} catch {
|
|
|
|
|
return { mtime, head: null };
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const hasSourceMtimeChanged = (stampMtime, deps) => {
|
|
|
|
|
const srcMtime = findLatestMtime(
|
|
|
|
|
deps.srcRoot,
|
|
|
|
|
(candidate) => isExcludedSource(candidate, deps.srcRoot),
|
|
|
|
|
deps,
|
|
|
|
|
);
|
2026-02-14 01:18:20 +00:00
|
|
|
return srcMtime != null && srcMtime > stampMtime;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const shouldBuild = (deps) => {
|
|
|
|
|
if (deps.env.OPENCLAW_FORCE_BUILD === "1") {
|
2026-01-31 21:29:14 +09:00
|
|
|
return true;
|
|
|
|
|
}
|
2026-02-14 15:20:30 +00:00
|
|
|
const stamp = readBuildStamp(deps);
|
2026-02-14 01:18:20 +00:00
|
|
|
if (stamp.mtime == null) {
|
2026-01-31 21:29:14 +09:00
|
|
|
return true;
|
|
|
|
|
}
|
2026-02-14 15:20:30 +00:00
|
|
|
if (statMtime(deps.distEntry, deps.fs) == null) {
|
2026-01-31 21:29:14 +09:00
|
|
|
return true;
|
|
|
|
|
}
|
2026-01-19 13:12:33 -06:00
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
for (const filePath of deps.configFiles) {
|
|
|
|
|
const mtime = statMtime(filePath, deps.fs);
|
2026-02-14 01:18:20 +00:00
|
|
|
if (mtime != null && mtime > stamp.mtime) {
|
2026-01-31 21:29:14 +09:00
|
|
|
return true;
|
|
|
|
|
}
|
2026-01-18 18:21:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const currentHead = resolveGitHead(deps);
|
2026-02-14 01:18:20 +00:00
|
|
|
if (currentHead && !stamp.head) {
|
2026-02-14 15:20:30 +00:00
|
|
|
return hasSourceMtimeChanged(stamp.mtime, deps);
|
2026-02-14 01:18:20 +00:00
|
|
|
}
|
|
|
|
|
if (currentHead && stamp.head && currentHead !== stamp.head) {
|
2026-02-14 15:20:30 +00:00
|
|
|
return hasSourceMtimeChanged(stamp.mtime, deps);
|
2026-02-14 01:18:20 +00:00
|
|
|
}
|
|
|
|
|
if (currentHead) {
|
2026-02-14 15:20:30 +00:00
|
|
|
const dirty = hasDirtySourceTree(deps);
|
2026-02-14 01:18:20 +00:00
|
|
|
if (dirty === true) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (dirty === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
if (hasSourceMtimeChanged(stamp.mtime, deps)) {
|
2026-01-31 21:29:14 +09:00
|
|
|
return true;
|
|
|
|
|
}
|
2026-01-19 13:12:33 -06:00
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const logRunner = (message, deps) => {
|
|
|
|
|
if (deps.env.OPENCLAW_RUNNER_LOG === "0") {
|
2026-01-31 21:29:14 +09:00
|
|
|
return;
|
|
|
|
|
}
|
2026-02-14 15:20:30 +00:00
|
|
|
deps.stderr.write(`[openclaw] ${message}\n`);
|
2026-01-19 13:12:33 -06:00
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const runOpenClaw = async (deps) => {
|
|
|
|
|
const nodeProcess = deps.spawn(deps.execPath, ["openclaw.mjs", ...deps.args], {
|
|
|
|
|
cwd: deps.cwd,
|
|
|
|
|
env: deps.env,
|
2026-01-31 21:21:09 +09:00
|
|
|
stdio: "inherit",
|
2026-01-18 18:21:13 +00:00
|
|
|
});
|
2026-02-14 15:20:30 +00:00
|
|
|
const res = await new Promise((resolve) => {
|
|
|
|
|
nodeProcess.on("exit", (exitCode, exitSignal) => {
|
|
|
|
|
resolve({ exitCode, exitSignal });
|
|
|
|
|
});
|
2026-01-18 18:21:13 +00:00
|
|
|
});
|
2026-02-14 15:20:30 +00:00
|
|
|
if (res.exitSignal) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return res.exitCode ?? 1;
|
2026-01-19 13:12:33 -06:00
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const writeBuildStamp = (deps) => {
|
2026-01-19 13:12:33 -06:00
|
|
|
try {
|
2026-02-14 15:20:30 +00:00
|
|
|
deps.fs.mkdirSync(deps.distRoot, { recursive: true });
|
2026-02-14 01:18:20 +00:00
|
|
|
const stamp = {
|
|
|
|
|
builtAt: Date.now(),
|
2026-02-14 15:20:30 +00:00
|
|
|
head: resolveGitHead(deps),
|
2026-02-14 01:18:20 +00:00
|
|
|
};
|
2026-02-14 15:20:30 +00:00
|
|
|
deps.fs.writeFileSync(deps.buildStampPath, `${JSON.stringify(stamp)}\n`);
|
2026-01-19 13:12:33 -06:00
|
|
|
} catch (error) {
|
|
|
|
|
// Best-effort stamp; still allow the runner to start.
|
2026-02-14 15:20:30 +00:00
|
|
|
logRunner(`Failed to write build stamp: ${error?.message ?? "unknown error"}`, deps);
|
2026-01-19 13:12:33 -06:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
export async function runNodeMain(params = {}) {
|
|
|
|
|
const deps = {
|
|
|
|
|
spawn: params.spawn ?? spawn,
|
|
|
|
|
spawnSync: params.spawnSync ?? spawnSync,
|
|
|
|
|
fs: params.fs ?? fs,
|
|
|
|
|
stderr: params.stderr ?? process.stderr,
|
|
|
|
|
execPath: params.execPath ?? process.execPath,
|
|
|
|
|
cwd: params.cwd ?? process.cwd(),
|
|
|
|
|
args: params.args ?? process.argv.slice(2),
|
|
|
|
|
env: params.env ? { ...params.env } : { ...process.env },
|
|
|
|
|
platform: params.platform ?? process.platform,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
deps.distRoot = path.join(deps.cwd, "dist");
|
|
|
|
|
deps.distEntry = path.join(deps.distRoot, "/entry.js");
|
|
|
|
|
deps.buildStampPath = path.join(deps.distRoot, ".buildstamp");
|
|
|
|
|
deps.srcRoot = path.join(deps.cwd, "src");
|
|
|
|
|
deps.configFiles = [path.join(deps.cwd, "tsconfig.json"), path.join(deps.cwd, "package.json")];
|
|
|
|
|
|
|
|
|
|
if (!shouldBuild(deps)) {
|
|
|
|
|
return await runOpenClaw(deps);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logRunner("Building TypeScript (dist is stale).", deps);
|
|
|
|
|
const buildCmd = deps.platform === "win32" ? "cmd.exe" : "pnpm";
|
2026-01-22 22:11:15 -06:00
|
|
|
const buildArgs =
|
2026-02-14 15:20:30 +00:00
|
|
|
deps.platform === "win32" ? ["/d", "/s", "/c", "pnpm", ...compilerArgs] : compilerArgs;
|
|
|
|
|
const build = deps.spawn(buildCmd, buildArgs, {
|
|
|
|
|
cwd: deps.cwd,
|
|
|
|
|
env: deps.env,
|
2026-01-31 21:21:09 +09:00
|
|
|
stdio: "inherit",
|
2026-01-19 13:12:33 -06:00
|
|
|
});
|
|
|
|
|
|
2026-02-14 15:20:30 +00:00
|
|
|
const buildRes = await new Promise((resolve) => {
|
|
|
|
|
build.on("exit", (exitCode, exitSignal) => resolve({ exitCode, exitSignal }));
|
2026-01-19 13:12:33 -06:00
|
|
|
});
|
2026-02-14 15:20:30 +00:00
|
|
|
if (buildRes.exitSignal) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if (buildRes.exitCode !== 0 && buildRes.exitCode !== null) {
|
|
|
|
|
return buildRes.exitCode;
|
|
|
|
|
}
|
|
|
|
|
writeBuildStamp(deps);
|
|
|
|
|
return await runOpenClaw(deps);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
|
|
|
|
|
void runNodeMain()
|
|
|
|
|
.then((code) => process.exit(code))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.error(err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
2026-01-19 13:12:33 -06:00
|
|
|
}
|