index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.cp was added in Node.js v16.7.0
  44. // fs.lchown is not available on at least some Linux
  45. return typeof fs[key] === 'function'
  46. })
  47. // Export cloned fs:
  48. Object.assign(exports, fs)
  49. // Universalify async methods:
  50. api.forEach(method => {
  51. exports[method] = u(fs[method])
  52. })
  53. // We differ from mz/fs in that we still ship the old, broken, fs.exists()
  54. // since we are a drop-in replacement for the native module
  55. exports.exists = function (filename, callback) {
  56. if (typeof callback === 'function') {
  57. return fs.exists(filename, callback)
  58. }
  59. return new Promise(resolve => {
  60. return fs.exists(filename, resolve)
  61. })
  62. }
  63. // fs.read(), fs.write(), fs.readv(), & fs.writev() need special treatment due to multiple callback args
  64. exports.read = function (fd, buffer, offset, length, position, callback) {
  65. if (typeof callback === 'function') {
  66. return fs.read(fd, buffer, offset, length, position, callback)
  67. }
  68. return new Promise((resolve, reject) => {
  69. fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => {
  70. if (err) return reject(err)
  71. resolve({ bytesRead, buffer })
  72. })
  73. })
  74. }
  75. // Function signature can be
  76. // fs.write(fd, buffer[, offset[, length[, position]]], callback)
  77. // OR
  78. // fs.write(fd, string[, position[, encoding]], callback)
  79. // We need to handle both cases, so we use ...args
  80. exports.write = function (fd, buffer, ...args) {
  81. if (typeof args[args.length - 1] === 'function') {
  82. return fs.write(fd, buffer, ...args)
  83. }
  84. return new Promise((resolve, reject) => {
  85. fs.write(fd, buffer, ...args, (err, bytesWritten, buffer) => {
  86. if (err) return reject(err)
  87. resolve({ bytesWritten, buffer })
  88. })
  89. })
  90. }
  91. // Function signature is
  92. // s.readv(fd, buffers[, position], callback)
  93. // We need to handle the optional arg, so we use ...args
  94. exports.readv = function (fd, buffers, ...args) {
  95. if (typeof args[args.length - 1] === 'function') {
  96. return fs.readv(fd, buffers, ...args)
  97. }
  98. return new Promise((resolve, reject) => {
  99. fs.readv(fd, buffers, ...args, (err, bytesRead, buffers) => {
  100. if (err) return reject(err)
  101. resolve({ bytesRead, buffers })
  102. })
  103. })
  104. }
  105. // Function signature is
  106. // s.writev(fd, buffers[, position], callback)
  107. // We need to handle the optional arg, so we use ...args
  108. exports.writev = function (fd, buffers, ...args) {
  109. if (typeof args[args.length - 1] === 'function') {
  110. return fs.writev(fd, buffers, ...args)
  111. }
  112. return new Promise((resolve, reject) => {
  113. fs.writev(fd, buffers, ...args, (err, bytesWritten, buffers) => {
  114. if (err) return reject(err)
  115. resolve({ bytesWritten, buffers })
  116. })
  117. })
  118. }
  119. // fs.realpath.native sometimes not available if fs is monkey-patched
  120. if (typeof fs.realpath.native === 'function') {
  121. exports.realpath.native = u(fs.realpath.native)
  122. } else {
  123. process.emitWarning(
  124. 'fs.realpath.native is not a function. Is fs being monkey-patched?',
  125. 'Warning', 'fs-extra-WARN0003'
  126. )
  127. }