UNPKG

805 BJavaScriptView Raw
1'use strict';
2
3var path = require('path');
4var which = require('which');
5var LRU = require('lru-cache');
6
7var commandCache = new LRU({ max: 50, maxAge: 30 * 1000 }); // Cache just for 30sec
8
9function resolveCommand(command, noExtension) {
10 var resolved;
11
12 noExtension = !!noExtension;
13 resolved = commandCache.get(command + '!' + noExtension);
14
15 // Check if its resolved in the cache
16 if (commandCache.has(command)) {
17 return commandCache.get(command);
18 }
19
20 try {
21 resolved = !noExtension ?
22 which.sync(command) :
23 which.sync(command, { pathExt: path.delimiter + (process.env.PATHEXT || '') });
24 } catch (e) { /* empty */ }
25
26 commandCache.set(command + '!' + noExtension, resolved);
27
28 return resolved;
29}
30
31module.exports = resolveCommand;