@skypack/package-check
Version:
A quality score checker for npm packages.
43 lines • 1.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.repoURL = exports.repoAssetURL = void 0;
const path_1 = __importDefault(require("path"));
/** Get URL for any asset from a repo */
function repoAssetURL(url, filepath, version) {
let sanitizedURL = repoURL(url);
// @ts-ignore this is a real thing
const { hostname, pathname } = new URL(sanitizedURL);
switch (hostname) {
case "bitbucket.org": {
return `https://bitbucket.org${path_1.default.join("/", pathname, version || "HEAD", filepath)}`;
}
case "github.com": {
const [repo, dir] = pathname.split("/tree/");
return `https://raw.githubusercontent.com${path_1.default.join("/", repo, version || "HEAD", dir ? dir.replace(/[^/]+/, "") : "", filepath)}`;
}
case "gitlab.com": {
return `https://gitlab.com${path_1.default.join("/", pathname, "-", "raw", version || "HEAD", filepath)}`;
}
default: {
return undefined; // Dunno what this is; return as-is
}
}
}
exports.repoAssetURL = repoAssetURL;
/** Turn repo URL into normal URL */
function repoURL(url) {
return url
.trim()
.replace(/^git\+/i, "")
.replace(/\.git$/i, "")
.replace(/git@/, "")
.replace(/(bitbucket|github|gitlab)\.([a-z]+):/, "$1.$2/")
.replace(/bitbucket:/, "$1.org/")
.replace(/(github|gitlab):/, "$1.com/")
.replace(/^(http\s*:\/\/|https\s*:\/\/|\/\/)?/, "https://");
}
exports.repoURL = repoURL;
//# sourceMappingURL=get-repo-url.js.map