pncat
Version:
A unified cli tool that enhances package managers catalogs feature.
35 lines (34 loc) • 1.56 kB
JavaScript
//#region node_modules/.pnpm/fast-npm-meta@0.4.7/node_modules/fast-npm-meta/dist/index.mjs
const defaultOptions = { apiEndpoint: "https://npm.antfu.dev/" };
async function getLatestVersionBatch(packages, options = {}) {
const { apiEndpoint = defaultOptions.apiEndpoint, fetch: fetchApi = fetch, throw: throwError = true } = options;
let query = [
options.force ? "force=true" : "",
options.metadata ? "metadata=true" : "",
throwError ? "" : "throw=false"
].filter(Boolean).join("&");
if (query) query = `?${query}`;
const list = toArray(await fetchApi(new URL(packages.join("+") + query, apiEndpoint)).then((r) => r.json()));
return throwError ? throwErrorObject(list) : list;
}
async function getLatestVersion(name, options = {}) {
const [data] = await getLatestVersionBatch([name], options);
return data;
}
function throwErrorObject(data) {
for (const item of toArray(data)) if (item && "error" in item) throw new Error(item.message || item.error);
return data;
}
function toArray(data) {
if (Array.isArray(data)) return data;
return [data];
}
const NPM_REGISTRY = "https://registry.npmjs.org/";
function pickRegistry(scope, npmConfigs, defaultRegistry = NPM_REGISTRY) {
let registry = scope ? npmConfigs[`${scope.replace(/^@?/, "@")}:registry`] : void 0;
if (!registry && typeof npmConfigs.scope === "string") registry = npmConfigs[`${npmConfigs.scope.replace(/^@?/, "@")}:registry`];
if (!registry) registry = npmConfigs.registry || defaultRegistry;
return registry;
}
//#endregion
export { NPM_REGISTRY, getLatestVersion, pickRegistry };