Skip to content

Commit dba0cbb

Browse files
committed
Universalify native fs methods
1 parent 03e1b47 commit dba0cbb

File tree

5 files changed

+121
-11
lines changed

5 files changed

+121
-11
lines changed

lib/__tests__/fs-integration.test.js lib/fs/__tests__/fs-integration.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const os = require('os')
44
const fs = require('fs')
5-
const fse = require('../')
5+
const fse = require('../..')
66
const path = require('path')
77
const assert = require('assert')
88

lib/fs/__tests__/mz.test.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This is adapted from https://github.com/normalize/mz
2+
// Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors
3+
4+
/* eslint-env mocha */
5+
const assert = require('assert')
6+
const fs = require('../..')
7+
8+
describe('fs', () => {
9+
it('.stat()', done => {
10+
fs.stat(__filename).then(function (stats) {
11+
assert.equal(typeof stats.size, 'number')
12+
done()
13+
}).catch(done)
14+
})
15+
16+
it('.statSync()', () => {
17+
const stats = fs.statSync(__filename)
18+
assert.equal(typeof stats.size, 'number')
19+
})
20+
21+
it('.exists()', done => {
22+
fs.exists(__filename).then(function (exists) {
23+
assert(exists)
24+
done()
25+
}).catch(done)
26+
})
27+
28+
it('.existsSync()', () => {
29+
const exists = fs.existsSync(__filename)
30+
assert(exists)
31+
})
32+
33+
describe('callback support', () => {
34+
it('.stat()', done => {
35+
fs.stat(__filename, function (err, stats) {
36+
assert(!err)
37+
assert.equal(typeof stats.size, 'number')
38+
done()
39+
})
40+
})
41+
42+
// This test is different from mz/fs, since we are a drop-in replacement for native fs
43+
it('.exists()', done => {
44+
fs.exists(__filename, function (exists) {
45+
assert(exists)
46+
done()
47+
})
48+
})
49+
})
50+
})

lib/fs/index.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This is adapted from https://github.com/normalize/mz
2+
// Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors
3+
const u = require('universalify').fromCallback
4+
const fs = require('graceful-fs')
5+
6+
const api = [
7+
'rename',
8+
'ftruncate',
9+
'chown',
10+
'fchown',
11+
'lchown',
12+
'chmod',
13+
'fchmod',
14+
'stat',
15+
'lstat',
16+
'fstat',
17+
'link',
18+
'symlink',
19+
'readlink',
20+
'realpath',
21+
'unlink',
22+
'rmdir',
23+
'mkdir',
24+
'readdir',
25+
'close',
26+
'open',
27+
'utimes',
28+
'futimes',
29+
'fsync',
30+
'fdatasync',
31+
'write',
32+
'read',
33+
'readFile',
34+
'writeFile',
35+
'appendFile',
36+
'truncate'
37+
]
38+
// fs.mkdtemp() was added in Node.js v5.10.0, so check if it exists
39+
typeof fs.mkdtemp === 'function' && api.push('mkdtemp')
40+
41+
// Export all keys:
42+
Object.keys(fs).forEach(key => {
43+
exports[key] = fs[key]
44+
})
45+
46+
// Universalify async methods:
47+
api.forEach(method => {
48+
exports[method] = u(fs[method])
49+
})
50+
51+
// We differ from mz/fs in that we still ship the old, broken, fs.exists()
52+
// since we are a drop-in replacement for the native module
53+
exports.exists = function (filename, callback) {
54+
if (typeof callback === 'function') {
55+
return fs.exists(filename, function (exists) {
56+
callback(exists)
57+
})
58+
}
59+
return new Promise(function (resolve) {
60+
return fs.exists(filename, function (exists) {
61+
resolve(exists)
62+
})
63+
})
64+
}

lib/index.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
const assign = require('./util/assign')
44

5-
const fse = {}
6-
const gfs = require('graceful-fs')
7-
8-
// attach fs methods to fse
9-
Object.keys(gfs).forEach(key => {
10-
fse[key] = gfs[key]
11-
})
12-
13-
const fs = fse
5+
const fs = {}
146

7+
// Export graceful-fs:
8+
assign(fs, require('./fs'))
9+
// Export extra methods:
1510
assign(fs, require('./copy'))
1611
assign(fs, require('./copy-sync'))
1712
assign(fs, require('./mkdirs'))

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"license": "MIT",
3535
"dependencies": {
3636
"graceful-fs": "^4.1.2",
37-
"jsonfile": "^2.1.0"
37+
"jsonfile": "^2.1.0",
38+
"universalify": "^0.1.0"
3839
},
3940
"devDependencies": {
4041
"coveralls": "^2.11.2",

0 commit comments

Comments
 (0)