UNPKG

804 BJavaScriptView Raw
1const path = require("path");
2const fs = require("fs");
3const _ = require("lodash");
4
5// Find command from all the paths possible
6function 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
25module.exports = findCommandPath;
26module.exports.factory = function(pathList) {
27 return cmd => {
28 return findCommandPath(cmd, pathList);
29 };
30};
\No newline at end of file