Comparing version 0.1.0 to 0.2.0
@@ -0,2 +1,5 @@ | ||
## 0.2.0 (2017-06-16) | ||
- add mkdir method | ||
## 0.1.0 (2017-02-28) | ||
- Init module |
@@ -1,2 +0,2 @@ | ||
/* yocto-sftp - - V0.1.0 */ | ||
"use strict";function Sftp(a){this.logger=a,this.config={},this.schema=require("./modules/schema")(a),this.sftp=require("./modules/sftp")(a)}var logger=require("yocto-logger"),_=require("lodash"),Q=require("q");Sftp.prototype.load=function(a){var b=Q.defer();return this.schema.validate(a,"load").then(function(a){this.config=a,this.logger.info("[ sftp.load ] - config load with success"),b.resolve(a)}.bind(this)).catch(function(a){this.logger.error("[ sftp.load ] - error when loading module"),b.reject(a)}.bind(this)),b.promise},Sftp.prototype.connect=function(){return this.sftp.connect(this.config)},Sftp.prototype.end=function(a){return this.sftp.end(a)},Sftp.prototype.ls=function(a){return this.sftp.ls(this.config,a)},Sftp.prototype.fileExist=function(a){return this.sftp.fileExist(this.config,a)},Sftp.prototype.put=function(a,b){return this.sftp.put(this.config,a,b)},Sftp.prototype.delete=function(a){return this.sftp.delete(this.config,a)},Sftp.prototype.get=function(a,b){return this.sftp.get(this.config,a,b)},module.exports=function(a){return(_.isUndefined(a)||_.isNull(a))&&(logger.warning("[ sftp.constructor ] - Invalid logger given. Use internal logger"),a=logger),new Sftp(a)}; | ||
/* yocto-sftp - This is an wrapper of SSH2 library to simply create sftp connection. - V0.2.0 */ | ||
"use strict";function Sftp(a){this.logger=a,this.config={},this.schema=require("./modules/schema")(a),this.sftp=require("./modules/sftp")(a)}var logger=require("yocto-logger"),_=require("lodash"),Q=require("q");Sftp.prototype.load=function(a){var b=Q.defer();return this.schema.validate(a,"load").then(function(a){this.config=a,this.logger.info("[ sftp.load ] - config load with success"),b.resolve(a)}.bind(this)).catch(function(a){this.logger.error("[ sftp.load ] - error when loading module"),b.reject(a)}.bind(this)),b.promise},Sftp.prototype.connect=function(){return this.sftp.connect(this.config)},Sftp.prototype.end=function(a){return this.sftp.end(a)},Sftp.prototype.ls=function(a){return this.sftp.ls(this.config,a)},Sftp.prototype.fileExist=function(a){return this.sftp.fileExist(this.config,a)},Sftp.prototype.put=function(a,b){return this.sftp.put(this.config,a,b)},Sftp.prototype.delete=function(a){return this.sftp.delete(this.config,a)},Sftp.prototype.get=function(a,b){return this.sftp.get(this.config,a,b)},Sftp.prototype.mkdir=function(a,b){return b=b||!1,this.sftp.mkdir(this.config,a,b)},module.exports=function(a){return(_.isUndefined(a)||_.isNull(a))&&(logger.warning("[ sftp.constructor ] - Invalid logger given. Use internal logger"),a=logger),new Sftp(a)}; |
@@ -1,2 +0,2 @@ | ||
/* yocto-sftp - - V0.1.0 */ | ||
/* yocto-sftp - This is an wrapper of SSH2 library to simply create sftp connection. - V0.2.0 */ | ||
"use strict";function Schema(a){this.logger=a}var logger=require("yocto-logger"),_=require("lodash"),utils=require("yocto-utils"),Q=require("q"),joi=require("joi");Schema.prototype.validate=function(a,b){var c=Q.defer();this.logger.debug("[ Schema.validate ] - validate schema for schemaName = "+b);var d=joi.validate(a,this.getSchema(b));return _.isEmpty(d.error)?c.resolve(d.value):(this.logger.error("[ Schema.validate ] - joi validation failed, details : "+utils.obj.inspect(d.error)),c.reject(d.error)),c.promise},Schema.prototype.getSchema=function(a){var b={load:joi.object().required().keys({host:joi.string().optional().default("localhost"),port:joi.number().integer().default(22),user:joi.string().optional(),password:joi.string().optional(),alogrithms:joi.object().optional(),agent:joi.string().optional()}).unknown()};return b[a]},module.exports=function(a){return(_.isUndefined(a)||_.isNull(a))&&(logger.warning("[ Schema.constructor ] - Invalid logger given. Use internal logger"),a=logger),new Schema(a)}; |
@@ -1,2 +0,2 @@ | ||
/* yocto-sftp - - V0.1.0 */ | ||
"use strict";function Sftp(a){this.logger=a}var logger=require("yocto-logger"),_=require("lodash"),utils=require("yocto-utils"),Q=require("q"),fs=require("fs"),ClientSSH=require("ssh2").Client,path=require("path");Sftp.prototype.connect=function(a){var b=Q.defer(),c=new ClientSSH;return this.logger.debug("[ Sftp.connect ] - connecting to server ..."),c.on("ready",function(){this.logger.debug("[ Sftp.connect.ready ] - connect successful to server"),c.sftp(function(a,d){a&&this.logger.error("[ Sftp.connect.sftp ] - connection establish to server, but sftp connection can't be established, details : ",utils.obj.inspect(a)),this.logger.info("[ Sftp.connect.sftp ] - connect successful to SFTP server"),b.resolve({client:c,sftp:d})}.bind(this))}.bind(this)),c.on("error",function(a){this.logger.error("[ Sftp.connect.error ] - an error occured ",utils.obj.inspect(a)),b.reject(a)}.bind(this)),c.connect(a),b.promise},Sftp.prototype.end=function(a){var b=Q.defer();try{this.logger.debug("[ Sftp.end ] - try to close the connection"),a.client.end(),this.logger.debug("[ Sftp.end ] - the connction was ended"),b.resolve(!0)}catch(a){this.logger.error("[ Sftp.end ] - an error occured when end the conection, more details : ",utils.obj.inspect(a)),b.reject(a)}return b.promise},Sftp.prototype.ls=function(a,b){var c=Q.defer();return b=path.normalize(b),this.connect(a).then(function(a){this.logger.debug("[ Sftp.ls ] - list file in folder : ",b),a.sftp.readdir(b,function(b,d){b&&(this.logger.error("[ Sftp.ls ] - an error occured when execute the list command, more details : ",utils.obj.inspect(b)),c.reject(b)),this.logger.debug("[ Sftp.ls ] - list file command success"),c.resolve(d),this.end(a)}.bind(this))}.bind(this)).catch(function(a){c.reject(a)}),c.promise},Sftp.prototype.fileExist=function(a,b){var c=Q.defer();return b=path.normalize(b),this.logger.info("[ Sftp.fileExist ] - check if the file exist : ",b),this.ls(a,path.dirname(b)).then(function(a){var d=_.find(a,{filename:path.basename(b)});return _.isUndefined(d)?(this.logger.warning("[ Sftp.fileExist ] - the file < "+b+" > don't exist on ftp"),c.reject("the file < "+b+" > don't exist on ftp")):(this.logger.info("[ Sftp.fileExist ] - the file < "+b+" > exist on server"),void c.resolve(d))}.bind(this)).catch(function(a){c.reject(a)}),c.promise},Sftp.prototype.put=function(a,b,c){var d=Q.defer();return b=path.normalize(b),c=path.normalize(c),this.connect(a).then(function(a){this.logger.debug("[ Sftp.put ] - try to put the file < "+b+" > to < "+c+">");var e=fs.createReadStream(b),f=a.sftp.createWriteStream(c);f.on("close",function(){this.logger.info("[ Sftp.put ] - the file was correctly uploaed on path : < "+c+" >"),d.resolve(!0),this.end(a)}.bind(this)),f.on("error",function(b){this.logger.error("[ Sftp.put.ws ] - an error occured, more details : ",utils.obj.inspect(b)),d.reject(b),this.end(a)}.bind(this)),e.on("error",function(b){this.logger.error("[ Sftp.put.rs ] - an error occured, more details : ",utils.obj.inspect(b)),d.reject(b),this.end(a)}.bind(this)),e.pipe(f)}.bind(this)).catch(function(a){d.reject(a)}),d.promise},Sftp.prototype.delete=function(a,b){var c=Q.defer();return b=path.normalize(b),this.connect(a).then(function(a){this.logger.debug("[ Sftp.delete ] - delete the file < "+b+" >"),a.sftp.unlink(b,function(d){d?(this.logger.error("[ Sftp.delete ] - an error occured, more details : "+utils.obj.inspect(d)),c.reject(d)):(this.logger.info("[ Sftp.delete ] - the file was deleted < "+b+" >"),c.resolve(!0)),this.end(a)}.bind(this))}.bind(this)).catch(function(a){c.reject(a)}),c.promise},Sftp.prototype.get=function(a,b,c){var d=Q.defer();return b=path.normalize(b),c=path.normalize(c),this.connect(a).then(function(a){this.logger.debug("[ Sftp.get ] - try to download the file < "+c+" > on remote to < "+b+">"),a.sftp.fastGet(c,b,{},function(c){c?(this.logger.error("[ Sftp.get ] - an error occured, more details : "+utils.obj.inspect(c)),d.reject(c)):(this.logger.info("[ Sftp.get ] - the file was correctly downloaded to path < "+b+" >"),d.resolve(!0)),this.end(a)}.bind(this))}.bind(this)).catch(function(a){d.reject(a)}),d.promise},module.exports=function(a){return(_.isUndefined(a)||_.isNull(a))&&(logger.warning("[ Sftp.constructor ] - Invalid logger given. Use internal logger"),a=logger),new Sftp(a)}; | ||
/* yocto-sftp - This is an wrapper of SSH2 library to simply create sftp connection. - V0.2.0 */ | ||
"use strict";function Sftp(a){this.logger=a}var logger=require("yocto-logger"),_=require("lodash"),utils=require("yocto-utils"),Q=require("q"),fs=require("fs"),ClientSSH=require("ssh2").Client,path=require("path"),async=require("async");Sftp.prototype.connect=function(a){var b=Q.defer(),c=new ClientSSH;return this.logger.debug("[ Sftp.connect ] - connecting to server ..."),c.on("ready",function(){this.logger.debug("[ Sftp.connect.ready ] - connect successful to server"),c.sftp(function(a,d){a&&this.logger.error("[ Sftp.connect.sftp ] - connection establish to server, but sftp connection can't be established, details : ",utils.obj.inspect(a)),this.logger.info("[ Sftp.connect.sftp ] - connect successful to SFTP server"),b.resolve({client:c,sftp:d})}.bind(this))}.bind(this)),c.on("error",function(a){this.logger.error("[ Sftp.connect.error ] - an error occured ",utils.obj.inspect(a)),b.reject(a)}.bind(this)),c.connect(a),b.promise},Sftp.prototype.end=function(a){var b=Q.defer();try{this.logger.debug("[ Sftp.end ] - try to close the connection"),a.client.end(),this.logger.debug("[ Sftp.end ] - the connction was ended"),b.resolve(!0)}catch(a){this.logger.error("[ Sftp.end ] - an error occured when end the conection, more details : ",utils.obj.inspect(a)),b.reject(a)}return b.promise},Sftp.prototype.ls=function(a,b,c){c=c||!1;var d=!1,e=[],f=Q.defer();return b=path.normalize(b),async.series([function(b){return c?(d=!0,b()):void this.connect(a).then(function(a){c=a,b()}.bind(this)).catch(function(a){b(a)})}.bind(this),function(a){this.logger.debug("[ Sftp.ls ] - list file in folder : ",b),c.sftp.readdir(b,function(b,c){return b?(this.logger.error("[ Sftp.ls ] - an error occured when execute the list command, more details : ",utils.obj.inspect(b)),a(b)):(this.logger.debug("[ Sftp.ls ] - list file command success"),e=c,void a())}.bind(this))}.bind(this)],function(a){return d||this.end(c),a?f.reject(a):void f.resolve(e)}.bind(this)),f.promise},Sftp.prototype.fileExist=function(a,b,c){var d=Q.defer();return b=path.normalize(b),this.logger.info("[ Sftp.fileExist ] - check if the file exist : ",b),this.ls(a,path.dirname(b),c).then(function(a){var c=_.find(a,{filename:path.basename(b)});return _.isUndefined(c)?(this.logger.warning("[ Sftp.fileExist ] - the file < "+b+" > don't exist on ftp"),d.reject("the file < "+b+" > don't exist on ftp")):(this.logger.info("[ Sftp.fileExist ] - the file < "+b+" > exist on server"),void d.resolve(c))}.bind(this)).catch(function(a){d.reject(a)}),d.promise},Sftp.prototype.put=function(a,b,c){var d=Q.defer();return b=path.normalize(b),c=path.normalize(c),this.connect(a).then(function(a){this.logger.debug("[ Sftp.put ] - try to put the file < "+b+" > to < "+c+">");var e=fs.createReadStream(b),f=a.sftp.createWriteStream(c);f.on("close",function(){this.logger.info("[ Sftp.put ] - the file was correctly uploaed on path : < "+c+" >"),d.resolve(!0),this.end(a)}.bind(this)),f.on("error",function(b){this.logger.error("[ Sftp.put.ws ] - an error occured, more details : ",utils.obj.inspect(b)),d.reject(b),this.end(a)}.bind(this)),e.on("error",function(b){this.logger.error("[ Sftp.put.rs ] - an error occured, more details : ",utils.obj.inspect(b)),d.reject(b),this.end(a)}.bind(this)),e.pipe(f)}.bind(this)).catch(function(a){d.reject(a)}),d.promise},Sftp.prototype.delete=function(a,b){var c=Q.defer();return b=path.normalize(b),this.connect(a).then(function(a){this.logger.debug("[ Sftp.delete ] - delete the file < "+b+" >"),a.sftp.unlink(b,function(d){d?(this.logger.error("[ Sftp.delete ] - an error occured, more details : "+utils.obj.inspect(d)),c.reject(d)):(this.logger.info("[ Sftp.delete ] - the file was deleted < "+b+" >"),c.resolve(!0)),this.end(a)}.bind(this))}.bind(this)).catch(function(a){c.reject(a)}),c.promise},Sftp.prototype.get=function(a,b,c){var d=Q.defer();return b=path.normalize(b),c=path.normalize(c),this.connect(a).then(function(a){this.logger.debug("[ Sftp.get ] - try to download the file < "+c+" > on remote to < "+b+">"),a.sftp.fastGet(c,b,{},function(c){c?(this.logger.error("[ Sftp.get ] - an error occured, more details : "+utils.obj.inspect(c)),d.reject(c)):(this.logger.info("[ Sftp.get ] - the file was correctly downloaded to path < "+b+" >"),d.resolve(!0)),this.end(a)}.bind(this))}.bind(this)).catch(function(a){d.reject(a)}),d.promise},Sftp.prototype.createFolder=function(a,b){var c=Q.defer();return this.fileExist(null,b,a).then(function(){c.resolve(!1)}.bind(this)).catch(function(){a.sftp.mkdir(b,function(a){return a?c.reject(a):void c.resolve(b)}.bind(this))}.bind(this)),c.promise},Sftp.prototype.mkdir=function(a,b,c){var d=Q.defer();b=path.normalize(b);var e=[b];if(c){var f="";e=_.map(_.compact(_.split(b,"/")),function(a){return f+="/"+a})}return this.connect(a).then(function(a){async.eachSeries(e,function(b,c){this.logger.debug("[ Sftp.mkdir ] - create folder : ",b),this.createFolder(a,b).then(function(a){this.logger.debug("[ Sftp.mkdir ] - "+(a?" folder was created : ":" folder already exist for path : ")+b),c()}.bind(this)).catch(function(a){this.logger.error("[ Sftp.createFolder ] - an error occured , more details : ",utils.obj.inspect(a)),c(a)}.bind(this))}.bind(this),function(c){return this.end(a),c?d.reject(c):(this.logger.info("[ Sftp.mkdir ] - the folder was correctly created : ",b),void d.resolve(b))}.bind(this))}.bind(this)).catch(function(a){d.reject(a)}),d.promise},module.exports=function(a){return(_.isUndefined(a)||_.isNull(a))&&(logger.warning("[ Sftp.constructor ] - Invalid logger given. Use internal logger"),a=logger),new Sftp(a)}; |
{ | ||
"name": "yocto-sftp", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "This is an wrapper of SSH2 library to simply create sftp connection.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -31,2 +31,3 @@ [![NPM](https://nodei.co/npm/yocto-sftp.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/yocto-sftp/) | ||
- ls | ||
- mkdir (with mkdirp) | ||
@@ -208,1 +209,27 @@ | ||
``` | ||
### Create folder and its own parent if speciefied | ||
> This Create folder into sftp | ||
```javascript | ||
var remotePathFOlder = '/toto/tata/titi/tutu'; | ||
// If true parent folder will be created otherwise none | ||
var createParent = true; | ||
// connect | ||
sftp.load(config).then(function () { | ||
console.log('\n --> config success ... '); | ||
sftp.mkdir(remotePathFile, createParent).then(function (list) { | ||
console.log('\n --> create success \n', list); | ||
}).catch(function (error) { | ||
console.log('\n --> create failed ', error); | ||
}); | ||
}).catch(function (error) { | ||
console.log('\n --> error : ', error); | ||
}); | ||
``` |
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
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
26046
234