mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-26 20:35:39 -06:00
23 lines
713 B
TypeScript
23 lines
713 B
TypeScript
import path from "node:path";
|
|
|
|
/**
|
|
* On Windows, Node's ESM loader requires absolute paths to be expressed as
|
|
* file:// URLs. Raw drive-letter paths like C:\... are parsed as URL schemes.
|
|
*/
|
|
export function toSafeImportPath(specifier: string): string {
|
|
if (process.platform !== "win32") {
|
|
return specifier;
|
|
}
|
|
if (specifier.startsWith("file://")) {
|
|
return specifier;
|
|
}
|
|
if (path.win32.isAbsolute(specifier)) {
|
|
const normalizedSpecifier = specifier.replaceAll("\\", "/");
|
|
if (normalizedSpecifier.startsWith("//")) {
|
|
return new URL(`file:${encodeURI(normalizedSpecifier)}`).href;
|
|
}
|
|
return new URL(`file:///${encodeURI(normalizedSpecifier)}`).href;
|
|
}
|
|
return specifier;
|
|
}
|