-
Notifications
You must be signed in to change notification settings - Fork 17
/
find-parent-modules.js
37 lines (29 loc) · 1.18 KB
/
find-parent-modules.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
const fs = require('fs');
const path = require('path');
const util = require('util');
const fsExists = util.promisify(fs.exists);
const fsReaddir = util.promisify(fs.readdir);
// Looks for node_modules in parent folders of the workspace recursively.
// Returns a list of paths relative to workspaceRoot/nodeModulesPath
const findParentModules = async (workspaceRoot, nodeModulesPath) => {
const rootDirectoryPath = path.parse(process.cwd()).root.toLowerCase();
const absoluteRootNodeModules = path.join(rootDirectoryPath, nodeModulesPath);
const find = async dir => {
const ret = [];
if (await fsExists(dir)) {
const getFilePath = file =>
path.relative(path.join(workspaceRoot, nodeModulesPath), path.join(dir, file));
const dirFiles = await fsReaddir(dir);
ret.push(...dirFiles.map(getFilePath));
}
if (dir !== absoluteRootNodeModules) {
const parent = path.join(dir, '..', '..', nodeModulesPath);
ret.push(...(await find(parent)));
}
return ret;
};
return find(path.join(workspaceRoot, '..', nodeModulesPath));
};
module.exports = {
findParentModules
};