blob: fd4169973d795829fe6b536d4cac959825970431 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const fs = require('fs');
3const pify = require('pify');
4
5const isExe = (mode, gid, uid) => {
6 if (process.platform === 'win32') {
7 return true;
8 }
9
10 const isGroup = gid ? process.getgid && gid === process.getgid() : true;
11 const isUser = uid ? process.getuid && uid === process.getuid() : true;
12
13 return Boolean((mode & 0o0001) ||
14 ((mode & 0o0010) && isGroup) ||
15 ((mode & 0o0100) && isUser));
16};
17
18module.exports = name => {
19 if (typeof name !== 'string') {
20 return Promise.reject(new TypeError('Expected a string'));
21 }
22
23 return pify(fs.stat)(name).then(stats => stats && stats.isFile() && isExe(stats.mode, stats.gid, stats.uid));
24};
25
26module.exports.sync = name => {
27 if (typeof name !== 'string') {
28 throw new TypeError('Expected a string');
29 }
30
31 const stats = fs.statSync(name);
32
33 return stats && stats.isFile() && isExe(stats.mode, stats.gid, stats.uid);
34};
35
36module.exports.checkMode = isExe;