1 | ;
|
2 | var path = require('path');
|
3 |
|
4 | /*
|
5 | * @private
|
6 | *
|
7 | * when using require.resolve, the entry files location is provided. This
|
8 | * utility attempts to locate the true root directory.
|
9 | *
|
10 | * given: /path/to/project/node_modules/library/lib/index.js + library
|
11 | * we get: /path/to/project/node_modules/library
|
12 | *
|
13 | * @method moduleBaseDir
|
14 | * @param modulePath {String}
|
15 | * @param moduleName {String}
|
16 | * @return {String} the root directory of a given module
|
17 | *
|
18 | */
|
19 | module.exports = function moduleBaseDir(modulePath, moduleName) {
|
20 | var joinedModulePath = path.join(modulePath, moduleName);
|
21 |
|
22 | // if moduleName contains something like '..'
|
23 | if (joinedModulePath.length < modulePath.length) {
|
24 | return joinedModulePath;
|
25 | }
|
26 |
|
27 | // path.join covers both linux and windows
|
28 | var segment = path.join('node_modules', moduleName);
|
29 |
|
30 | // this will match windows only and have no effect on linux
|
31 | var regexSegment = segment.replace(/\\/g, '\\\\');
|
32 |
|
33 | modulePath = modulePath.replace(new RegExp(regexSegment + '.*$'), segment);
|
34 | modulePath = modulePath.replace(/index\.js\/?$/, '');
|
35 |
|
36 | return modulePath;
|
37 | };
|