win32.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. /**
  3. * This is the Windows implementation of isexe, which uses the file
  4. * extension and PATHEXT setting.
  5. *
  6. * @module
  7. */
  8. Object.defineProperty(exports, "__esModule", { value: true });
  9. exports.sync = exports.isexe = void 0;
  10. const fs_1 = require("fs");
  11. const promises_1 = require("fs/promises");
  12. /**
  13. * Determine whether a path is executable based on the file extension
  14. * and PATHEXT environment variable (or specified pathExt option)
  15. */
  16. const isexe = async (path, options = {}) => {
  17. const { ignoreErrors = false } = options;
  18. try {
  19. return checkStat(await (0, promises_1.stat)(path), path, options);
  20. }
  21. catch (e) {
  22. const er = e;
  23. if (ignoreErrors || er.code === 'EACCES')
  24. return false;
  25. throw er;
  26. }
  27. };
  28. exports.isexe = isexe;
  29. /**
  30. * Synchronously determine whether a path is executable based on the file
  31. * extension and PATHEXT environment variable (or specified pathExt option)
  32. */
  33. const sync = (path, options = {}) => {
  34. const { ignoreErrors = false } = options;
  35. try {
  36. return checkStat((0, fs_1.statSync)(path), path, options);
  37. }
  38. catch (e) {
  39. const er = e;
  40. if (ignoreErrors || er.code === 'EACCES')
  41. return false;
  42. throw er;
  43. }
  44. };
  45. exports.sync = sync;
  46. const checkPathExt = (path, options) => {
  47. const { pathExt = process.env.PATHEXT || '' } = options;
  48. const peSplit = pathExt.split(';');
  49. if (peSplit.indexOf('') !== -1) {
  50. return true;
  51. }
  52. for (let i = 0; i < peSplit.length; i++) {
  53. const p = peSplit[i].toLowerCase();
  54. const ext = path.substring(path.length - p.length).toLowerCase();
  55. if (p && ext === p) {
  56. return true;
  57. }
  58. }
  59. return false;
  60. };
  61. const checkStat = (stat, path, options) => stat.isFile() && checkPathExt(path, options);
  62. //# sourceMappingURL=win32.js.map