|
1 | | -#!/usr/bin/env node |
2 | | - |
3 | 1 | // get our parent folder path |
4 | | -const path = require('path'); |
5 | | -const fs = require('fs'); |
| 2 | +const path = require("path"); |
| 3 | +const fs = require("fs"); |
6 | 4 |
|
7 | 5 | const pwd = __dirname.split(path.sep); |
8 | 6 |
|
9 | | -function getPath(moduleName, folder = pwd) { |
10 | | - if(folder.length < 1) { |
| 7 | +module.exports = function getPath(moduleName, folder = pwd) { |
| 8 | + if (folder.length < 1) { |
11 | 9 | logError(moduleName, folder); |
12 | 10 | return null; |
13 | 11 | } |
14 | | - const nodeModulesPath = folder.concat(['node_modules']).join(path.sep); |
15 | | - const p = moduleName ? path.join(nodeModulesPath, moduleName) : nodeModulesPath; |
16 | | - if(fs.existsSync(p)) { |
| 12 | + const nodeModulesPath = folder.concat(["node_modules"]).join(path.sep); |
| 13 | + const p = moduleName |
| 14 | + ? path.join(nodeModulesPath, moduleName) |
| 15 | + : nodeModulesPath; |
| 16 | + if (fs.existsSync(p)) { |
17 | 17 | return nodeModulesPath; |
18 | 18 | } |
19 | 19 | res = getPath(moduleName, folder.slice(0, -1)); |
20 | | - if(!res) logError(moduleName, folder); |
| 20 | + if (!res) logError(moduleName, folder); |
21 | 21 | return res; |
22 | 22 | }; |
23 | 23 |
|
24 | 24 | function logError(moduleName, folder) { |
25 | | - console.error(`Could not find the node_modules folder ${ moduleName ? "which contains " + moduleName : "" } in ${ folder.join(path.sep) }`); |
| 25 | + console.error( |
| 26 | + `Could not find the node_modules folder ${ |
| 27 | + moduleName ? "which contains " + moduleName : "" |
| 28 | + } in ${folder.join(path.sep)}` |
| 29 | + ); |
26 | 30 | } |
27 | | -const result = getPath(process.argv[2]); |
28 | | - |
29 | | -// set an env var - avail to the current process, not in the global shell process |
30 | | -process.env.NODE_MODULES = result; |
31 | | - |
32 | | -// return the node_module path |
33 | | -console.log(result); |
34 | | - |
35 | | -// for use in a nodejs script |
36 | | -module.exports = getPath; |
37 | | - |
0 commit comments