Comparing version 0.3.4 to 0.3.5
@@ -74,2 +74,8 @@ var Client = require('ssh2').Client | ||
}, | ||
exists: function(remotePath, next) { | ||
sftp.stat(remotePath, function(err, stat) { | ||
if (err && err.message !== 'No such file') return next(err) | ||
return next(null, !!stat) | ||
}) | ||
}, | ||
disconnect: function(next) { | ||
@@ -76,0 +82,0 @@ if (!sftp.isConnected()) return next() |
{ | ||
"name": "whoosh", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"description": "A streaming sftp client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -78,2 +78,15 @@ # Whoosh | ||
#### exists | ||
Reports on whether a remote file exists | ||
```js | ||
Whoosh.connect(config, function(err, whoosh) { | ||
if (err) return bail(err) | ||
whoosh.exists('some/remote/file', function(err, exists) { | ||
whoosh.disconnect(function() { | ||
if (err) return bail(err) | ||
console.log(exists ? 'File exists' : 'File does not exist') | ||
)} | ||
}) | ||
}) | ||
#### Everything else | ||
@@ -80,0 +93,0 @@ |
@@ -203,2 +203,41 @@ var assert = require('assert') | ||
it('should report if a file exists', function(done) { | ||
var title = this.test.title | ||
Whoosh.connect(config, function(err, whoosh) { | ||
assert.ifError(err) | ||
whoosh.putContent('test message', getRemotePath(title), function(err, stats) { | ||
assert.ifError(err) | ||
whoosh.exists(getRemotePath(title), function(err, exists) { | ||
assert.ifError(err) | ||
assert.equal(exists, true) | ||
whoosh.disconnect(done) | ||
}) | ||
}) | ||
}) | ||
}) | ||
it('should report if a file does not exist', function(done) { | ||
var title = this.test.title | ||
Whoosh.connect(config, function(err, whoosh) { | ||
assert.ifError(err) | ||
whoosh.exists(getRemotePath(title), function(err, exists) { | ||
assert.ifError(err) | ||
assert.equal(exists, false) | ||
whoosh.disconnect(done) | ||
}) | ||
}) | ||
}) | ||
it('should not disconnect after checking if a non existent file exists', function(done) { | ||
var title = this.test.title | ||
Whoosh.connect(config, function(err, whoosh) { | ||
assert.ifError(err) | ||
whoosh.exists(getRemotePath(title), function(err, exists) { | ||
assert.ifError(err) | ||
assert.ok(whoosh.isConnected()) | ||
whoosh.disconnect(done) | ||
}) | ||
}) | ||
}) | ||
function getRemotePath(filename) { | ||
@@ -205,0 +244,0 @@ return 'files/uploads/' + (filename ? filename.replace(/\W/g, '_') : '') |
19123
319
108