fs.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var Promise = require('any-promise')
  2. var fs
  3. try {
  4. fs = require('graceful-fs')
  5. } catch(err) {
  6. fs = require('fs')
  7. }
  8. var api = [
  9. 'appendFile',
  10. 'chmod',
  11. 'chown',
  12. 'close',
  13. 'fchmod',
  14. 'fchown',
  15. 'fdatasync',
  16. 'fstat',
  17. 'fsync',
  18. 'ftruncate',
  19. 'futimes',
  20. 'lchown',
  21. 'link',
  22. 'lstat',
  23. 'mkdir',
  24. 'open',
  25. 'read',
  26. 'readFile',
  27. 'readdir',
  28. 'readlink',
  29. 'realpath',
  30. 'rename',
  31. 'rmdir',
  32. 'stat',
  33. 'symlink',
  34. 'truncate',
  35. 'unlink',
  36. 'utimes',
  37. 'write',
  38. 'writeFile'
  39. ]
  40. typeof fs.access === 'function' && api.push('access')
  41. typeof fs.copyFile === 'function' && api.push('copyFile')
  42. typeof fs.mkdtemp === 'function' && api.push('mkdtemp')
  43. require('thenify-all').withCallback(fs, exports, api)
  44. exports.exists = function (filename, callback) {
  45. // callback
  46. if (typeof callback === 'function') {
  47. return fs.stat(filename, function (err) {
  48. callback(null, !err);
  49. })
  50. }
  51. // or promise
  52. return new Promise(function (resolve) {
  53. fs.stat(filename, function (err) {
  54. resolve(!err)
  55. })
  56. })
  57. }