Comparing version 0.1.1 to 0.1.2
@@ -1,1 +0,1 @@ | ||
module.exports=function(){function t(t){"use strict";var i=[];if("undefined"==typeof t.username&&i.push(new s("You must specify a valid username")),"undefined"==typeof t.host&&i.push(new s("You must specify a valid host")),"undefined"==typeof t.privateKey?i.push(new s("You must specify path to a privateKey")):n.existsSync(t.privateKey)?n.lstatSync(t.privateKey).isFile()||i.push(new s("The privateKey specified isn't page")):i.push(new s("The privateKey specified doesn't exist")),i.length>0)throw i;t.privateKey=n.readFileSync(t.privateKey),t.port=t.port||22,this.config=t,this.status=!1;try{this.conn=new e}catch(s){throw[s]}}var n=require("fs"),e=require("ssh2"),i=require("a-promise");return t.prototype.connect=function(){"use strict";return new i(function(t,n){try{this.conn.on("error",n),this.conn.on("ready",function(){this.status=!0,t()}.bind(this)),this.conn.connect(this.config)}catch(e){n(e)}}.bind(this))},t.prototype.mkdir=function(t){"use strict";return this.exec("mkdir -p "+t)},t.prototype.exec=function(t,n){"use strict";return n=n||{},new i(function(e,i){return this.status?(n.cwd&&(t="cd "+n.cwd+"; "+t),void this.conn.exec(t,function(t,n){if(t)return i(t);var s={stdout:"",stderr:""};n.on("close",function(){s.stdout=s.stdout.toString(),s.stderr=s.stderr.toString(),e(s)}).on("data",function(t){s.stdout=t}).stderr.on("data",function(t){s.stderr=t})})):i("Not yet connected to server")}.bind(this))},t.prototype.put=function(t,e){"use strict";return new i(function(i,s){return n.existsSync(t)?void this.conn.sftp(function(n,o){return n?s(n):void o.fastPut(t,e,function(n){return n?void("No such file"===n.message?this.mkdir(e.split("/").slice(0,-1).join("/")).then(function(){this.put(t,e).then(i)}):s(n)):i()}.bind(this))}.bind(this)):s("Local File doesn't exist")}.bind(this))},t.prototype.get=function(t,e){"use strict";return new i(function(i,s){this.conn.sftp(function(o,r){if(o)return s(o);var u=[],c=r.createReadStream(t);c.on("data",function(t){u.push(t.toString())}),c.on("error",function(t){s(t)}),c.on("close",function(){return u=u.join(""),e?void n.writeFile(e,u,function(t){t?s(t):i(u)}):i(u)})}.bind(this))}.bind(this))},t}(); | ||
module.exports=function(){function t(t){"use strict";var i=[];if("undefined"==typeof t.username&&i.push(new o("You must specify a valid username")),"undefined"==typeof t.host&&i.push(new o("You must specify a valid host")),"undefined"==typeof t.privateKey?i.push(new o("You must specify path to a privateKey")):n.existsSync(t.privateKey)?n.lstatSync(t.privateKey).isFile()||i.push(new o("The privateKey specified isn't page")):i.push(new o("The privateKey specified doesn't exist")),i.length>0)throw i;t.privateKey=n.readFileSync(t.privateKey),t.port=t.port||22,this.config=t,this.status=!1;try{this.conn=new e}catch(o){throw[o]}}var n=require("fs"),e=require("ssh2"),i=require("a-promise");return t.prototype.connect=function(){"use strict";return new i(function(t,n){try{this.conn.on("error",n),this.conn.on("ready",function(){this.status=!0,t()}.bind(this)),this.conn.connect(this.config)}catch(e){n(e)}}.bind(this))},t.prototype.mkdir=function(t){"use strict";return this.exec("mkdir -p "+t)},t.prototype.exec=function(t,n){"use strict";return n=n||{},new i(function(e,i){return this.status?(n.cwd&&(t="cd "+n.cwd+"; "+t),void this.conn.exec(t,function(t,n){if(t)return i(t);var o={stdout:"",stderr:""};n.on("close",function(){o.stdout=o.stdout.toString(),o.stderr=o.stderr.toString(),e(o)}).on("data",function(t){o.stdout=t}).stderr.on("data",function(t){o.stderr=t})})):i("Not yet connected to server")}.bind(this))},t.prototype.put=function(t,e){"use strict";var o=this;return new i(function(i,r){return n.existsSync(t)?void o.conn.sftp(function(n,s){return n?r(n):void s.fastPut(t,e,function(n){return n?"No such file"!==n.message?r(n):void o.mkdir(e.split("/").slice(0,-1).join("/")).then(function(){o.put(t,e).then(i)}):i()})}):r("Local File doesn't exist")})},t.prototype.putMulti=function(t){"use strict";var e=this;return new i(function(o,r){e.conn.sftp(function(s,u){if(s)return r(s);var c=[];t.forEach(function(t){t.Local&&t.Remote&&n.existsSync(t.Local)&&c.push(new i(function(n,i){u.fastPut(t.Local,t.Remote,function(o){return o?"No such file"!==o.message?i(o):void e.mkdir(t.Remote.split("/").slice(0,-1).join("/")).then(function(){e.put(t.Local,t.Remote).then(n)}):n()})}))}),i.all(c).then(o,r)})})},t.prototype.get=function(t,e){"use strict";var o=this;return new i(function(i,r){o.conn.sftp(function(o,s){if(o)return r(o);var u=[],c=s.createReadStream(t);c.on("data",function(t){u.push(t.toString())}),c.on("error",function(t){r(t)}),c.on("close",function(){return u=u.join(""),e?void n.writeFile(e,u,function(t){t?r(t):i(u)}):i(u)})})})},t}(); |
@@ -1,1 +0,1 @@ | ||
var node_ssh,ssh;node_ssh=require("./build/index"),ssh=new node_ssh({host:"localhost",username:"steel",privateKey:"/home/steel/.ssh/id_rsa"}),ssh.connect().then(function(){console.log("I am now connected to it"),ssh.get("/home/steel/.ssh/1.txt","/home/steel/.ssh/3.txt").then(function(e){console.log(e)},function(e){console.log(e)})}); | ||
var node_ssh,ssh;node_ssh=require("./build/index"),ssh=new node_ssh({host:"localhost",username:"steel",privateKey:"/home/steel/.ssh/id_rsa"}),ssh.connect().then(function(){console.log("I am now connected to it"),ssh.putMulti([{Local:"/home/steel/.ssh/1.txt",Remote:"/home/steel/.ssh/3.txt"},{Local:"/home/steel/.ssh/2.txt",Remote:"/home/steel/.ssh/4.txt"}]).then(function(){console.log("Done")},function(){console.log("Error")})}); |
@@ -66,6 +66,6 @@ module.exports = (function(){ | ||
}).on('data',function(data){ | ||
toReturn.stdout = data | ||
toReturn.stdout = data; | ||
}).stderr.on('data',function(data){ | ||
toReturn.stderr = data | ||
}); | ||
toReturn.stderr = data; | ||
}); | ||
}); | ||
@@ -75,6 +75,7 @@ }.bind(this)); | ||
put(localFile,remoteFile){ | ||
var Self = this; | ||
return new Promise(function(resolve,reject){ | ||
if(!fs.existsSync(localFile)) | ||
return reject("Local File doesn't exist"); | ||
this.conn.sftp(function(error,sftp){ | ||
Self.conn.sftp(function(error,sftp){ | ||
if(error) | ||
@@ -85,16 +86,46 @@ return reject(error); | ||
return resolve(); | ||
if (error.message === 'No such file'){ | ||
this.mkdir(remoteFile.split("/").slice(0,-1).join('/')).then(function(){ | ||
this.put(localFile,remoteFile).then(resolve); | ||
}); | ||
} else { | ||
reject(error); | ||
if (error.message !== 'No such file') { | ||
return reject(error) | ||
} | ||
}.bind(this)); | ||
}.bind(this)); | ||
}.bind(this)); | ||
Self.mkdir(remoteFile.split("/").slice(0,-1).join('/')).then(function(){ | ||
Self.put(localFile,remoteFile).then(resolve); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
// Shape('Local' => string, 'Remote' => string) | ||
putMulti(Files){ | ||
var Self = this; | ||
return new Promise(function(resolve,reject){ | ||
Self.conn.sftp(function(error,sftp){ | ||
if(error) | ||
return reject(error); | ||
var Promises = []; | ||
Files.forEach(function(Entry){ | ||
if(!Entry.Local || !Entry.Remote) | ||
return ; // Skip invalid entries | ||
if(!fs.existsSync(Entry.Local)) | ||
return ; // Skip non-existing ones | ||
Promises.push(new Promise(function(resolve,reject){ | ||
sftp.fastPut(Entry.Local,Entry.Remote,function(error){ | ||
if(!error) | ||
return resolve(); | ||
if (error.message !== 'No such file') { | ||
return reject(error) | ||
} | ||
Self.mkdir(Entry.Remote.split("/").slice(0,-1).join('/')).then(function(){ | ||
Self.put(Entry.Local,Entry.Remote).then(resolve); | ||
}); | ||
}); | ||
})) | ||
}); | ||
Promise.all(Promises).then(resolve,reject); | ||
}); | ||
}); | ||
} | ||
get(remoteFile,localFile){ | ||
var Self = this; | ||
return new Promise(function(resolve,reject){ | ||
this.conn.sftp(function(error,sftp){ | ||
Self.conn.sftp(function(error,sftp){ | ||
if(error) | ||
@@ -122,4 +153,4 @@ return reject(error); | ||
}); | ||
}.bind(this)); | ||
}.bind(this)); | ||
}); | ||
}); | ||
} | ||
@@ -126,0 +157,0 @@ } |
@@ -14,7 +14,10 @@ var node_ssh, ssh; | ||
console.log("I am now connected to it"); | ||
ssh.get('/home/steel/.ssh/1.txt','/home/steel/.ssh/3.txt').then(function(contents){ | ||
console.log(contents); | ||
},function(error){ | ||
console.log(error); | ||
ssh.putMulti([ | ||
{Local: '/home/steel/.ssh/1.txt', Remote: '/home/steel/.ssh/3.txt'}, | ||
{Local: '/home/steel/.ssh/2.txt', Remote: '/home/steel/.ssh/4.txt'} | ||
]).then(function(){ | ||
console.log("Done"); | ||
},function(){ | ||
console.log("Error"); | ||
}); | ||
}); |
{ | ||
"name": "node-ssh", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "SS2 with Promises", | ||
@@ -5,0 +5,0 @@ "main": "./lib/build/index.js", |
@@ -25,2 +25,9 @@ Node-SSH - SSH2 with Promises | ||
}); | ||
// Array<Shape('Local' => string, 'Remote' => string)> | ||
ssh.putMulti([{'Local': '/home/steel/Lab/LocalSource', 'Remote': '/home/steel/Lab/RemoteTarget'}]).then(function() { | ||
console.log("The Directory thing is done"); | ||
}, function(error) { | ||
console.log("Something's wrong"); | ||
console.log(error); | ||
}); | ||
// Source, Target | ||
@@ -27,0 +34,0 @@ ssh.get('/home/steel/Lab/RemoteSource', '/home/steel/Lab/LocalTarget').then(function(Contents) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
10641
173
46