mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 15:34:31 +00:00
refactor(daemon): unify runtime binary detection
This commit is contained in:
45
src/daemon/runtime-binary.test.ts
Normal file
45
src/daemon/runtime-binary.test.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isBunRuntime, isNodeRuntime } from "./runtime-binary.js";
|
||||
|
||||
describe("isNodeRuntime", () => {
|
||||
it("recognizes standard node binaries", () => {
|
||||
expect(isNodeRuntime("/usr/bin/node")).toBe(true);
|
||||
expect(isNodeRuntime("C:\\Program Files\\nodejs\\node.exe")).toBe(true);
|
||||
expect(isNodeRuntime("/usr/bin/nodejs")).toBe(true);
|
||||
expect(isNodeRuntime("C:\\nodejs.exe")).toBe(true);
|
||||
});
|
||||
|
||||
it("recognizes versioned node binaries with and without dashes", () => {
|
||||
expect(isNodeRuntime("/usr/bin/node24")).toBe(true);
|
||||
expect(isNodeRuntime("/usr/bin/node-24")).toBe(true);
|
||||
expect(isNodeRuntime("/usr/bin/node24.1")).toBe(true);
|
||||
expect(isNodeRuntime("/usr/bin/node-24.1")).toBe(true);
|
||||
expect(isNodeRuntime("C:\\node24.exe")).toBe(true);
|
||||
expect(isNodeRuntime("C:\\node-24.exe")).toBe(true);
|
||||
});
|
||||
|
||||
it("handles quotes and casing", () => {
|
||||
expect(isNodeRuntime('"/usr/bin/node24"')).toBe(true);
|
||||
expect(isNodeRuntime("'C:\\Program Files\\nodejs\\NODE.EXE'")).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects non-node runtimes", () => {
|
||||
expect(isNodeRuntime("/usr/bin/bun")).toBe(false);
|
||||
expect(isNodeRuntime("/usr/bin/node-dev")).toBe(false);
|
||||
expect(isNodeRuntime("/usr/bin/nodeenv")).toBe(false);
|
||||
expect(isNodeRuntime("/usr/bin/nodemon")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isBunRuntime", () => {
|
||||
it("recognizes bun binaries", () => {
|
||||
expect(isBunRuntime("/usr/bin/bun")).toBe(true);
|
||||
expect(isBunRuntime("C:\\BUN.EXE")).toBe(true);
|
||||
expect(isBunRuntime('"/opt/homebrew/bin/bun"')).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects non-bun runtimes", () => {
|
||||
expect(isBunRuntime("/usr/bin/node")).toBe(false);
|
||||
expect(isBunRuntime("/usr/bin/bunx")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,24 @@
|
||||
import path from "node:path";
|
||||
const NODE_VERSIONED_PATTERN = /^node(?:-\d+|\d+)(?:\.\d+)*(?:\.exe)?$/;
|
||||
|
||||
function normalizeRuntimeBasename(execPath: string): string {
|
||||
const trimmed = execPath.trim().replace(/^["']|["']$/g, "");
|
||||
const lastSlash = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
|
||||
const basename = lastSlash === -1 ? trimmed : trimmed.slice(lastSlash + 1);
|
||||
return basename.toLowerCase();
|
||||
}
|
||||
|
||||
export function isNodeRuntime(execPath: string): boolean {
|
||||
const base = path.basename(execPath).toLowerCase();
|
||||
return base === "node" || base === "node.exe";
|
||||
const base = normalizeRuntimeBasename(execPath);
|
||||
return (
|
||||
base === "node" ||
|
||||
base === "node.exe" ||
|
||||
base === "nodejs" ||
|
||||
base === "nodejs.exe" ||
|
||||
NODE_VERSIONED_PATTERN.test(base)
|
||||
);
|
||||
}
|
||||
|
||||
export function isBunRuntime(execPath: string): boolean {
|
||||
const base = path.basename(execPath).toLowerCase();
|
||||
const base = normalizeRuntimeBasename(execPath);
|
||||
return base === "bun" || base === "bun.exe";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user