Skip to content

Instantly share code, notes, and snippets.

@alroniks
Created November 29, 2013 13:47
Show Gist options
  • Save alroniks/7705924 to your computer and use it in GitHub Desktop.
Save alroniks/7705924 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var Connection = require('ssh2');
var chokidar = require('chokidar');
var c = new Connection();
var local_path;
var deploy_path;
process.argv.forEach(function (val, index, array) {
var item_arr = val.split('=');
if(item_arr.length > 1){
switch(item_arr[0]){
case 'local_path':
local_path = item_arr[1];
break;
case 'deploy_path':
deploy_path = item_arr[1];
break;
}
}
});
if(!local_path){
console.log('local_path not defined');
return false;
}
if(!deploy_path){
console.log('deploy_path not defined');
return false;
}
var ssh = {
host: 'hostname',
port: 22,
username: 'user',
password: 'pass'
};
var time = new Date();
c.on('connect', function() {
console.log('Connection :: connect');
});
c.on('ready', function() {
c.sftp(function(err,sftp){
if(!err){
console.log('sftp start');
var watcher = chokidar.watch(local_path,
{ignored: // функция отвечающая за игнорирование файлов. я добавил сюда пути папки SVN и самого PhpStorm
function(path){
if(path.indexOf(".idea") >= 0 || path.indexOf(".svn") >= 0 || path == (local_path+'/dpl')){
return true;
}
},
ignoreInitial:true // параметр отчающий за игнорирование файлов при инициализации скрипта. Если false - будет литься весь проект при каждом запуске
});
watcher
.on('add', function(path,meta) {
console.log('upload added',path.replace(local_path,deploy_path));
sftp.fastPut(path,path.replace(local_path,deploy_path),{},function(err){
if(err){
if(err.toString().indexOf('such file') >= 0){
console.log('start mkdir');
var path_arr = path.replace(local_path,deploy_path).split('/');
var path_mini_arr = [];
for(i in path_arr){
path_mini_arr.push(path_arr[i]);
if(path_mini_arr.length != path_arr.length){
sftp.mkdir(path_mini_arr.join('/'),{},function(err){
if(err && err.toString().indexOf('ailure') == -1 && err.toString().indexOf('such file') == -1){
console.log('mkdir err',err);
}
});
}
if(path_mini_arr.length == path_arr.length){
watcher.emit('change',path);
}
}
}else{
console.log('sftp upload err',err);
}
}
});
})
.on('change', function(path) {
console.log('upload changed',path.replace(local_path,deploy_path));
sftp.fastPut(path,path.replace(local_path,deploy_path),{},function(err){
if(err){
if(err.toString().indexOf('such file') >= 0){
console.log('start mkdir');
var path_arr = path.replace(local_path,deploy_path).split('/');
var path_mini_arr = [];
for(i in path_arr){
path_mini_arr.push(path_arr[i]);
if(path_mini_arr.length != path_arr.length){
sftp.mkdir(path_mini_arr.join('/'),{},function(err){
if(err && err.toString().indexOf('ailure') == -1 && err.toString().indexOf('such file') == -1){
console.log('mkdir err',err);
}
});
}
if(path_mini_arr.length == path_arr.length){
watcher.emit('change',path);
}
}
}else{
console.log('sftp upload err',err);
}
}
});
})
.on('unlink', function(path) {
console.log('remove file',path.replace(local_path,deploy_path));
sftp.unlink(path.replace(local_path,deploy_path),function(err){
if(err){
console.log('sftp remove err',err);
}
});
})
.on('error', function(error) {console.error('Error happened', error);})
}else{
console.log('sftp err',err);
}
});
});
c.on('error', function(err) {
console.log('Connection :: error :: ' + err);
});
c.on('end', function() {
console.log('Connection :: end');
});
c.on('close', function(had_error) {
console.log('Connection :: close');
});
c.connect(ssh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment