-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
resolve-package.es6.js
49 lines (43 loc) · 1.2 KB
/
resolve-package.es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const { existsSync, readdirSync } = require('fs-extra');
/**
* Find full path for package file.
* Replacement for require.resolve(), as it is broken for packages with "exports" property.
*
* @param {string} relativePath Relative path to the file to resolve, in format packageName/file-name.js
* @returns {string|boolean}
*/
module.exports.resolvePackageFile = (relativePath) => {
for (let i = 0, l = module.paths.length; i < l; i += 1) {
const path = module.paths[i];
const fullPath = `${path}/${relativePath}`;
if (existsSync(fullPath)) {
return fullPath;
}
}
return false;
};
/**
* Find a list of modules under given scope,
* eg: @foobar will look for all submodules @foobar/foo, @foobar/bar
*
* @param scope
* @returns {[]}
*/
module.exports.getPackagesUnderScope = (scope) => {
const cmModules = [];
// Get the scope roots
const roots = [];
module.paths.forEach((path) => {
const fullPath = `${path}/${scope}`;
if (existsSync(fullPath)) {
roots.push(fullPath);
}
});
// List of modules
roots.forEach((rootPath) => {
readdirSync(rootPath).forEach((subModule) => {
cmModules.push(`${scope}/${subModule}`);
});
});
return cmModules;
};