40 lines
No EOL
1.2 KiB
JavaScript
40 lines
No EOL
1.2 KiB
JavaScript
import path from "node:path";
|
|
import os from "node:os";
|
|
const isWindows = /win/.test(os.platform());
|
|
// Deep convert any Windows path to Unix path in a string or object
|
|
export function convertToUnixPaths(obj) {
|
|
if (!isWindows) {
|
|
return obj;
|
|
}
|
|
if (typeof obj === "string") {
|
|
return obj.replace(/\\\\?/g, "/");
|
|
}
|
|
else if (Array.isArray(obj)) {
|
|
return obj.map((value) => convertToUnixPaths(value));
|
|
}
|
|
else if (typeof obj === "object") {
|
|
for (const [k, v] of Object.entries(obj)) {
|
|
obj[k] = convertToUnixPaths(v);
|
|
}
|
|
}
|
|
return obj;
|
|
}
|
|
// Deep convert unix paths to native path in a string or object
|
|
export function convertToNativePaths(obj) {
|
|
if (!isWindows) {
|
|
return obj;
|
|
}
|
|
if (typeof obj === "string") {
|
|
return /^https?:\/\//.test(obj) ? obj : obj.replace(/\//g, path.sep);
|
|
}
|
|
else if (Array.isArray(obj)) {
|
|
return obj.map((value) => convertToNativePaths(value));
|
|
}
|
|
else if (typeof obj === "object") {
|
|
for (const [k, v] of Object.entries(obj)) {
|
|
obj[k] = convertToNativePaths(v);
|
|
}
|
|
}
|
|
return obj;
|
|
}
|
|
//# sourceMappingURL=test.helpers.js.map
|