提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.7 KiB

  1. # isexe
  2. Minimal module to check if a file is executable, and a normal file.
  3. Uses `fs.stat` and tests against the `PATHEXT` environment variable on
  4. Windows.
  5. ## USAGE
  6. ```js
  7. import { isexe, sync } from 'isexe'
  8. // or require() works too
  9. // const { isexe } = require('isexe')
  10. isexe('some-file-name').then(isExe => {
  11. if (isExe) {
  12. console.error('this thing can be run')
  13. } else {
  14. console.error('cannot be run')
  15. }
  16. }, (err) => {
  17. console.error('probably file doesnt exist or something')
  18. })
  19. // same thing but synchronous, throws errors
  20. isExe = sync('some-file-name')
  21. // treat errors as just "not executable"
  22. const isExe = await isexe('maybe-missing-file', { ignoreErrors: true })
  23. const isExe = sync('maybe-missing-file', { ignoreErrors: true })
  24. ```
  25. ## API
  26. ### `isexe(path, [options]) => Promise<boolean>`
  27. Check if the path is executable.
  28. Will raise whatever errors may be raised by `fs.stat`, unless
  29. `options.ignoreErrors` is set to true.
  30. ### `sync(path, [options]) => boolean`
  31. Same as `isexe` but returns the value and throws any errors raised.
  32. ## Platform Specific Implementations
  33. If for some reason you want to use the implementation for a
  34. specific platform, you can do that.
  35. ```js
  36. import { win32, posix } from 'isexe'
  37. win32.isexe(...)
  38. win32.sync(...)
  39. // etc
  40. // or:
  41. import { isexe, sync } from 'isexe/posix'
  42. ```
  43. The default exported implementation will be chosen based on
  44. `process.platform`.
  45. ### Options
  46. ```ts
  47. import type IsexeOptions from 'isexe'
  48. ```
  49. * `ignoreErrors` Treat all errors as "no, this is not
  50. executable", but don't raise them.
  51. * `uid` Number to use as the user id on posix
  52. * `gid` Number to use as the group id on posix
  53. * `pathExt` List of path extensions to use instead of `PATHEXT`
  54. environment variable on Windows.