Git Product home page Git Product logo

isexe's Introduction

isexe

Minimal module to check if a file is executable, and a normal file.

Uses fs.stat and tests against the PATHEXT environment variable on Windows.

USAGE

import { isexe, sync } from 'isexe'
// or require() works too
// const { isexe } = require('isexe')
isexe('some-file-name').then(isExe => {
  if (isExe) {
    console.error('this thing can be run')
  } else {
    console.error('cannot be run')
  }
}, (err) => {
  console.error('probably file doesnt exist or something')
})

// same thing but synchronous, throws errors
isExe = sync('some-file-name')

// treat errors as just "not executable"
const isExe = await isexe('maybe-missing-file', { ignoreErrors: true })
const isExe = sync('maybe-missing-file', { ignoreErrors: true })

API

isexe(path, [options]) => Promise<boolean>

Check if the path is executable.

Will raise whatever errors may be raised by fs.stat, unless options.ignoreErrors is set to true.

sync(path, [options]) => boolean

Same as isexe but returns the value and throws any errors raised.

Platform Specific Implementations

If for some reason you want to use the implementation for a specific platform, you can do that.

import { win32, posix } from 'isexe'
win32.isexe(...)
win32.sync(...)
// etc

// or:
import { isexe, sync } from 'isexe/posix'

The default exported implementation will be chosen based on process.platform.

Options

import type IsexeOptions from 'isexe'
  • ignoreErrors Treat all errors as "no, this is not executable", but don't raise them.
  • uid Number to use as the user id on posix
  • gid Number to use as the group id on posix
  • pathExt List of path extensions to use instead of PATHEXT environment variable on Windows.

isexe's People

Contributors

isaacs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

isexe's Issues

Error: Cannot find module, Need Help !

node:internal/modules/cjs/loader:408
throw err;
^

Error: Cannot find module 'C:\Users\Administrator\Downloads\react-vscode-portfolio-main\node_modules\isexe\index.js'. Please verify that the package.json has a valid "main" entry
at tryPackage (node:internal/modules/cjs/loader:400:19)
at Module._findPath (node:internal/modules/cjs/loader:613:18)
at Module._resolveFilename (node:internal/modules/cjs/loader:981:27)
at Module._load (node:internal/modules/cjs/loader:841:27)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object. (C:\Users\Administrator\Downloads\react-vscode-portfolio-main\node_modules\which\which.js:7:15)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Module.load (node:internal/modules/cjs/loader:1037:32) {
code: 'MODULE_NOT_FOUND',
path: 'C:\Users\Administrator\Downloads\react-vscode-portfolio-main\node_modules\isexe\package.json',
requestPath: 'isexe'
}

test fails on windows

Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\Users\Burt_>cd \code\isexe

C:\code\isexe>npm i
up to date in 1.345s

C:\code\isexe>npm test

