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 | 'lchown',
|
---|
22 | 'lchmod',
|
---|
23 | 'link',
|
---|
24 | 'lstat',
|
---|
25 | 'mkdir',
|
---|
26 | 'mkdtemp',
|
---|
27 | 'open',
|
---|
28 | 'readFile',
|
---|
29 | 'readdir',
|
---|
30 | 'readlink',
|
---|
31 | 'realpath',
|
---|
32 | 'rename',
|
---|
33 | 'rmdir',
|
---|
34 | 'stat',
|
---|
35 | 'symlink',
|
---|
36 | 'truncate',
|
---|
37 | 'unlink',
|
---|
38 | 'utimes',
|
---|
39 | 'writeFile'
|
---|
40 | ].filter(key => {
|
---|
41 | // Some commands are not available on some systems. Ex:
|
---|
42 | // fs.copyFile was added in Node.js v8.5.0
|
---|
43 | // fs.mkdtemp was added in Node.js v5.10.0
|
---|
44 | // fs.lchown is not available on at least some Linux
|
---|
45 | return typeof fs[key] === 'function'
|
---|
46 | })
|
---|
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 |
|
---|
58 | // Universalify async methods:
|
---|
59 | api.forEach(method => {
|
---|
60 | exports[method] = u(fs[method])
|
---|
61 | })
|
---|
62 |
|
---|
63 | // We differ from mz/fs in that we still ship the old, broken, fs.exists()
|
---|
64 | // since we are a drop-in replacement for the native module
|
---|
65 | exports.exists = function (filename, callback) {
|
---|
66 | if (typeof callback === 'function') {
|
---|
67 | return fs.exists(filename, callback)
|
---|
68 | }
|
---|
69 | return new Promise(resolve => {
|
---|
70 | return fs.exists(filename, resolve)
|
---|
71 | })
|
---|
72 | }
|
---|
73 |
|
---|
74 | // fs.read() & fs.write need special treatment due to multiple callback args
|
---|
75 |
|
---|
76 | exports.read = function (fd, buffer, offset, length, position, callback) {
|
---|
77 | if (typeof callback === 'function') {
|
---|
78 | return fs.read(fd, buffer, offset, length, position, callback)
|
---|
79 | }
|
---|
80 | return new Promise((resolve, reject) => {
|
---|
81 | fs.read(fd, buffer, offset, length, position, (err, bytesRead, buffer) => {
|
---|
82 | if (err) return reject(err)
|
---|
83 | resolve({ bytesRead, buffer })
|
---|
84 | })
|
---|
85 | })
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Function signature can be
|
---|
89 | // fs.write(fd, buffer[, offset[, length[, position]]], callback)
|
---|
90 | // OR
|
---|
91 | // fs.write(fd, string[, position[, encoding]], callback)
|
---|
92 | // We need to handle both cases, so we use ...args
|
---|
93 | exports.write = function (fd, buffer, ...args) {
|
---|
94 | if (typeof args[args.length - 1] === 'function') {
|
---|
95 | return fs.write(fd, buffer, ...args)
|
---|
96 | }
|
---|
97 |
|
---|
98 | return new Promise((resolve, reject) => {
|
---|
99 | fs.write(fd, buffer, ...args, (err, bytesWritten, buffer) => {
|
---|
100 | if (err) return reject(err)
|
---|
101 | resolve({ bytesWritten, buffer })
|
---|
102 | })
|
---|
103 | })
|
---|
104 | }
|
---|
105 |
|
---|
106 | // fs.realpath.native only available in Node v9.2+
|
---|
107 | if (typeof fs.realpath.native === 'function') {
|
---|
108 | exports.realpath.native = u(fs.realpath.native)
|
---|
109 | }
|
---|