Skip to content

Commit 4ba2706

Browse files
authored
refactor(plugin-link)!: rename resolvers and fetchers (#4865)
1 parent 2931e4d commit 4ba2706

File tree

9 files changed

+128
-118
lines changed

9 files changed

+128
-118
lines changed

.yarn/versions/4df7cbc6.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
releases:
2+
"@yarnpkg/plugin-link": major
3+
4+
declined:
5+
- "@yarnpkg/cli"
6+
- "@yarnpkg/core"

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ The following changes only affect people writing Yarn plugins:
5353

5454
- `versionUtils.{fetchBase,fetchRoot,fetchChangedFiles}` have been moved from `@yarnpkg/plugin-version` to `@yarnpkg/plugin-git`. Use `gitUtils.{fetchBase,fetchRoot,fetchChangedFiles}` instead.
5555

56+
- For consistency reasons:
57+
- `Link{Resolver,Fetcher}` have been renamed to `Portal{Resolver,Fetcher}`
58+
- `RawLink{Resolver,Fetcher}` have been renamed to `Link{Resolver,Fetcher}`
59+
5660
### Installs
5761

5862
- The `pnpm` linker avoids creating symlinks that lead to loops on the file system, by moving them higher up in the directory structure.

packages/plugin-link/sources/LinkFetcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export class LinkFetcher implements Fetcher {
5252
);
5353

5454
if (parentFetch.localPath) {
55-
return {packageFs: new CwdFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, localPath: sourcePath};
55+
return {packageFs: new CwdFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, discardFromLookup: true, localPath: sourcePath};
5656
} else {
57-
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot};
57+
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, discardFromLookup: true};
5858
}
5959
}
6060
}

packages/plugin-link/sources/LinkResolver.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {Resolver, ResolveOptions, MinimalResolveOptions} from '@yarnpkg/core';
2-
import {Descriptor, Locator, Manifest, Package} from '@yarnpkg/core';
3-
import {LinkType} from '@yarnpkg/core';
4-
import {miscUtils, structUtils} from '@yarnpkg/core';
5-
import {npath} from '@yarnpkg/fslib';
1+
import {Resolver, ResolveOptions, MinimalResolveOptions, Package} from '@yarnpkg/core';
2+
import {Descriptor, Locator} from '@yarnpkg/core';
3+
import {LinkType} from '@yarnpkg/core';
4+
import {structUtils} from '@yarnpkg/core';
5+
import {npath} from '@yarnpkg/fslib';
66

7-
import {LINK_PROTOCOL} from './constants';
7+
import {LINK_PROTOCOL} from './constants';
88

