query-registry
Version:
Query the npm registry for packuments, manifests, packages and download counts
89 lines (73 loc) • 2.03 kB
JavaScript
import urlJoin from 'url-join';
function normalizeRawRepository({
rawRepository
}) {
if (isRepository(rawRepository)) {
return normalizeRepository({
rawRepository
});
}
if (typeof rawRepository === 'string') {
return normalizeRepository({
rawRepository: {
url: rawRepository
}
});
}
return undefined;
}
function isRepository(rawRepository) {
return rawRepository && typeof rawRepository === 'object' && typeof rawRepository['url'] === 'string' && ['string', 'undefined'].includes(typeof rawRepository['type']) && ['string', 'undefined'].includes(typeof rawRepository['directory']);
}
function normalizeRepository({
rawRepository
}) {
const {
url,
directory
} = rawRepository;
const parsedUrl = parseGitURL({
url
});
if (!parsedUrl) {
return undefined;
}
return {
type: 'git',
url: parsedUrl,
directory
};
}
function parseGitURL({
url
}) {
const urlWithProtocol = url.includes(':') ? // A normal URL or a shortcut like `github:user/repository`
url : // The short form github shortcut `user/repository`
url.includes('/') ? `github:${url}` : // Not a URL
'';
try {
const {
protocol,
hostname,
pathname
} = new URL(urlWithProtocol);
const cleanPathname = pathname.replace(/\.git$/, '');
if (protocol === 'github:' || hostname === 'github.com') {
return urlJoin('https://github.com', cleanPathname);
}
if (protocol === 'gist:' || hostname === 'gist.github.com') {
return urlJoin('https://gist.github.com', cleanPathname);
}
if (protocol === 'bitbucket:' || hostname === 'bitbucket.org') {
return urlJoin('https://bitbucket.org', cleanPathname);
}
if (protocol === 'gitlab:' || hostname === 'gitlab.com') {
return urlJoin('https://gitlab.com', cleanPathname);
}
return urlWithProtocol;
} catch {
return undefined;
}
}
export { normalizeRawRepository };
//# sourceMappingURL=normalize-raw-repository.esm.js.map