UNPKG

2.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.VirtualFetcher = void 0;
4const tslib_1 = require("tslib");
5const fslib_1 = require("@yarnpkg/fslib");
6const structUtils = tslib_1.__importStar(require("./structUtils"));
7class VirtualFetcher {
8 supports(locator) {
9 if (!locator.reference.startsWith(`virtual:`))
10 return false;
11 return true;
12 }
13 getLocalPath(locator, opts) {
14 const splitPoint = locator.reference.indexOf(`#`);
15 if (splitPoint === -1)
16 throw new Error(`Invalid virtual package reference`);
17 const nextReference = locator.reference.slice(splitPoint + 1);
18 const nextLocator = structUtils.makeLocator(locator, nextReference);
19 return opts.fetcher.getLocalPath(nextLocator, opts);
20 }
21 async fetch(locator, opts) {
22 const splitPoint = locator.reference.indexOf(`#`);
23 if (splitPoint === -1)
24 throw new Error(`Invalid virtual package reference`);
25 const nextReference = locator.reference.slice(splitPoint + 1);
26 const nextLocator = structUtils.makeLocator(locator, nextReference);
27 const parentFetch = await opts.fetcher.fetch(nextLocator, opts);
28 return await this.ensureVirtualLink(locator, parentFetch, opts);
29 }
30 getLocatorFilename(locator) {
31 return structUtils.slugifyLocator(locator);
32 }
33 async ensureVirtualLink(locator, sourceFetch, opts) {
34 const to = sourceFetch.packageFs.getRealPath();
35 const virtualFolder = opts.project.configuration.get(`virtualFolder`);
36 const virtualName = this.getLocatorFilename(locator);
37 const virtualPath = fslib_1.VirtualFS.makeVirtualPath(virtualFolder, virtualName, to);
38 // 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
39 const aliasFs = new fslib_1.AliasFS(virtualPath, { baseFs: sourceFetch.packageFs, pathUtils: fslib_1.ppath });
40 return { ...sourceFetch, packageFs: aliasFs };
41 }
42}
43exports.VirtualFetcher = VirtualFetcher;