@bomb.sh/tools
Version:
The internal dev, build, and lint CLI for Bombshell projects
39 lines (38 loc) • 1.49 kB
JavaScript
import { fileURLToPath, pathToFileURL } from "node:url";
//#region src/utils.ts
function local(file) {
return fileURLToPath(new URL(`../node_modules/.bin/${file}`, import.meta.url));
}
/** Error class for `bsh` failures. Carries a stable, greppable code. */
var ToolsError = class extends Error {
code;
constructor(message, code) {
super(message);
this.name = "ToolsError";
this.code = code;
}
};
/**
* Compute a relative path from one directory URL to another, for APIs that
* require a string path (e.g. `fs.symlink` targets). POSIX-style — forward
* slashes are accepted by every platform Node supports for this purpose.
*/
function relativeUrlPath(from, to) {
const fromParts = from.pathname.split("/").filter(Boolean);
const toParts = to.pathname.split("/").filter(Boolean);
let common = 0;
while (common < fromParts.length && common < toParts.length && fromParts[common] === toParts[common]) common++;
const ups = fromParts.length - common;
return [...Array(ups).fill(".."), ...toParts.slice(common)].map(decodeURIComponent).join("/");
}
/**
* Resolve a symlink target (as returned by `fs.readlink`, which may be
* relative to the link's directory) to an absolute URL.
*/
function resolveLinkTarget(linkDir, target) {
if (target.startsWith("/") || /^[a-zA-Z]:[\\/]/.test(target)) return pathToFileURL(target);
return new URL(target, linkDir);
}
//#endregion
export { ToolsError, local, relativeUrlPath, resolveLinkTarget };
//# sourceMappingURL=utils.mjs.map