-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
VirtualFetcher.ts
55 lines (40 loc) Β· 2.02 KB
/
VirtualFetcher.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
import {AliasFS, VirtualFS, ppath} from '@yarnpkg/fslib';
import {Fetcher, FetchOptions, FetchResult} from './Fetcher';
import * as structUtils from './structUtils';
import {Locator} from './types';
export class VirtualFetcher implements Fetcher {
supports(locator: Locator) {
if (!locator.reference.startsWith(`virtual:`))
return false;
return true;
}
getLocalPath(locator: Locator, opts: FetchOptions) {
const splitPoint = locator.reference.indexOf(`#`);
if (splitPoint === -1)
throw new Error(`Invalid virtual package reference`);
const nextReference = locator.reference.slice(splitPoint + 1);
const nextLocator = structUtils.makeLocator(locator, nextReference);
return opts.fetcher.getLocalPath(nextLocator, opts);
}
async fetch(locator: Locator, opts: FetchOptions) {
const splitPoint = locator.reference.indexOf(`#`);
if (splitPoint === -1)
throw new Error(`Invalid virtual package reference`);
const nextReference = locator.reference.slice(splitPoint + 1);
const nextLocator = structUtils.makeLocator(locator, nextReference);
const parentFetch = await opts.fetcher.fetch(nextLocator, opts);
return await this.ensureVirtualLink(locator, parentFetch, opts);
}
getLocatorFilename(locator: Locator) {
return structUtils.slugifyLocator(locator);
}
private async ensureVirtualLink(locator: Locator, sourceFetch: FetchResult, opts: FetchOptions) {
const to = sourceFetch.packageFs.getRealPath();
const virtualFolder = opts.project.configuration.get(`virtualFolder`);
const virtualName = this.getLocatorFilename(locator);
const virtualPath = VirtualFS.makeVirtualPath(virtualFolder, virtualName, to);
// We then use an alias to tell anyone that asks us that we're operating within the virtual folder, while still using the same old fs
const aliasFs = new AliasFS(virtualPath, {baseFs: sourceFetch.packageFs, pathUtils: ppath});
return {...sourceFetch, packageFs: aliasFs};
}
}