99
export class LinkResolver implements Resolver {
1010
supportsDescriptor(descriptor: Descriptor, opts: MinimalResolveOptions) {
@@ -50,33 +50,24 @@ export class LinkResolver implements Resolver {
5050
};
5151
}
5252

53-
async resolve(locator: Locator, opts: ResolveOptions): Promise<Package> {
54-
if (!opts.fetchOptions)
55-
throw new Error(`Assertion failed: This resolver cannot be used unless a fetcher is configured`);
56-
57-
const packageFetch = await opts.fetchOptions.fetcher.fetch(locator, opts.fetchOptions);
58-
59-
const manifest = await miscUtils.releaseAfterUseAsync(async () => {
60-
return await Manifest.find(packageFetch.prefixPath, {baseFs: packageFetch.packageFs});
61-
}, packageFetch.releaseFs);
62-
53+
async resolve(locator: Locator, opts: ResolveOptions) {
6354
return {
6455
...locator,
6556

66-
version: manifest.version || `0.0.0`,
57+
version: `0.0.0`,
6758

68-
languageName: manifest.languageName || opts.project.configuration.get(`defaultLanguageName`),
59+
languageName: opts.project.configuration.get(`defaultLanguageName`),
6960
linkType: LinkType.SOFT,
7061

71-
conditions: manifest.getConditions(),
62+
conditions: null,
7263

73-
dependencies: opts.project.configuration.normalizeDependencyMap(manifest.dependencies),
74-
peerDependencies: manifest.peerDependencies,
64+
dependencies: new Map(),
65+
peerDependencies: new Map(),
7566

76-
dependenciesMeta: manifest.dependenciesMeta,
77-
peerDependenciesMeta: manifest.peerDependenciesMeta,
67+
dependenciesMeta: new Map(),
68+
peerDependenciesMeta: new Map(),
7869

79-
bin: manifest.bin,
70+
bin: new Map(),
8071
};
8172
}
8273
}

packages/plugin-link/sources/RawLinkFetcher.ts renamed to packages/plugin-link/sources/PortalFetcher.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import {Locator} from '@yarnpkg/
33
import {structUtils} from '@yarnpkg/core';
44
import {CwdFS, JailFS, ppath, PortablePath} from '@yarnpkg/fslib';
55

6-
import {RAW_LINK_PROTOCOL} from './constants';
6+
import {PORTAL_PROTOCOL} from './constants';
77

8-
export class RawLinkFetcher implements Fetcher {
8+
export class PortalFetcher implements Fetcher {
99
supports(locator: Locator, opts: MinimalFetchOptions) {
10-
if (!locator.reference.startsWith(RAW_LINK_PROTOCOL))
10+
if (!locator.reference.startsWith(PORTAL_PROTOCOL))
1111
return false;
1212

1313
return true;
1414
}
1515

1616
getLocalPath(locator: Locator, opts: FetchOptions) {
17-
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: RAW_LINK_PROTOCOL});
17+
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: PORTAL_PROTOCOL});
1818
if (ppath.isAbsolute(path))
1919
return path;
2020

@@ -26,9 +26,9 @@ export class RawLinkFetcher implements Fetcher {
2626
}
2727

2828
async fetch(locator: Locator, opts: FetchOptions) {
29-
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: RAW_LINK_PROTOCOL});
29+
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: PORTAL_PROTOCOL});
3030

31-
// If the link target is an absolute path we can directly access it via its
31+
// If the portal target is an absolute path we can directly access it via its
3232
// location on the disk. Otherwise we must go through the package fs.
3333
const parentFetch: FetchResult = ppath.isAbsolute(path)
3434
? {packageFs: new CwdFS(PortablePath.root), prefixPath: PortablePath.dot, localPath: PortablePath.root}
@@ -52,9 +52,9 @@ export class RawLinkFetcher implements Fetcher {
5252
);
5353

