Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallbacks to npm when COREPACK_NPM_REGISTRY is set #339

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@
"versions": "tags"
}
},
"npmRegistry": {
"type": "npm",
"package": "@yarnpkg/cli-dist"
},
"commands": {
"use": [
"yarn",
Expand Down
10 changes: 8 additions & 2 deletions sources/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ export class Engine {
const ranges = Object.keys(definition.ranges);
const tagRange = ranges[ranges.length - 1];

const tags = await corepackUtils.fetchAvailableTags(definition.ranges[tagRange].registry);
const packageManagerSpec = definition.ranges[tagRange];
const registry = corepackUtils.getRegistryFromPackageManagerSpec(packageManagerSpec);

const tags = await corepackUtils.fetchAvailableTags(registry);
if (!Object.prototype.hasOwnProperty.call(tags, descriptor.range))
throw new UsageError(`Tag not found (${descriptor.range})`);

Expand All @@ -178,7 +181,10 @@ export class Engine {
return {name: finalDescriptor.name, reference: finalDescriptor.range};

const versions = await Promise.all(Object.keys(definition.ranges).map(async range => {
const versions = await corepackUtils.fetchAvailableVersions(definition.ranges[range].registry);
const packageManagerSpec = definition.ranges[range];
const registry = corepackUtils.getRegistryFromPackageManagerSpec(packageManagerSpec);

const versions = await corepackUtils.fetchAvailableVersions(registry);
return versions.filter(version => semverUtils.satisfiesWithPrereleases(version, finalDescriptor.range));
}));

Expand Down
6 changes: 6 additions & 0 deletions sources/corepackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import * as nodeUtils from './nodeUtils
import * as npmRegistryUtils from './npmRegistryUtils';
import {RegistrySpec, Descriptor, Locator, PackageManagerSpec} from './types';

export function getRegistryFromPackageManagerSpec(spec: PackageManagerSpec) {
return process.env.COREPACK_NPM_REGISTRY
? spec.npmRegistry ?? spec.registry
: spec.registry;
}

export async function fetchLatestStableVersion(spec: RegistrySpec): Promise<string> {
switch (spec.type) {
case `npm`: {
Expand Down
1 change: 1 addition & 0 deletions sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface PackageManagerSpec {
url: string;
bin: BinSpec | BinList;
registry: RegistrySpec;
npmRegistry?: NpmRegistrySpec;
commands?: {
use?: Array<string>;
};
Expand Down