Skip to content

Commit

Permalink
Universalify native fs methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Apr 26, 2017
1 parent 03e1b47 commit dba0cbb
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 11 deletions.
Expand Up @@ -2,7 +2,7 @@

const os = require('os')
const fs = require('fs')
const fse = require('../')
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
50 changes: 50 additions & 0 deletions lib/fs/__tests__/mz.test.js
@@ -0,0 +1,50 @@
// This is adapted from https://github.com/normalize/mz
// Copyright (c) 2014-2016 Jonathan Ong me@jongleberry.com and Contributors

/* eslint-env mocha */
const assert = require('assert')
const fs = require('../..')

describe('fs', () => {
it('.stat()', done => {
fs.stat(__filename).then(function (stats) {
assert.equal(typeof stats.size, 'number')
done()
}).catch(done)
})

it('.statSync()', () => {
const stats = fs.statSync(__filename)
assert.equal(typeof stats.size, 'number')
})

it('.exists()', done => {
fs.exists(__filename).then(function (exists) {
assert(exists)
done()
}).catch(done)
})

it('.existsSync()', () => {
const exists = fs.existsSync(__filename)
assert(exists)
})

describe('callback support', () => {
it('.stat()', done => {
fs.stat(__filename, function (err, stats) {
assert(!err)
assert.equal(typeof stats.size, 'number')
done()
})
})

// This test is different from mz/fs, since we are a drop-in replacement for native fs
it('.exists()', done => {
fs.exists(__filename, function (exists) {
assert(exists)
done()
})
})
})
})
64 changes: 64 additions & 0 deletions lib/fs/index.js
@@ -0,0 +1,64 @@
// 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 = [
'rename',
'ftruncate',
'chown',
'fchown',
'lchown',
'chmod',
'fchmod',
'stat',
'lstat',
'fstat',
'link',
'symlink',
'readlink',
'realpath',
'unlink',
'rmdir',
'mkdir',
'readdir',
'close',
'open',
'utimes',
'futimes',
'fsync',
'fdatasync',
'write',
'read',
'readFile',
'writeFile',
'appendFile',
'truncate'
]
// fs.mkdtemp() was added in Node.js v5.10.0, so check if it exists
typeof fs.mkdtemp === 'function' && api.push('mkdtemp')

// Export all keys:
Object.keys(fs).forEach(key => {
exports[key] = fs[key]
})

// 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, function (exists) {
callback(exists)
})
}
return new Promise(function (resolve) {
return fs.exists(filename, function (exists) {
resolve(exists)
})
})
}
13 changes: 4 additions & 9 deletions lib/index.js
Expand Up @@ -2,16 +2,11 @@

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

const fse = {}
const gfs = require('graceful-fs')

// attach fs methods to fse
Object.keys(gfs).forEach(key => {
fse[key] = gfs[key]
})

const fs = fse
const fs = {}

// Export graceful-fs:
assign(fs, require('./fs'))
// Export extra methods:
assign(fs, require('./copy'))
assign(fs, require('./copy-sync'))
assign(fs, require('./mkdirs'))
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -34,7 +34,8 @@
"license": "MIT",
"dependencies": {
"graceful-fs": "^4.1.2",
"jsonfile": "^2.1.0"
"jsonfile": "^2.1.0",
"universalify": "^0.1.0"
},
"devDependencies": {
"coveralls": "^2.11.2",
Expand Down

0 comments on commit dba0cbb

Please sign in to comment.