index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var fs = require('fs')
  2. var core
  3. if (process.platform === 'win32' || global.TESTING_WINDOWS) {
  4. core = require('./windows.js')
  5. } else if (typeof fs.access === 'function') {
  6. core = require('./access.js')
  7. } else {
  8. core = require('./mode.js')
  9. }
  10. module.exports = isexe
  11. isexe.sync = sync
  12. function isexe (path, options, cb) {
  13. if (typeof options === 'function') {
  14. cb = options
  15. options = {}
  16. }
  17. if (!cb) {
  18. if (typeof Promise !== 'function') {
  19. throw new TypeError('callback not provided')
  20. }
  21. return new Promise(function (resolve, reject) {
  22. isexe(path, options || {}, function (er, is) {
  23. if (er) {
  24. reject(er)
  25. } else {
  26. resolve(is)
  27. }
  28. })
  29. })
  30. }
  31. core(path, options || {}, function (er, is) {
  32. // ignore EACCES because that just means we aren't allowed to run it
  33. if (er) {
  34. if (er.code === 'EACCES' || options && options.ignoreErrors) {
  35. er = null
  36. is = false
  37. }
  38. }
  39. cb(er, is)
  40. })
  41. }
  42. function sync (path, options) {
  43. // my kingdom for a filtered catch
  44. try {
  45. return core.sync(path, options || {})
  46. } catch (er) {
  47. if (options && options.ignoreErrors || er.code === 'EACCES') {
  48. return false
  49. } else {
  50. throw er
  51. }
  52. }
  53. }