exists.js 775 B

123456789101112131415161718192021222324252627
  1. 'use strict'
  2. var fs = require('fs')
  3. var inflight = require('inflight')
  4. var accessError = require('./access-error.js')
  5. var isFsAccessAvailable = require('./is-fs-access-available.js')
  6. if (isFsAccessAvailable) {
  7. module.exports = fsAccessImplementation
  8. } else {
  9. module.exports = fsStatImplementation
  10. }
  11. // exposed only for testing purposes
  12. module.exports.fsAccessImplementation = fsAccessImplementation
  13. module.exports.fsStatImplementation = fsStatImplementation
  14. function fsAccessImplementation (dir, done) {
  15. done = inflight('exists:' + dir, done)
  16. if (!done) return
  17. fs.access(dir, fs.F_OK, done)
  18. }
  19. function fsStatImplementation (dir, done) {
  20. done = inflight('exists:' + dir, done)
  21. if (!done) return
  22. fs.stat(dir, function (er) { done(accessError(dir, er)) })
  23. }