├── .gitignore ├── .gitattributes ├── fixtures └── optipng ├── .travis.yml ├── .editorconfig ├── test.js ├── package.json ├── index.js ├── license └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /fixtures/optipng: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevva/executable/HEAD/fixtures/optipng -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '6' 4 | - '4' 5 | before_script: 6 | - chmod +x fixtures/optipng 7 | - chmod 755 fixtures/optipng 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import path from 'path'; 2 | import test from 'ava'; 3 | import m from './'; 4 | 5 | test('test executable and return true', async t => { 6 | t.true(await m(path.join(__dirname, 'fixtures/optipng'))); 7 | }); 8 | 9 | test('test executable synchronously and return true', t => { 10 | t.true(m.sync(path.join(__dirname, 'fixtures/optipng'))); 11 | }); 12 | 13 | test('test non-executable', async t => { 14 | t.false(await m(path.join(__dirname, 'readme.md'))); 15 | }); 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "executable", 3 | "version": "4.1.1", 4 | "description": "Check if a file is executable", 5 | "license": "MIT", 6 | "repository": "kevva/executable", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "https://github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "exec", 23 | "executable", 24 | "permission" 25 | ], 26 | "dependencies": { 27 | "pify": "^2.2.0" 28 | }, 29 | "devDependencies": { 30 | "ava": "*", 31 | "xo": "*" 32 | }, 33 | "xo": { 34 | "esnext": true 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 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; 37 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Kevin Mårtensson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # executable [![Build Status](https://travis-ci.org/kevva/executable.svg?branch=master)](https://travis-ci.org/kevva/executable) 2 | 3 | > Check if a file is executable 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save executable 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const executable = require('executable'); 17 | 18 | executable('bash').then(exec => { 19 | console.log(exec); 20 | //=> true 21 | }); 22 | ``` 23 | 24 | 25 | ## API 26 | 27 | ### executable(file) 28 | 29 | Returns a Promise for a boolean. 30 | 31 | ### executable.sync(file) 32 | 33 | Returns a boolean of whether the file is executable. 34 | 35 | #### file 36 | 37 | Type: `string` 38 | 39 | Path of the file. 40 | 41 | ### executable.checkMode(mode, [gid], [uid]) 42 | 43 | Returns a boolean of whether the mode passed as first argument means that the file is executable. 44 | 45 | #### mode 46 | 47 | Type: `number` 48 | 49 | Property `mode` of `fs.Stats` instance returned by `fs.stat()` (or `fs.statSync()`) function. 50 | 51 | #### gid, uid 52 | 53 | Type: `number` 54 | 55 | Respectively the group identity and user identity of the file. If not set, permissions will be evaluated without considering owner or group of the file. 56 | 57 | ## Related 58 | 59 | * [executable-cli](https://github.com/kevva/executable-cli) - CLI for this module 60 | 61 | 62 | ## License 63 | 64 | MIT © [Kevin Mårtensson](https://github.com/kevva) 65 | --------------------------------------------------------------------------------