index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict'
  2. // This is adapted from https://github.com/normalize/mz
  3. // Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors
  4. const u = require('universalify').fromCallback
  5. const fs = require('graceful-fs')
  6. const api = [
  7. 'access',
  8. 'appendFile',
  9. 'chmod',
  10. 'chown',
  11. 'close',
  12. 'copyFile',
  13. 'fchmod',
  14. 'fchown',
  15. 'fdatasync',
  16. 'fstat',
  17. 'fsync',
  18. 'ftruncate',
  19. 'futimes',
  20. 'lchmod',
  21. 'lchown',
  22. 'link',
  23. 'lstat',
  24. 'mkdir',
  25. 'mkdtemp',
  26. 'open',
  27. 'opendir',
  28. 'readdir',
  29. 'readFile',
  30. 'readlink',
  31. 'realpath',
  32. 'rename',
  33. 'rm',
  34. 'rmdir',
  35. 'stat',
  36. 'symlink',
  37. 'truncate',
  38. 'unlink',
  39. 'utimes',
  40. 'writeFile'
  41. ].filter(key => {
  42. // Some commands are not available on some systems. Ex:
  43. // fs.opendir was added in Node.js v12.12.0
  44. // fs.rm was added in Node.js v14.14.0
  45. // fs.lchown is not available on at least some Linux
  46. return typeof fs[key] === 'function'
  47. })
  48. // Export all keys:
  49. Object.keys(fs).forEach(key => {
  50. if (key === 'promises') {
  51. // fs.promises is a getter property that triggers ExperimentalWarning
  52. // Don't re-export it here, the getter is defined in "lib/index.js"
  53. return
  54. }
  55. exports[key] = fs[key]
  56. })
  57. // Universalify async methods:
  58. api.forEach(method => {
  59. exports[method] = u(fs[method])
  60. })
  61. // We differ from mz/fs in that we still ship the old, broken, fs.exists()
  62. // since we are a drop-in replacement for the native module
  63. exports.exists = function (filename, callback) {
  64. if (typeof callback === 'function') {
  65. return fs.exists(filename, callback)
  66. }
  67. return new Promise(resolve => {
  68. return fs.exists(filename, resolve)
  69. })
  70. }
  71. // fs.read(), fs.write(), & fs.writev() need special treatment due to multiple callback args
  72. exports.read = function (fd, buffer, offset, length, position, callback) {
  73. if (typeof callback === 'function') {
  74. return fs.read(fd, buffer, offset, length, position, callback)
  75. }
  76. return new Promise((resolve, reject) => {
  77. fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => {
  78. if (err) return reject(err)
  79. resolve({ bytesRead, buffer })
  80. })
  81. })
  82. }
  83. // Function signature can be
  84. // fs.write(fd, buffer[, offset[, length[, position]]], callback)
  85. // OR
  86. // fs.write(fd, string[, position[, encoding]], callback)
  87. // We need to handle both cases, so we use ...args
  88. exports.write = function (fd, buffer, ...args) {
  89. if (typeof args[args.length - 1] === 'function') {
  90. return fs.write(fd, buffer, ...args)
  91. }
  92. return new Promise((resolve, reject) => {
  93. fs.write(fd, buffer, ...args, (err, bytesWritten, buffer) => {
  94. if (err) return reject(err)
  95. resolve({ bytesWritten, buffer })
  96. })
  97. })
  98. }
  99. // fs.writev only available in Node v12.9.0+
  100. if (typeof fs.writev === 'function') {
  101. // Function signature is
  102. // s.writev(fd, buffers[, position], callback)
  103. // We need to handle the optional arg, so we use ...args
  104. exports.writev = function (fd, buffers, ...args) {
  105. if (typeof args[args.length - 1] === 'function') {
  106. return fs.writev(fd, buffers, ...args)
  107. }
  108. return new Promise((resolve, reject) => {
  109. fs.writev(fd, buffers, ...args, (err, bytesWritten, buffers) => {
  110. if (err) return reject(err)
  111. resolve({ bytesWritten, buffers })
  112. })
  113. })
  114. }
  115. }
  116. // fs.realpath.native only available in Node v9.2+
  117. if (typeof fs.realpath.native === 'function') {
  118. exports.realpath.native = u(fs.realpath.native)
  119. }