123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- 'use strict'
- // This is adapted from https://github.com/normalize/mz
- // Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors
- const u = require('universalify').fromCallback
- const fs = require('graceful-fs')
- const api = [
- 'access',
- 'appendFile',
- 'chmod',
- 'chown',
- 'close',
- 'copyFile',
- 'fchmod',
- 'fchown',
- 'fdatasync',
- 'fstat',
- 'fsync',
- 'ftruncate',
- 'futimes',
- 'lchmod',
- 'lchown',
- 'link',
- 'lstat',
- 'mkdir',
- 'mkdtemp',
- 'open',
- 'opendir',
- 'readdir',
- 'readFile',
- 'readlink',
- 'realpath',
- 'rename',
- 'rm',
- 'rmdir',
- 'stat',
- 'symlink',
- 'truncate',
- 'unlink',
- 'utimes',
- 'writeFile'
- ].filter(key => {
- // Some commands are not available on some systems. Ex:
- // fs.cp was added in Node.js v16.7.0
- // fs.lchown is not available on at least some Linux
- return typeof fs[key] === 'function'
- })
- // Export cloned fs:
- Object.assign(exports, fs)
- // Universalify async methods:
- api.forEach(method => {
- exports[method] = u(fs[method])
- })
- // We differ from mz/fs in that we still ship the old, broken, fs.exists()
- // since we are a drop-in replacement for the native module
- exports.exists = function (filename, callback) {
- if (typeof callback === 'function') {
- return fs.exists(filename, callback)
- }
- return new Promise(resolve => {
- return fs.exists(filename, resolve)
- })
- }
- // fs.read(), fs.write(), fs.readv(), & fs.writev() need special treatment due to multiple callback args
- exports.read = function (fd, buffer, offset, length, position, callback) {
- if (typeof callback === 'function') {
- return fs.read(fd, buffer, offset, length, position, callback)
- }
- return new Promise((resolve, reject) => {
- fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => {
- if (err) return reject(err)
- resolve({ bytesRead, buffer })
- })
- })
- }
- // Function signature can be
- // fs.write(fd, buffer[, offset[, length[, position]]], callback)
- // OR
- // fs.write(fd, string[, position[, encoding]], callback)
- // We need to handle both cases, so we use ...args
- exports.write = function (fd, buffer, ...args) {
- if (typeof args[args.length - 1] === 'function') {
- return fs.write(fd, buffer, ...args)
- }
- return new Promise((resolve, reject) => {
- fs.write(fd, buffer, ...args, (err, bytesWritten, buffer) => {
- if (err) return reject(err)
- resolve({ bytesWritten, buffer })
- })
- })
- }
- // Function signature is
- // s.readv(fd, buffers[, position], callback)
- // We need to handle the optional arg, so we use ...args
- exports.readv = function (fd, buffers, ...args) {
- if (typeof args[args.length - 1] === 'function') {
- return fs.readv(fd, buffers, ...args)
- }
- return new Promise((resolve, reject) => {
- fs.readv(fd, buffers, ...args, (err, bytesRead, buffers) => {
- if (err) return reject(err)
- resolve({ bytesRead, buffers })
- })
- })
- }
- // Function signature is
- // s.writev(fd, buffers[, position], callback)
- // We need to handle the optional arg, so we use ...args
- exports.writev = function (fd, buffers, ...args) {
- if (typeof args[args.length - 1] === 'function') {
- return fs.writev(fd, buffers, ...args)
- }
- return new Promise((resolve, reject) => {
- fs.writev(fd, buffers, ...args, (err, bytesWritten, buffers) => {
- if (err) return reject(err)
- resolve({ bytesWritten, buffers })
- })
- })
- }
- // fs.realpath.native sometimes not available if fs is monkey-patched
- if (typeof fs.realpath.native === 'function') {
- exports.realpath.native = u(fs.realpath.native)
- } else {
- process.emitWarning(
- 'fs.realpath.native is not a function. Is fs being monkey-patched?',
- 'Warning', 'fs-extra-WARN0003'
- )
- }
|