1 | const path = require("path");
|
2 | const fs = require("fs");
|
3 | const _uniq = require("lodash/uniq");
|
4 |
|
5 | const pRoot = (process.env.PROJECT_ROOT) || (process.env.PROJECT_ROOT) || (process.cwd() + path.sep);
|
6 | const lRoot = process.env.LIB_ROOT || path.resolve(__dirname, "../");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | module.exports = (projectRoot = pRoot,libRoot = lRoot) => {
|
15 | let executablePaths = process.env.PATH.split(path.delimiter);
|
16 |
|
17 |
|
18 | executablePaths.unshift(libRoot);
|
19 |
|
20 |
|
21 | fs.existsSync(path.join(libRoot, ".bin")) &&
|
22 | executablePaths.unshift(path.join(libRoot, ".bin"));
|
23 |
|
24 | fs.existsSync(path.join(libRoot, "node_modules")) &&
|
25 | executablePaths.unshift(path.join(libRoot, "node_modules"));
|
26 |
|
27 | fs.existsSync(path.join(libRoot, "node_modules", ".bin")) &&
|
28 | executablePaths.unshift(path.join(libRoot, "node_modules", ".bin"));
|
29 |
|
30 |
|
31 | fs.existsSync(path.join(libRoot, "..", "node_modules")) &&
|
32 | executablePaths.unshift(path.join(libRoot, "..", "node_modules"));
|
33 |
|
34 |
|
35 | fs.existsSync(path.join(libRoot, "..", "..", "node_modules")) &&
|
36 | executablePaths.unshift(path.join(libRoot, "..", "..", "node_modules"));
|
37 |
|
38 |
|
39 |
|
40 | fs.existsSync(path.join(libRoot, "..", "..", "..", "node_modules")) &&
|
41 | executablePaths.unshift(path.join(libRoot, "..", "..", "..", "node_modules"));
|
42 |
|
43 | fs.existsSync(path.join(libRoot, "..", "node_modules", ".bin")) &&
|
44 | executablePaths.unshift(path.join(libRoot, "..", "node_modules", ".bin"));
|
45 |
|
46 |
|
47 | executablePaths.unshift(projectRoot);
|
48 |
|
49 |
|
50 | fs.existsSync(path.join(projectRoot, ".bin")) &&
|
51 | executablePaths.unshift(path.join(projectRoot, ".bin"));
|
52 |
|
53 | fs.existsSync(path.join(projectRoot, "node_modules")) &&
|
54 | executablePaths.unshift(path.join(projectRoot, "node_modules"));
|
55 |
|
56 | fs.existsSync(path.join(projectRoot, "node_modules", ".bin")) &&
|
57 | executablePaths.unshift(path.join(projectRoot, "node_modules", ".bin"));
|
58 |
|
59 |
|
60 |
|
61 | if(lRoot === process.cwd()) {
|
62 | const packages = fs.readdirSync(path.join(libRoot, "packages"));
|
63 | packages.forEach(p => {
|
64 | const packagePath = path.join(libRoot, "packages", p);
|
65 |
|
66 |
|
67 | fs.existsSync(path.join(packagePath, ".bin")) &&
|
68 | executablePaths.unshift(path.join(packagePath, ".bin"));
|
69 |
|
70 | fs.existsSync(path.join(projectRoot, "node_modules")) &&
|
71 | executablePaths.unshift(path.join(packagePath, "node_modules"));
|
72 |
|
73 | fs.existsSync(path.join(packagePath, "node_modules", ".bin")) &&
|
74 | executablePaths.unshift(path.join(packagePath, "node_modules", ".bin"));
|
75 | });
|
76 | }
|
77 |
|
78 |
|
79 | executablePaths = _uniq(executablePaths);
|
80 |
|
81 | return executablePaths;
|
82 | };
|