fs-extra-promise
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -41,1 +41,12 @@ # Changelog | ||
* Change `main` in package.json to `./lib/index` (closes #6) | ||
## 0.3.0 | ||
* `usePromise` method | ||
* Update bluebird dependency | ||
* Update dev dependencies | ||
* README update | ||
Breaking changes: | ||
* Now creates a new instance of `fs` rather than adding methods to the global `fs-extra` module |
@@ -6,46 +6,57 @@ // -------------------- | ||
// modules | ||
var fs = require('fs-extra'), | ||
var fsExtra = require('fs-extra'), | ||
Promise = require('bluebird'); | ||
// exports | ||
module.exports = fs; | ||
module.exports = makeFs(Promise); | ||
// extend fs with isDirectory and isDirectorySync methods | ||
fs.isDirectory = function(path, callback) { | ||
fs.stat(path, function(err, stats) { | ||
if (err) return callback(err); | ||
callback(null, stats.isDirectory()); | ||
}); | ||
}; | ||
function makeFs(Promise) { | ||
// clone fs-extra | ||
var fs = {}; | ||
for (var methodName in fsExtra) { | ||
fs[methodName] = fsExtra[methodName]; | ||
} | ||
fs.isDirectorySync = function(path) { | ||
return fs.statSync(path).isDirectory(); | ||
}; | ||
// extend fs with isDirectory and isDirectorySync methods | ||
fs.isDirectory = function(path, callback) { | ||
fs.stat(path, function(err, stats) { | ||
if (err) return callback(err); | ||
// promisify all methods | ||
// (except those ending with 'Sync', classes and various methods which do not use a callback) | ||
var method; | ||
for (var name in fs) { | ||
method = fs[name]; | ||
if (typeof method != 'function') continue; | ||
if (name.slice(-4) == 'Sync') continue; | ||
if (name.match(/^[A-Z]/)) continue; | ||
if (['exists', 'watch', 'watchFile', 'unwatchFile', 'createReadStream'].indexOf(name) != -1) continue; | ||
fs[name + 'Async'] = Promise.promisify(method); | ||
} | ||
callback(null, stats.isDirectory()); | ||
}); | ||
}; | ||
// create fs.existsAsync() | ||
// fs.exists() is asynchronous but does not call callback with usual node (err, result) signature - uses just (result) | ||
fs.existsAsync = function(path) { | ||
return new Promise(function(resolve) { | ||
fs.exists(path, function(exists) { | ||
resolve(exists); | ||
fs.isDirectorySync = function(path) { | ||
return fs.statSync(path).isDirectory(); | ||
}; | ||
// promisify all methods | ||
// (except those ending with 'Sync', classes and various methods which do not use a callback) | ||
var method; | ||
for (methodName in fs) { | ||
method = fs[methodName]; | ||
if (typeof method != 'function') continue; | ||
if (methodName.slice(-4) == 'Sync') continue; | ||
if (methodName.match(/^[A-Z]/)) continue; | ||
if (['exists', 'watch', 'watchFile', 'unwatchFile', 'createReadStream'].indexOf(methodName) != -1) continue; | ||
fs[methodName + 'Async'] = Promise.promisify(method); | ||
} | ||
// create fs.existsAsync() | ||
// fs.exists() is asynchronous but does not call callback with usual node (err, result) signature - uses just (result) | ||
fs.existsAsync = function(path) { | ||
return new Promise(function(resolve) { | ||
fs.exists(path, function(exists) { | ||
resolve(exists); | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; | ||
// expose Promise object so it can be augmented (e.g. by using bluebird-extra module) | ||
fs.Promise = Promise; | ||
// usePromise method to set Promise used internally (e.g. by using bluebird-extra module) | ||
fs.usePromise = makeFs; | ||
// return fs | ||
return fs; | ||
} |
{ | ||
"name": "fs-extra-promise", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Node file system library and fs-extra module promisified with bluebird", | ||
@@ -18,10 +18,10 @@ "main": "./lib/index", | ||
"fs-extra": "^0.24.0", | ||
"bluebird": "^2.10.0" | ||
"bluebird": "^2.10.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.3.2", | ||
"chai": "^3.2.0", | ||
"mocha": "^2.3.3", | ||
"chai": "^3.3.0", | ||
"chai-as-promised": "^5.1.0", | ||
"jshint": "^2.8.0", | ||
"istanbul": "^0.3.19", | ||
"istanbul": "^0.3.21", | ||
"coveralls": "^2.11.4" | ||
@@ -28,0 +28,0 @@ }, |
@@ -19,21 +19,25 @@ # fs-extra-promise.js | ||
Additionally, it creates promisified versions of all fs's and fs-extra's async methods, using [bluebird](https://www.npmjs.org/package/bluebird). These methods are named the same as the original fs/fs-extra methods with 'Async' added to the end of the methods. | ||
Additionally, it creates promisified versions of all `fs`'s and `fs-extra`'s async methods, using [bluebird](https://www.npmjs.org/package/bluebird). These methods are named the same as the original `fs`/`fs-extra` methods with `'Async'` added to the end of the method names. | ||
So instead of: | ||
var fs = require('fs'); | ||
fs.readFile(path, function(err, data) { | ||
console.log(data); | ||
}); | ||
```js | ||
var fs = require('fs'); | ||
fs.readFile(path, function(err, data) { | ||
console.log(data); | ||
}); | ||
``` | ||
You can now: | ||
var fs = require('fs-extra-promise'); | ||
fs.readFileAsync(path).then(function(data) { | ||
console.log(data); | ||
}); | ||
```js | ||
var fs = require('fs-extra-promise'); | ||
fs.readFileAsync(path).then(function(data) { | ||
console.log(data); | ||
}); | ||
``` | ||
All original fs and fs-extra methods are included unmodified. | ||
All original `fs` and `fs-extra` methods are included unmodified. | ||
### isDirectory() methods | ||
### `isDirectory()` methods | ||
@@ -44,2 +48,18 @@ For convenience, additional methods `isDirectory()`, `isDirectorySync()` and `isDirectoryAsync()` are provided. | ||
### `usePromise()` method | ||
Creates a new instance of `fs-extra-promise`, which uses the Promise implementation provided. | ||
```js | ||
var Bluebird = require('bluebird'); | ||
var fs = require('fs-extra-promise').usePromise(Bluebird); | ||
// now use `fs-extra-promise` in the usual way | ||
var promise = fs.readFileAsync(path); | ||
console.log(promise instanceof Bluebird); // true | ||
``` | ||
This can be useful for using a Promise implementation that supports `cls`, or an augmented version of Bluebird like [bluebird-extra](https://www.npmjs.org/package/bluebird-extra). | ||
## Tests | ||
@@ -53,3 +73,3 @@ | ||
See changelog.md | ||
See [changelog.md](https://github.com/overlookmotel/fs-extra-promise/blob/master/changelog.md) | ||
@@ -56,0 +76,0 @@ ## Issues |
9431
49
76
Updatedbluebird@^2.10.1