> [email protected] test C:\code\isexe
> tap test/*.js --100

module.js:487
    throw err;
    ^

Error: Cannot find module 'C:\Users\Burt_\.node-spawn-wrap-2296-ee88a1276edb\node.EXE'
    at Function.Module._resolveFilename (module.js:485:15)
    at Function.Module._load (module.js:437:25)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3
test/basic.js ......................................... 0/1
  not ok test/basic.js
    timeout: 240000
    file: test/basic.js
    command: 'C:\Program Files\nodejs\node.EXE'
    args:
      - test/basic.js
    cwd: 'C:\code\isexe'
    exitCode: 1

total ................................................. 0/1


  0 passing (131.621ms)
  1 failing

----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
All files |  Unknown |  Unknown |  Unknown |  Unknown |                |
----------|----------|----------|----------|----------|----------------|

Returns false when used in case-sensitive directory on Windows

Windows now features per-directory case-sensitivity. This is explained here: https://blogs.msdn.microsoft.com/commandline/2018/02/28/per-directory-case-sensitivity-and-wsl/
A directory created in the Windows Subsystem for Linux will be case-sensitive by default.

As a result, the isexe check will always return false unless the file extension is in all-caps. This is because the file extensions obtained from the PathExt environment variable are in all-caps. This is not a great default behavior, because most file extensions are lower-case.

Edit: Nevermind; this library works fine. It converts both the path and the extension to lower-case. I was thinking of the step performed in the which library, which tries finding an executable by appending each extension found in PathExt to the command name until it finds one.

Test 'promise > meow async' fails on windows.

The test promise > meow async fails on Windows.

$ tap test/*.js --100
test/basic.js ....................................... 58/61
  promise > meow async
  not ok expect truthy value
    at:
      line: 43
      column: 9
      file: test/basic.js
      type: global
    stack: |
      test/basic.js:43:9
      <anonymous>
    source: |
      t.ok(is)

  Skipped: 2
    access windows
    mode windows

total ............................................... 58/61

It looks like this fails because meow.cat has the file extension cat, which is not in my PATHEXT.

error

error is comin.please help
code: 'MODULE_NOT_FOUND',
path: 'C:\Users\Joby\Desktop\Blog-Website\client\node_modules\isexe\package.json',
requestPath: 'isexe'

Drop tests from npm package?

Hi,

I saw that test file were part of the npm pckage
Current .npmignore is:

.nyc_output/
coverage/

maybe we could add test/ ?

Thank you

Reduce the size of the npm package by limiting the included files

Looks like the files property (https://docs.npmjs.com/files/package.json#files) is not used in package.json to specify the included files, nor is the .npmignore file (https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package) is being used for blacklisting unwanted files, for the package published to npm.

Would you consider adding either the files property or the .npmignore file, so that the resulting package file would have smaller size?

The current size can be seen when executing the command npm pack (https://docs.npmjs.com/cli/pack).

This issue was create via tawata

Create LICENSE file

Will you please create a LICENSE file with the copyright information and the text of the ISC license? The ISC license specifically states that the license text much accompany the source code.

isexe fails to recognize an executable that is not owned by current user

On Linux, the isexe module does not recognize an executable file as being executable when

  1. the file's owner is different from the current user, and
  2. the file's group is different from the current user's main group, and
  3. the file is not readable by "others".

So a file with the permissions rwxrwx--- with user=nobody and group=adm is not recognized as executable, even though my user is a member of the group "adm" and the group "adm" does have the "x" permission on that particular file. On the shell however, the file executes just fine!

In mode.js, process.getgid() is used the get the current main groups. However, all secondary groups of the current user are not checked. I think mode.js should use process.getgroups() instead and check every group. (Or maybe just use process.access() like https://www.npmjs.com/package/is-executable does?)

Demonstration:

Set up a test project and install "isexe" module

/tmp$ mkdir isexetest
/tmp$ cd isexetest/
/tmp/isexetest$ npm init -y
(...)
/tmp/isexetest$ npm install isexe
(...)
+ [email protected]
added 1 package from 1 contributor in 1.661s

Create a simple executable file and test it

/tmp/isexetest$ cat << EOF > hello.sh
> #!/bin/bash
> echo hello
> EOF
/tmp/isexetest$ chmod +x hello.sh 
/tmp/isexetest$ ./hello.sh 
hello
/tmp/isexetest$ ls -la hello.sh 
-rwxrwxr-x 1 nico nico 23 Mai  6 14:24 hello.sh
/tmp/isexetest$ node -e "require('isexe')('hello.sh', console.log)"
null 1

So I can run this program on the terminal and "isexe" returns 1 which is truthy, so everything seems to be in order correct.

Change the file ownership and test it again

First, I restrict the file permissions to 770, so only the owner and members of the group can access the file. Then I set the owner to "nobody" and the group to "adm". Note that those values are rather arbitrary. I chose them because they are present on most systems and my user is already a member of the "adm" group.

/tmp/isexetest$ id
uid=1000(nico) gid=1000(nico) groups=1000(nico),4(adm),20(dialout),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare),999(vboxsf)
/tmp/isexetest$ chmod 770 hello.sh 
/tmp/isexetest$ sudo chown nobody:adm hello.sh 
/tmp/isexetest$ ls -la hello.sh 
-rwxrwx--- 1 nobody adm 23 Mai  6 14:24 hello.sh
/tmp/isexetest$ ./hello.sh 
hello
/tmp/isexetest$ node -e "require('isexe')('./hello.sh', console.log)"
null false

As you can see I can still execute the file (as expected), but "isexe" now returns false, which is incorrect.

Some version information

/tmp/isexetest$ cat /etc/issue
Ubuntu 16.04.4 LTS \n \l

/tmp/isexetest$ node -v
v8.11.1
/tmp/isexetest$ npm list
[email protected] /tmp/isexetest
+-- [email protected]

Throwing too many exceptions on windows

Using fs.stat or fs.statSync on windows ends up throwing lots of exceptions.

For example (one of hundreds generated by running a gulp command.

Exception has occurred: Error
Error: ENOENT: no such file or directory, stat 'c:\code\antlr4ts\npm.COM'
at Object.fs.statSync (fs.js:940:11)
at Function.sync (c:\code\antlr4ts\node_modules\isexe\windows.js:41:23)
at Function.sync (c:\code\antlr4ts\node_modules\isexe\index.js:49:17)
at Function.whichSync [as sync] (c:\code\antlr4ts\node_modules\which\which.js:117:20)
at npmPath (c:\code\antlr4ts\node_modules\global-prefix\index.js:67:45)
at Object.<anonymous> (c:\code\antlr4ts\node_modules\global-prefix\index.js:30:15)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.