description and source-codegulp-sftp = function (options) {
options = assign({}, options);// credit sindresorhus
if (options.host === undefined) {
throw new gutil.PluginError('gulp-sftp', '`host` required.');
}
var fileCount = 0;
var remotePath = options.remotePath || '/';
var remotePlatform = options.remotePlatform || options.platform || 'unix';
options.authKey = options.authKey||options.auth;
var authFilePath = options.authFile || '.ftppass';
var authFile=path.join('./',authFilePath);
if(options.authKey && fs.existsSync(authFile)){
var auth = JSON.parse(fs.readFileSync(authFile,'utf8'))[options.authKey];
if(!auth)
this.emit('error', new gutil.PluginError('gulp-sftp', 'Could not find authkey in .ftppass'));
if(typeof auth == "string" && auth.indexOf(":")!=-1){
var authparts = auth.split(":");
auth = {user:authparts[0],pass:authparts[1]};
}
for (var attr in auth) { options[attr] = auth[attr]; }
}
//option aliases
options.password = options.password||options.pass;
options.username = options.username||options.user||'anonymous';
var key = options.key || options.keyLocation || null;
if(key&&typeof key == "string")
key = {location:key};
//check for other options that imply a key or if there is no password
if(!key && (options.passphrase||options.keyContents||!options.password)){
key = {};
}
if(key){
//aliases
key.contents=key.contents||options.keyContents;
key.passphrase=key.passphrase||options.passphrase;
//defaults
key.location=key.location||["~/.ssh/id_rsa","/.ssh/id_rsa","~/.ssh/id_dsa","/.ssh/id_dsa"];
//type normalization
if(!util.isArray(key.location))
key.location=[key.location];
//resolve all home paths
if(key.location){
var home = process.env.HOME||process.env.USERPROFILE;
for(var i=0;i<key.location.length;i++)
if (key.location[i].substr(0,2) === '~/')
key.location[i] = path.resolve(home,key.location[i].replace(/^~\//,""));
for(var i=0,keyPath;keyPath=key.location[i++];){
if(fs.existsSync(keyPath)){
key.contents = fs.readFileSync(keyPath);
break;
}
}
}else if(!key.contents){
this.emit('error', new gutil.PluginError('gulp-sftp', 'Cannot find RSA key, searched: '+key.location.join(', ')));
}
}
/*
* End Key normalization, key should now be of form:
* {location:Array,passphrase:String,contents:String}
* or null
*/
var logFiles = options.logFiles === false ? false : true;
delete options.remotePath;
delete options.localPath;
delete options.user;
delete options.pass;
delete options.logFiles;
var mkDirCache = {};
var finished=false;
var sftpCache = null;//sftp connection cache
var connectionCache = null;//ssh connection cache
var pool = function(remotePath, uploader){ // method to get cache or create connection
if(sftpCache)
return uploader(sftpCache);
if(options.password){
gutil.log('Authenticating with password.');
}else if(key){
gutil.log('Authenticating with private key.');
}
var c = new Connection();
connectionCache = c;
c.on('ready', function() {
c.sftp(function(err, sftp) {
if (err)
throw err;
sftp.on('end', function() {
gutil.log('SFTP :: SFTP session closed');
sftpCache=null;
if(!finished)
this.emit('error', new gutil.PluginError('gulp-sftp', "SFTP abrupt closure"));
});
sftpCache = sftp;
uploader(sftpCache);
});//c.sftp
});//c.on('ready')
var s ...