keycloak_theme/src/bin/tools/getAbsoluteAndInOsFormatPath.ts

33 lines
689 B
TypeScript
Raw Normal View History

2024-05-20 15:48:51 +02:00
import {
isAbsolute as pathIsAbsolute,
sep as pathSep,
join as pathJoin,
resolve as pathResolve
} from "path";
import * as os from "os";
2023-09-03 23:26:34 +02:00
2024-05-20 15:48:51 +02:00
export function getAbsoluteAndInOsFormatPath(params: {
pathIsh: string;
cwd: string;
}): string {
2023-09-03 23:26:34 +02:00
const { pathIsh, cwd } = params;
let pathOut = pathIsh;
pathOut = pathOut.replace(/\//g, pathSep);
if (pathOut.startsWith("~")) {
pathOut = pathOut.replace("~", os.homedir());
}
2024-01-30 05:54:36 +01:00
pathOut = pathOut.endsWith(pathSep) ? pathOut.slice(0, -1) : pathOut;
2023-09-03 23:26:34 +02:00
if (!pathIsAbsolute(pathOut)) {
pathOut = pathJoin(cwd, pathOut);
}
pathOut = pathResolve(pathOut);
2023-09-03 23:26:34 +02:00
return pathOut;
}