resolveCommand.js 805 B

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