1 | const path = require("path");
|
2 | const fs = require("fs");
|
3 | const _ = require("lodash");
|
4 |
|
5 |
|
6 | function findCommandPath(com, pathList) {
|
7 | let execPath = "";
|
8 | const possibleExtension = ["", ".exe", ".cmd"];
|
9 | _.each(pathList, executablePath => {
|
10 | if (execPath.length) return;
|
11 |
|
12 | _.each(possibleExtension, ext => {
|
13 | if (execPath.length) return;
|
14 |
|
15 | const extendedPath = path.join(executablePath, `${com}${ext}`);
|
16 | if (fs.existsSync(extendedPath)) {
|
17 | execPath = extendedPath;
|
18 | }
|
19 | });
|
20 | });
|
21 | if (!execPath.length) throw new Error(`Cannot find command ${com}.`);
|
22 | return `${execPath}`;
|
23 | }
|
24 |
|
25 | module.exports = findCommandPath;
|
26 | module.exports.factory = function(pathList) {
|
27 | return cmd => {
|
28 | return findCommandPath(cmd, pathList);
|
29 | };
|
30 | }; |
\ | No newline at end of file |