win32.js 1.7 KB

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