index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // This is adapted from https://github.com/normalize/mz
  2. // Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors
  3. const u = require('universalify').fromCallback
  4. const fs = require('graceful-fs')
  5. const api = [
  6. 'access',
  7. 'appendFile',
  8. 'chmod',
  9. 'chown',
  10. 'close',
  11. 'fchmod',
  12. 'fchown',
  13. 'fdatasync',
  14. 'fstat',
  15. 'fsync',
  16. 'ftruncate',
  17. 'futimes',
  18. 'lchown',
  19. 'link',
  20. 'lstat',
  21. 'mkdir',
  22. 'open',
  23. 'read',
  24. 'readFile',
  25. 'readdir',
  26. 'readlink',
  27. 'realpath',
  28. 'rename',
  29. 'rmdir',
  30. 'stat',
  31. 'symlink',
  32. 'truncate',
  33. 'unlink',
  34. 'utimes',
  35. 'write',
  36. 'writeFile'
  37. ]
  38. // fs.mkdtemp() was added in Node.js v5.10.0, so check if it exists
  39. typeof fs.mkdtemp === 'function' && api.push('mkdtemp')
  40. // Export all keys:
  41. Object.keys(fs).forEach(key => {
  42. exports[key] = fs[key]
  43. })
  44. // Universalify async methods:
  45. api.forEach(method => {
  46. exports[method] = u(fs[method])
  47. })
  48. // We differ from mz/fs in that we still ship the old, broken, fs.exists()
  49. // since we are a drop-in replacement for the native module
  50. exports.exists = function (filename, callback) {
  51. if (typeof callback === 'function') {
  52. return fs.exists(filename, callback)
  53. }
  54. return new Promise(resolve => {
  55. return fs.exists(filename, resolve)
  56. })
  57. }