mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 08:30:44 +00:00
Merged via squash.
Prepared head SHA: ebf93ad913
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
26 lines
750 B
TypeScript
26 lines
750 B
TypeScript
import { createRequire } from "node:module";
|
|
import path from "node:path";
|
|
|
|
const nodeRequire = createRequire(import.meta.url);
|
|
|
|
export function isJavaScriptModulePath(modulePath: string): boolean {
|
|
return [".js", ".mjs", ".cjs"].includes(path.extname(modulePath).toLowerCase());
|
|
}
|
|
|
|
export function tryNativeRequireJavaScriptModule(
|
|
modulePath: string,
|
|
options: { allowWindows?: boolean } = {},
|
|
): { ok: true; moduleExport: unknown } | { ok: false } {
|
|
if (process.platform === "win32" && options.allowWindows !== true) {
|
|
return { ok: false };
|
|
}
|
|
if (!isJavaScriptModulePath(modulePath)) {
|
|
return { ok: false };
|
|
}
|
|
try {
|
|
return { ok: true, moduleExport: nodeRequire(modulePath) };
|
|
} catch {
|
|
return { ok: false };
|
|
}
|
|
}
|