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 | module.exports = function resolve(dirname) {
|
23 |
|
24 | if (process.env.APP_ROOT_PATH) {
|
25 | return path.resolve(process.env.APP_ROOT_PATH);
|
26 | }
|
27 |
|
28 |
|
29 | if ('undefined' !== typeof window && window.process && 'renderer' === window.process.type) {
|
30 | var electron = 'electron';
|
31 | var remote = require(electron).remote;
|
32 | return remote.require('app-root-path').path;
|
33 | }
|
34 |
|
35 |
|
36 | if (process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV) {
|
37 | return process.env.LAMBDA_TASK_ROOT;
|
38 | }
|
39 |
|
40 | var resolved = path.resolve(dirname);
|
41 | var alternateMethod = false;
|
42 | var appRootPath = null;
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | globalPaths.forEach(function(globalPath) {
|
49 | if (!alternateMethod && 0 === resolved.indexOf(globalPath)) {
|
50 | alternateMethod = true;
|
51 | }
|
52 | });
|
53 |
|
54 |
|
55 |
|
56 | var nodeModulesDir = sep + 'node_modules';
|
57 | if (!alternateMethod && -1 !== resolved.indexOf(nodeModulesDir)) {
|
58 | var parts = resolved.split(nodeModulesDir);
|
59 | if (parts.length) {
|
60 | appRootPath = parts[0];
|
61 | parts = null;
|
62 | }
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 | if (alternateMethod || null == appRootPath) {
|
68 | appRootPath = path.dirname(require.main.filename);
|
69 | }
|
70 |
|
71 |
|
72 | if (alternateMethod && -1 !== appRootPath.indexOf(npmGlobalModuleDir) && (appRootPath.length - 4) === appRootPath.indexOf(sep + 'bin')) {
|
73 | appRootPath = appRootPath.slice(0, -4);
|
74 | }
|
75 |
|
76 |
|
77 | return appRootPath;
|
78 | };
|