1 | 'use strict';
|
2 |
|
3 |
|
4 | var path = require('path');
|
5 |
|
6 |
|
7 | var globalPaths = require('module').globalPaths;
|
8 |
|
9 |
|
10 | var npmGlobalPrefix;
|
11 | if ('win32' === process.platform) {
|
12 | npmGlobalPrefix = path.dirname(process.execPath);
|
13 | } else {
|
14 | npmGlobalPrefix = path.dirname(path.dirname(process.execPath));
|
15 | }
|
16 | var npmGlobalModuleDir = path.resolve(npmGlobalPrefix, 'lib', 'node_modules');
|
17 |
|
18 |
|
19 | var sep = path.sep;
|
20 |
|
21 |
|
22 | var requireFunction = ("function" === typeof __webpack_require__ || "function" === typeof __non_webpack_require__)
|
23 | ? __non_webpack_require__
|
24 | : require;
|
25 |
|
26 | const isInstalledWithPNPM = function(resolved) {
|
27 | const pnpmDir = sep + '.pnpm';
|
28 |
|
29 | for (const globalPath of globalPaths) {
|
30 | if (-1 !== globalPath.indexOf(pnpmDir) && -1 !== resolved.indexOf(pnpmDir)) {
|
31 | return true;
|
32 | }
|
33 | }
|
34 | return false;
|
35 | }
|
36 |
|
37 | const getFirstPartFromNodeModules = function(resolved) {
|
38 | const nodeModulesDir = sep + 'node_modules';
|
39 |
|
40 | if (-1 !== resolved.indexOf(nodeModulesDir)) {
|
41 | const parts = resolved.split(nodeModulesDir);
|
42 | if (parts.length) {
|
43 | return parts[0];
|
44 | }
|
45 | }
|
46 |
|
47 | return null;
|
48 | }
|
49 |
|
50 |
|
51 | module.exports = function resolve(dirname) {
|
52 |
|
53 | if (process.env.APP_ROOT_PATH) {
|
54 | return path.resolve(process.env.APP_ROOT_PATH);
|
55 | }
|
56 |
|
57 |
|
58 | if (process.versions.pnp) {
|
59 | try {
|
60 | var pnp = requireFunction('pnpapi');
|
61 | return pnp.getPackageInformation(pnp.topLevel).packageLocation;
|
62 | } catch (e) {}
|
63 | }
|
64 |
|
65 |
|
66 | if ('undefined' !== typeof window && window.process && 'renderer' === window.process.type) {
|
67 | try {
|
68 | var remote = requireFunction('electron').remote;
|
69 | return remote.require('app-root-path').path;
|
70 | } catch (e) {}
|
71 | }
|
72 |
|
73 |
|
74 | if (process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV) {
|
75 | return process.env.LAMBDA_TASK_ROOT;
|
76 | }
|
77 |
|
78 | var resolved = path.resolve(dirname);
|
79 | var alternateMethod = false;
|
80 | var appRootPath = null;
|
81 |
|
82 |
|
83 |
|
84 | if (isInstalledWithPNPM(resolved)) {
|
85 | appRootPath = getFirstPartFromNodeModules(resolved);
|
86 |
|
87 | if (appRootPath) {
|
88 | return appRootPath;
|
89 | }
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | globalPaths.forEach(function(globalPath) {
|
97 | if (!alternateMethod && 0 === resolved.indexOf(globalPath)) {
|
98 | alternateMethod = true;
|
99 | }
|
100 | });
|
101 |
|
102 |
|
103 |
|
104 | if (!alternateMethod) {
|
105 | appRootPath = getFirstPartFromNodeModules(resolved);
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 | if ((alternateMethod || null == appRootPath)) {
|
111 | if (requireFunction.main) {
|
112 | appRootPath = path.dirname(requireFunction.main.filename);
|
113 | } else {
|
114 |
|
115 |
|
116 |
|
117 | appRootPath = path.dirname(process.argv[1]);
|
118 | }
|
119 | }
|
120 |
|
121 |
|
122 | if (alternateMethod && -1 !== appRootPath.indexOf(npmGlobalModuleDir) && (appRootPath.length - 4) === appRootPath.indexOf(sep + 'bin')) {
|
123 | appRootPath = appRootPath.slice(0, -4);
|
124 | }
|
125 |
|
126 |
|
127 | return appRootPath;
|
128 | };
|