5454
if (parentFetch.localPath) {
55-
return {packageFs: new CwdFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, discardFromLookup: true, localPath: sourcePath};
55+
return {packageFs: new CwdFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, localPath: sourcePath};
5656
} else {
57-
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot, discardFromLookup: true};
57+
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.dot};
5858
}
5959
}
6060
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {Resolver, ResolveOptions, MinimalResolveOptions} from '@yarnpkg/core';
2+
import {Descriptor, Locator, Manifest, Package} from '@yarnpkg/core';
3+
import {LinkType} from '@yarnpkg/core';
4+
import {miscUtils, structUtils} from '@yarnpkg/core';
5+
import {npath} from '@yarnpkg/fslib';
6+
7+
import {PORTAL_PROTOCOL} from './constants';
8+
9+
export class PortalResolver implements Resolver {
10+
supportsDescriptor(descriptor: Descriptor, opts: MinimalResolveOptions) {
11+
if (!descriptor.range.startsWith(PORTAL_PROTOCOL))
12+
return false;
13+
14+
return true;
15+
}
16+
17+
supportsLocator(locator: Locator, opts: MinimalResolveOptions) {
18+
if (!locator.reference.startsWith(PORTAL_PROTOCOL))
19+
return false;
20+
21+
return true;
22+
}
23+
24+
shouldPersistResolution(locator: Locator, opts: MinimalResolveOptions) {
25+
return false;
26+
}
27+
28+
bindDescriptor(descriptor: Descriptor, fromLocator: Locator, opts: MinimalResolveOptions) {
29+
return structUtils.bindDescriptor(descriptor, {
30+
locator: structUtils.stringifyLocator(fromLocator),
31+
});
32+
}
33+
34+
getResolutionDependencies(descriptor: Descriptor, opts: MinimalResolveOptions) {
35+
return {};
36+
}
37+
38+
async getCandidates(descriptor: Descriptor, dependencies: unknown, opts: ResolveOptions) {
39+
const path = descriptor.range.slice(PORTAL_PROTOCOL.length);
40+
41+
return [structUtils.makeLocator(descriptor, `${PORTAL_PROTOCOL}${npath.toPortablePath(path)}`)];
42+
}
43+
44+
async getSatisfying(descriptor: Descriptor, dependencies: Record<string, Package>, locators: Array<Locator>, opts: ResolveOptions) {
45+
const [locator] = await this.getCandidates(descriptor, dependencies, opts);
46+
47+
return {
48+
locators: locators.filter(candidate => candidate.locatorHash === locator.locatorHash),
49+
sorted: false,
50+
};
51+
}
52+
53+
async resolve(locator: Locator, opts: ResolveOptions): Promise<Package> {
54+
if (!opts.fetchOptions)
55+
throw new Error(`Assertion failed: This resolver cannot be used unless a fetcher is configured`);
56+
57+
const packageFetch = await opts.fetchOptions.fetcher.fetch(locator, opts.fetchOptions);
58+
59+
const manifest = await miscUtils.releaseAfterUseAsync(async () => {
60+
return await Manifest.find(packageFetch.prefixPath, {baseFs: packageFetch.packageFs});
61+
}, packageFetch.releaseFs);
62+
63+
return {
64+
...locator,
65+
66+
version: manifest.version || `0.0.0`,
67+
68+
languageName: manifest.languageName || opts.project.configuration.get(`defaultLanguageName`),
69+
linkType: LinkType.SOFT,
70+
71+
conditions: manifest.getConditions(),
72+
73+
dependencies: opts.project.configuration.normalizeDependencyMap(manifest.dependencies),
74+
peerDependencies: manifest.peerDependencies,
75+
76+
dependenciesMeta: manifest.dependenciesMeta,
77+
peerDependenciesMeta: manifest.peerDependenciesMeta,
78+
79+
bin: manifest.bin,
80+
};
81+
}
82+
}

packages/plugin-link/sources/RawLinkResolver.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export const LINK_PROTOCOL = `portal:`;
1+
export const PORTAL_PROTOCOL = `portal:`;
22

3-
export const RAW_LINK_PROTOCOL = `link:`;
3+
export const LINK_PROTOCOL = `link:`;

packages/plugin-link/sources/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import {Plugin} from '@yarnpkg/core';
1+
import {Plugin} from '@yarnpkg/core';
22

3-
import {LinkFetcher} from './LinkFetcher';
4-
import {LinkResolver} from './LinkResolver';
5-
import {RawLinkFetcher} from './RawLinkFetcher';
6-
import {RawLinkResolver} from './RawLinkResolver';
3+
import {LinkFetcher} from './LinkFetcher';
4+
import {LinkResolver} from './LinkResolver';
5+
import {PortalFetcher} from './PortalFetcher';
6+
import {PortalResolver} from './PortalResolver';
77

8+
export {PortalFetcher};
9+
export {PortalResolver};
810
export {LinkFetcher};
911
export {LinkResolver};
10-
export {RawLinkFetcher};
11-
export {RawLinkResolver};
1212

1313
const plugin: Plugin = {
1414
fetchers: [
15-
RawLinkFetcher,
1615
LinkFetcher,
16+
PortalFetcher,
1717
],
1818
resolvers: [
19-
RawLinkResolver,
2019
LinkResolver,
20+
PortalResolver,
2121
],
2222
};
2323

0 commit comments

Comments
 (0)