Skip to content

Commit 038aa91

Browse files
committed
Move CLI logic to bin.js
1 parent 837fc66 commit 038aa91

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

bin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env node
2+
3+
const getPath = require("./index.js");
4+
const result = getPath(process.argv[2]);
5+
6+
// set an env var - avail to the current process, not in the global shell process
7+
process.env.NODE_MODULES = result;
8+
9+
// return the node_module path
10+
console.log(result);

index.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
1-
#!/usr/bin/env node
2-
31
// 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");
64

75
const pwd = __dirname.split(path.sep);
86

9-
function getPath(moduleName, folder = pwd) {
10-
if(folder.length < 1) {
7+
module.exports = function getPath(moduleName, folder = pwd) {
8+
if (folder.length < 1) {
119
logError(moduleName, folder);
1210
return null;
1311
}
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)) {
1717
return nodeModulesPath;
1818
}
1919
res = getPath(moduleName, folder.slice(0, -1));
20-
if(!res) logError(moduleName, folder);
20+
if (!res) logError(moduleName, folder);
2121
return res;
2222
};
2323

2424
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+
);
2630
}
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-

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"main": "index.js",
1313
"bin": {
14-
"node_modules": "index.js"
14+
"node_modules": "bin.js"
1515
},
1616
"author": "lexoyo",
1717
"license": "GPL",

0 commit comments

Comments
 (0)