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