UNPKG

3.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.resolveNodeModulesPath = void 0;
4const fslib_1 = require("@yarnpkg/fslib");
5const fslib_2 = require("@yarnpkg/fslib");
6const NODE_MODULES = `node_modules`;
7var LinkType;
8(function (LinkType) {
9 LinkType["HARD"] = "HARD";
10 LinkType["SOFT"] = "SOFT";
11})(LinkType || (LinkType = {}));
12/**
13 * Resolves paths containing `/node_modules` inside PnP projects. If path is outside PnP
14 * project it is not changed.
15 *
16 * @param inputPath full path containing `node_modules`
17 *
18 * @returns resolved path
19 */
20const 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 // We have the situation .../node_modules/{something or @something}
29 lastIdx++;
30 if (segments[lastIdx][0] === `@` && typeof segments[lastIdx + 1] !== `undefined`)
31 // We have the situation .../node_modules/@something/{foo}
32 lastIdx++;
33 // We lookup all the path substrings that end on [firstIdx..lastIdx] in the node_modules tree
34 // and follow them if they are symlinks
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 // If node_modules is inside .zip archive, we use parent folder as a statPath instead
65 if (result.forwardedDirPath.endsWith(`.zip`)) {
66 result.forwardedDirPath = fslib_2.ppath.dirname(result.forwardedDirPath);
67 }
68 }
69 }
70 return result;
71};
72exports.resolveNodeModulesPath = resolveNodeModulesPath;