fs-promise
Advanced tools
Comparing version
143
index.js
@@ -1,52 +0,101 @@ | ||
'use strict'; | ||
'use strict' | ||
var fs, | ||
Prom = require('any-promise'), | ||
slice = Array.prototype.slice, | ||
noError = /exists/, | ||
returnValue = /Sync$|watch|(Read|Write)Stream$|^Stats$/; | ||
var mzfs = require('mz/fs') | ||
var fsExtra = require('fs-extra') | ||
var Promise = require('any-promise') | ||
var thenifyAll = require('thenify-all') | ||
var slice = Array.prototype.slice | ||
try { | ||
fs = require('fs-extra'); | ||
} catch(e) { | ||
try { | ||
fs = require('graceful-fs'); | ||
} catch(e2){ | ||
fs = require('fs'); | ||
} | ||
} | ||
// thenify-all for all fs-extra that make sense to be promises | ||
var fsExtraKeys = [ | ||
'copy', | ||
'emptyDir', | ||
'ensureFile', | ||
'ensureDir', | ||
'ensureLink', | ||
'ensureSymlink', | ||
'mkdirs', | ||
'move', | ||
'outputFile', | ||
'outputJson', | ||
'readJson', | ||
'remove', | ||
'writeJson', | ||
// aliases | ||
'createFile', | ||
'createLink', | ||
'createSymlink', | ||
'emptydir', | ||
'mkdirp', | ||
'readJSON', | ||
'outputJSON', | ||
'writeJSON' | ||
] | ||
thenifyAll.withCallback(fsExtra, exports, fsExtraKeys) | ||
Object.keys(fs).forEach(function(key) { | ||
var func = fs[key]; | ||
if (typeof func == 'function') | ||
if(returnValue.test(key)){ | ||
exports[key] = fs[key]; | ||
} else if(noError.test(key)){ | ||
exports[key] = promiseWithoutError(func); | ||
} else { | ||
exports[key] = promise(func); | ||
} | ||
}); | ||
// Delegate all normal fs to mz/fs | ||
// (this overwrites anything proxies directly above) | ||
var mzKeys = [ | ||
'rename', | ||
'ftruncate', | ||
'chown', | ||
'fchown', | ||
'lchown', | ||
'chmod', | ||
'fchmod', | ||
'stat', | ||
'lstat', | ||
'fstat', | ||
'link', | ||
'symlink', | ||
'readlink', | ||
'realpath', | ||
'unlink', | ||
'rmdir', | ||
'mkdir', | ||
'readdir', | ||
'close', | ||
'open', | ||
'utimes', | ||
'futimes', | ||
'fsync', | ||
'write', | ||
'read', | ||
'readFile', | ||
'writeFile', | ||
'appendFile', | ||
'access', | ||
'exists' | ||
] | ||
mzKeys.forEach(function(key){ | ||
exports[key] = mzfs[key] | ||
}) | ||
function promise(func){ | ||
return function(){ | ||
var args = slice.call(arguments); | ||
return new Prom(function(resolve, reject){ | ||
args.push(function(err, res){ | ||
if(err) reject(err); | ||
else resolve(res); | ||
}); | ||
func.apply(fs, args); | ||
}); | ||
}; | ||
} | ||
function promiseWithoutError(func){ | ||
return function(){ | ||
var args = slice.call(arguments); | ||
return new Prom(function(resolve){ | ||
args.push(resolve); | ||
func.apply(fs, args); | ||
}); | ||
}; | ||
} | ||
// Resolve fs-extra streams as Promise for array | ||
var streamKeys = [ | ||
'walk' | ||
] | ||
streamKeys.forEach(function(key){ | ||
exports[key] = function(){ | ||
var func = fsExtra[key] | ||
var args = slice.call(arguments) | ||
return new Promise(function(resolve, reject){ | ||
var stream = func.apply(fsExtra, args) | ||
var items = [] | ||
stream | ||
.on('data', function(item){ | ||
items.push(item) | ||
}) | ||
.on('end', function(){ | ||
resolve(items) | ||
}) | ||
.on('error', function(error){ | ||
reject(error) | ||
}) | ||
}) | ||
} | ||
}) |
{ | ||
"name": "fs-promise", | ||
"version": "0.4.1", | ||
"description": "Filesystem methods as promises, with optional fs-extra and fs-graceful dependencies", | ||
"version": "0.5.0", | ||
"description": "Filesystem methods as promises with fs-extra", | ||
"main": "index.js", | ||
@@ -22,8 +22,9 @@ "scripts": { | ||
"dependencies": { | ||
"any-promise": "^1.0.0" | ||
"any-promise": "^1.0.0", | ||
"fs-extra": "^0.26.5", | ||
"mz": "^2.3.1", | ||
"thenify-all": "^1.6.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~2.4.2", | ||
"fs-extra": "~0.26.5", | ||
"promise": "~7.1.1", | ||
"es6-promise": "~3.0.2", | ||
@@ -33,5 +34,4 @@ "rsvp": "~3.1.0", | ||
"when": "~3.7.7", | ||
"q": "~1.4.1", | ||
"native-promise-only": "~0.8.1" | ||
"q": "~1.4.1" | ||
} | ||
} |
@@ -5,10 +5,3 @@ # fs-promise | ||
Proxies all async `fs` methods exposing them as ES 2015 (ES6) compatible promises. | ||
Passes all sync methods through as values. | ||
Also exposes to [graceful-fs][1] and/or [fs-extra][2] methods if they are installed. | ||
Uses [any-promise][3] to load preferred `Promise` implementation. | ||
```javascript | ||
@@ -24,10 +17,32 @@ var fsp = require('fs-promise'); | ||
## Usage | ||
## Implementation | ||
Attempts to load libraries in the following order. The first successful require will be proxied to a Promise based implementation. | ||
`fs-promise` is now a thin wrapper on top of [`mz/fs`][4] adding support for async functions from [`fs-extra`][2]. If you do not need the functions from `fs-extra`, consider using `mz` directly. | ||
- [`fs-extra`](https://github.com/jprichardson/node-fs-extra) | ||
- [`graceful-fs`](https://github.com/isaacs/node-graceful-fs) | ||
- `fs` from standard library | ||
* Proxies async [`fs`][1] and [`fs-extra`][2] methods exposing them as ES 2015 (ES6) compatible promises. | ||
* Uses [any-promise][3] to load preferred `Promise` implementation. | ||
* Directly uses [mz/fs][4] for all `fs` functions. | ||
* Proxies `walk` from `fs-extra` to resolve Promise as arrays of items. | ||
* Proxies the following functions from fs-extra using [thenify-all][5]. (Proxies all other functions directly). | ||
```javascript | ||
[ | ||
'copy', | ||
'emptyDir', | ||
'ensureFile', | ||
'ensureDir', | ||
'ensureLink' | ||
'ensureSymlink', | ||
'mkdirs', | ||
'move', | ||
'outputFile', | ||
'outputJson', | ||
'readJson', | ||
'remove', | ||
'writeJson' | ||
] | ||
``` | ||
## Usage | ||
Detects a `Promise` implementation using [`any-promise`][3]. If you have a preferred implementation, or are working in an environment without a global implementation, you must explicitly register a `Promise` implementation and it will be used. See [`any-promise`][3] for details. | ||
@@ -38,10 +53,11 @@ | ||
```bash | ||
$ npm install --save fs-extra | ||
$ npm install --save fs-promise | ||
``` | ||
Note that `fs-extra` depends on `graceful-fs`, so you would get the benefits of both libraries. | ||
Note that `fs-extra` depends on `graceful-fs`, so you will get the benefits of both libraries. | ||
[1]: https://github.com/isaacs/node-graceful-fs | ||
[1]: https://nodejs.org/api/fs.html | ||
[2]: https://www.npmjs.org/package/fs-extra | ||
[3]: https://github.com/kevinbeaty/any-promise | ||
[4]: https://github.com/normalize/mz | ||
[5]: https://github.com/thenables/thenify-all |
'use strict'; | ||
/*globals describe, it, beforeEach, afterEach */ | ||
var fsp = require('..'), | ||
@@ -8,4 +7,6 @@ path = require('path'), | ||
Prom = require('any-promise'), | ||
testdir = path.join(__dirname, 'tmp'); | ||
testdir = path.join(__dirname, 'tmp'), | ||
testdir2 = path.join(__dirname, 'tmp2') | ||
describe('basic', function(){ | ||
@@ -21,15 +22,36 @@ beforeEach(function(){ | ||
it('should create files and readdir', function(){ | ||
return fsp.createFile(file('hello')).then(readtmp).then(function(files){ | ||
return fsp.ensureFile(file('hello')).then(readtmp).then(function(files){ | ||
assert.deepEqual(files.sort(), ['hello']); | ||
return fsp.createFile(file('world')); | ||
return fsp.ensureFile(file('world')); | ||
}).then(readtmp).then(function(files){ | ||
assert.deepEqual(files.sort(), ['hello', 'world']); | ||
}); | ||
return fsp.exists(testdir2) | ||
}).then(function(exists){ | ||
assert.equal(exists, false); | ||
return fsp.move(testdir, testdir2) | ||
}).then(function(){ | ||
return Prom.all([fsp.exists(testdir), fsp.exists(testdir2)]) | ||
}).then(function(exists){ | ||
return assert.deepEqual(exists, [false, true]) | ||
}).then(function(){ | ||
return fsp.copy(testdir2, testdir) | ||
}).then(function(){ | ||
return Prom.all([fsp.exists(testdir), fsp.exists(testdir2)]) | ||
}).then(function(exists){ | ||
return assert.deepEqual(exists, [true, true]) | ||
}).then(readtmps).then(function(files){ | ||
assert.deepEqual(files[0].sort(), files[1].sort()); | ||
return fsp.emptyDir(testdir2); | ||
}).then(readtmp2).then(function(files){ | ||
assert.deepEqual(files, []); | ||
}).then(function(){ | ||
fsp.remove(testdir2); | ||
}) | ||
}); | ||
it('should pass through Sync as value', function(){ | ||
return fsp.createFile(file('hello')).then(function(files){ | ||
return fsp.ensureFile(file('hello')).then(function(files){ | ||
assert(fsp.existsSync(file('hello'))); | ||
assert(!fsp.existsSync(file('world'))); | ||
return fsp.createFile(file('world')); | ||
return fsp.ensureFile(file('world')); | ||
}).then(readtmp).then(function(files){ | ||
@@ -61,2 +83,27 @@ assert(fsp.existsSync(file('hello'))); | ||
}); | ||
it('should pass third argument from write #7', function testWriteFsp() { | ||
return fsp.open(file('some.txt'), 'w+').then(function (fd){ | ||
return fsp.write(fd, "hello fs-promise").then(function(result) { | ||
var written = result[0]; | ||
var text = result[1]; | ||
assert.equal(text.substring(0, written), "hello fs-promise".substring(0, written)) | ||
return fsp.close(fd); | ||
}) | ||
}) | ||
}); | ||
it('should create files and walk #9', function(){ | ||
return fsp.ensureFile(file('hello')).then(readtmp).then(function(files){ | ||
assert.deepEqual(files.sort(), ['hello']); | ||
return fsp.ensureFile(file('world')); | ||
}).then(readtmp).then(function(files){ | ||
assert.deepEqual(files.sort(), ['hello', 'world']); | ||
}).then(function(){ | ||
return fsp.walk(testdir); | ||
}).then(function(items){ | ||
// tmp, tmp/hello tmp/world | ||
assert.equal(items.length, 3); | ||
}); | ||
}); | ||
}); | ||
@@ -81,1 +128,9 @@ | ||
} | ||
function readtmp2(){ | ||
return fsp.readdir(testdir2); | ||
} | ||
function readtmps(){ | ||
return Prom.all([readtmp(), readtmp2()]); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
11388
61.62%6
-33.33%11
22.22%267
132.17%61
35.56%4
300%3
50%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added