Skip to content

Commit

Permalink
feat(yarn): fallback to npm when COREPACK_NPM_REGISTRY is set (#339)
Browse files Browse the repository at this point in the history
Yarn publishing its versions on its own was inconvenient for people using private npm mirrors, so it makes sense to offer a way to fallback to the npm store when requested. This diff changes the logic so that we use the `@yarnpkg/cli-dist` package when the `COREPACK_NPM_REGISTRY` variable is set. It should be backward-compatible, since the `registry` field is still the same.
  • Loading branch information
arcanis committed Dec 29, 2023
1 parent adcd989 commit 0717c6a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
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

0 comments on commit 0717c6a

Please sign in to comment.