-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
LinkFetcher.ts
56 lines (44 loc) Β· 2.42 KB
/
LinkFetcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {Fetcher, FetchOptions, MinimalFetchOptions} from '@yarnpkg/core';
import {Locator} from '@yarnpkg/core';
import {structUtils} from '@yarnpkg/core';
import {JailFS, NodeFS, ppath, PortablePath} from '@yarnpkg/fslib';
import {LINK_PROTOCOL} from './constants';
export class LinkFetcher implements Fetcher {
supports(locator: Locator, opts: MinimalFetchOptions) {
if (!locator.reference.startsWith(LINK_PROTOCOL))
return false;
return true;
}
getLocalPath(locator: Locator, opts: FetchOptions) {
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: LINK_PROTOCOL});
if (ppath.isAbsolute(path))
return path;
const parentLocalPath = opts.fetcher.getLocalPath(parentLocator, opts);
if (parentLocalPath === null)
return null;
return ppath.resolve(parentLocalPath, path);
}
async fetch(locator: Locator, opts: FetchOptions) {
const {parentLocator, path} = structUtils.parseFileStyleRange(locator.reference, {protocol: LINK_PROTOCOL});
// If the link target is an absolute path we can directly access it via its
// location on the disk. Otherwise we must go through the package fs.
const parentFetch = ppath.isAbsolute(path)
? {packageFs: new NodeFS(), prefixPath: PortablePath.root, localPath: PortablePath.root}
: await opts.fetcher.fetch(parentLocator, opts);
// If the package fs publicized its "original location" (for example like
// in the case of "file:" packages), we use it to derive the real location.
const effectiveParentFetch = parentFetch.localPath
? {packageFs: new NodeFS(), prefixPath: parentFetch.localPath}
: parentFetch;
// Discard the parent fs unless we really need it to access the files
if (parentFetch !== effectiveParentFetch && parentFetch.releaseFs)
parentFetch.releaseFs();
const sourceFs = effectiveParentFetch.packageFs;
const sourcePath = ppath.resolve(effectiveParentFetch.prefixPath, path);
if (parentFetch.localPath) {
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.root, localPath: sourcePath};
} else {
return {packageFs: new JailFS(sourcePath, {baseFs: sourceFs}), releaseFs: effectiveParentFetch.releaseFs, prefixPath: PortablePath.root};
}
}
}