| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const fs = require('fs'); |
| 3 | const pify = require('pify'); |
| 4 | |
| 5 | const 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 | |
| 18 | module.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 | |
| 26 | module.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 | |
| 36 | module.exports.checkMode = isExe; |