1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.resolveNodeModulesPath = void 0;
|
4 | const fslib_1 = require("@yarnpkg/fslib");
|
5 | const fslib_2 = require("@yarnpkg/fslib");
|
6 | const NODE_MODULES = `node_modules`;
|
7 | var LinkType;
|
8 | (function (LinkType) {
|
9 | LinkType["HARD"] = "HARD";
|
10 | LinkType["SOFT"] = "SOFT";
|
11 | })(LinkType || (LinkType = {}));
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const resolveNodeModulesPath = (inputPath, nodeModulesTree) => {
|
21 | const result = { resolvedPath: inputPath };
|
22 | const segments = inputPath.split(fslib_2.ppath.sep);
|
23 | const firstIdx = segments.indexOf(NODE_MODULES);
|
24 | if (firstIdx < 0)
|
25 | return result;
|
26 | let lastIdx = segments.lastIndexOf(NODE_MODULES);
|
27 | if (typeof segments[lastIdx + 1] !== `undefined`)
|
28 |
|
29 | lastIdx++;
|
30 | if (segments[lastIdx][0] === `@` && typeof segments[lastIdx + 1] !== `undefined`)
|
31 |
|
32 | lastIdx++;
|
33 |
|
34 |
|
35 | let locationCandidate = fslib_2.npath.toPortablePath(segments.slice(0, firstIdx).join(fslib_2.ppath.sep));
|
36 | let node, lastNode, lastNodeLocation;
|
37 | let curIdx = firstIdx;
|
38 | let request = fslib_1.PortablePath.dot;
|
39 | while (curIdx <= lastIdx) {
|
40 | const curSegment = segments[curIdx];
|
41 | locationCandidate = fslib_2.ppath.join(locationCandidate, curSegment);
|
42 | node = nodeModulesTree.get(locationCandidate);
|
43 | if (node) {
|
44 | if (node.linkType === LinkType.SOFT)
|
45 | locationCandidate = node.target;
|
46 | lastNode = node;
|
47 | request = fslib_1.PortablePath.dot;
|
48 | lastNodeLocation = node.dirList ? locationCandidate : node.target;
|
49 | }
|
50 | else {
|
51 | request = fslib_2.ppath.join(request, curSegment);
|
52 | }
|
53 | curIdx++;
|
54 | }
|
55 | request = fslib_2.ppath.join(request, ...segments.slice(lastIdx + 1).map(x => x));
|
56 | if (lastNode) {
|
57 | if (!lastNode.dirList || request !== fslib_1.PortablePath.dot) {
|
58 | result.resolvedPath = fslib_2.ppath.join(lastNodeLocation, request);
|
59 | result.isSymlink = lastNode && lastNode.linkType === LinkType.SOFT && request === fslib_1.PortablePath.dot;
|
60 | }
|
61 | else if (request === fslib_1.PortablePath.dot) {
|
62 | result.dirList = lastNode.dirList;
|
63 | result.forwardedDirPath = fslib_2.npath.toPortablePath(segments.slice(0, firstIdx).join(fslib_2.ppath.sep));
|
64 |
|
65 | if (result.forwardedDirPath.endsWith(`.zip`)) {
|
66 | result.forwardedDirPath = fslib_2.ppath.dirname(result.forwardedDirPath);
|
67 | }
|
68 | }
|
69 | }
|
70 | return result;
|
71 | };
|
72 | exports.resolveNodeModulesPath = resolveNodeModulesPath;
|