UNPKG

3.24 kBJavaScriptView Raw
1const path = require("path");
2const fs = require("fs");
3const _uniq = require("lodash/uniq");
4
5const pRoot = (process.env.PROJECT_ROOT) || (process.env.PROJECT_ROOT) || (process.cwd() + path.sep);
6const lRoot = process.env.LIB_ROOT || path.resolve(__dirname, "../");
7
8/**
9 *
10 * @param projectRoot
11 * @param libRoot
12 * @returns {Array}
13 */
14module.exports = (projectRoot = pRoot,libRoot = lRoot) => {
15 let executablePaths = process.env.PATH.split(path.delimiter);
16
17 // Add library root to executable path
18 executablePaths.unshift(libRoot);
19
20 // Include library's bin and it's node_modules's bin
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 // Add parent directory node_modules of pawjs to the list
31 fs.existsSync(path.join(libRoot, "..", "node_modules")) &&
32 executablePaths.unshift(path.join(libRoot, "..", "node_modules"));
33
34 // Add parent directory node_modules of pawjs to the list
35 fs.existsSync(path.join(libRoot, "..", "..", "node_modules")) &&
36 executablePaths.unshift(path.join(libRoot, "..", "..", "node_modules"));
37
38 // Add parent to parent directory
39 // thus trying to add directory storing @pawjs/pawjs for resolution
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 // Add project root to executable path
47 executablePaths.unshift(projectRoot);
48
49 // Include current folder bin and node_modules's bin
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 // If library root is current directory, i.e. we are developing pawjs,
60 // then include the node_modules from packages as well.
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 // Include package folder bin and node_modules's bin
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 // If there are duplicate entries clear them up
79 executablePaths = _uniq(executablePaths);
80
81 return executablePaths;
82};