| 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 |
|
|---|
| 7 | const api = [
|
|---|
| 8 | 'access',
|
|---|
| 9 | 'appendFile',
|
|---|
| 10 | 'chmod',
|
|---|
| 11 | 'chown',
|
|---|
| 12 | 'close',
|
|---|
| 13 | 'copyFile',
|
|---|
| 14 | 'fchmod',
|
|---|
| 15 | 'fchown',
|
|---|
| 16 | 'fdatasync',
|
|---|
| 17 | 'fstat',
|
|---|
| 18 | 'fsync',
|
|---|
| 19 | 'ftruncate',
|
|---|
| 20 | 'futimes',
|
|---|
| 21 | 'lchmod',
|
|---|
| 22 | 'lchown',
|
|---|
| 23 | 'link',
|
|---|
| 24 | 'lstat',
|
|---|
| 25 | 'mkdir',
|
|---|
| 26 | 'mkdtemp',
|
|---|
| 27 | 'open',
|
|---|
| 28 | 'opendir',
|
|---|
| 29 | 'readdir',
|
|---|
| 30 | 'readFile',
|
|---|
| 31 | 'readlink',
|
|---|
| 32 | 'realpath',
|
|---|
| 33 | 'rename',
|
|---|
| 34 | 'rm',
|
|---|
| 35 | 'rmdir',
|
|---|
| 36 | 'stat',
|
|---|
| 37 | 'symlink',
|
|---|
| 38 | 'truncate',
|
|---|
| 39 | 'unlink',
|
|---|
| 40 | 'utimes',
|
|---|
| 41 | 'writeFile'
|
|---|
| 42 | ].filter(key => {
|
|---|
| 43 | // Some commands are not available on some systems. Ex:
|
|---|
| 44 | // fs.opendir was added in Node.js v12.12.0
|
|---|
| 45 | // fs.rm was added in Node.js v14.14.0
|
|---|
| 46 | // fs.lchown is not available on at least some Linux
|
|---|
| 47 | return typeof fs[key] === 'function'
|
|---|
| 48 | })
|
|---|
| 49 |
|
|---|
| 50 | // Export cloned fs:
|
|---|
| 51 | Object.assign(exports, fs)
|
|---|
| 52 |
|
|---|
| 53 | // Universalify async methods:
|
|---|
| 54 | api.forEach(method => {
|
|---|
| 55 | exports[method] = u(fs[method])
|
|---|
| 56 | })
|
|---|
| 57 |
|
|---|
| 58 | // We differ from mz/fs in that we still ship the old, broken, fs.exists()
|
|---|
| 59 | // since we are a drop-in replacement for the native module
|
|---|
| 60 | exports.exists = function (filename, callback) {
|
|---|
| 61 | if (typeof callback === 'function') {
|
|---|
| 62 | return fs.exists(filename, callback)
|
|---|
| 63 | }
|
|---|
| 64 | return new Promise(resolve => {
|
|---|
| 65 | return fs.exists(filename, resolve)
|
|---|
| 66 | })
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | // fs.read(), fs.write(), & fs.writev() need special treatment due to multiple callback args
|
|---|
| 70 |
|
|---|
| 71 | exports.read = function (fd, buffer, offset, length, position, callback) {
|
|---|
| 72 | if (typeof callback === 'function') {
|
|---|
| 73 | return fs.read(fd, buffer, offset, length, position, callback)
|
|---|
| 74 | }
|
|---|
| 75 | return new Promise((resolve, reject) => {
|
|---|
| 76 | fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => {
|
|---|
| 77 | if (err) return reject(err)
|
|---|
| 78 | resolve({ bytesRead, buffer })
|
|---|
| 79 | })
|
|---|
| 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 |
|
|---|
| 93 | return new Promise((resolve, reject) => {
|
|---|
| 94 | fs.write(fd, buffer, ...args, (err, bytesWritten, buffer) => {
|
|---|
| 95 | if (err) return reject(err)
|
|---|
| 96 | resolve({ bytesWritten, buffer })
|
|---|
| 97 | })
|
|---|
| 98 | })
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | // fs.writev only available in Node v12.9.0+
|
|---|
| 102 | if (typeof fs.writev === 'function') {
|
|---|
| 103 | // Function signature is
|
|---|
| 104 | // s.writev(fd, buffers[, position], callback)
|
|---|
| 105 | // We need to handle the optional arg, so we use ...args
|
|---|
| 106 | exports.writev = function (fd, buffers, ...args) {
|
|---|
| 107 | if (typeof args[args.length - 1] === 'function') {
|
|---|
| 108 | return fs.writev(fd, buffers, ...args)
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | return new Promise((resolve, reject) => {
|
|---|
| 112 | fs.writev(fd, buffers, ...args, (err, bytesWritten, buffers) => {
|
|---|
| 113 | if (err) return reject(err)
|
|---|
| 114 | resolve({ bytesWritten, buffers })
|
|---|
| 115 | })
|
|---|
| 116 | })
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | // fs.realpath.native sometimes not available if fs is monkey-patched
|
|---|
| 121 | if (typeof fs.realpath.native === 'function') {
|
|---|
| 122 | exports.realpath.native = u(fs.realpath.native)
|
|---|
| 123 | } else {
|
|---|
| 124 | process.emitWarning(
|
|---|
| 125 | 'fs.realpath.native is not a function. Is fs being monkey-patched?',
|
|---|
| 126 | 'Warning', 'fs-extra-WARN0003'
|
|---|
| 127 | )
|
|---|
| 128 | }
|
|---|