var fs = require('fs'); var Connection = require('ssh2'); var hokidar = require('chokidar'); var c = new Connection();
c.connect({ host: 'habrahabr.ru', port: 1500, username: 'habr', password: 'habrapassword' });
c.on('connect', function() { console.log('Connection :: connect'); }); 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.on('ready', function() { .... })
c.sftp(function(err,sftp){ if(!err){ console.log('sftp start'); }else{ console.log('sftp err',err); } });
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){ 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){ console.log('sftp change 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);})
#!/bin/bash DEPLOY_PATH="/deploy/path" DEPLOYER_PATH="/path/to/deploy.js" SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ]; do TARGET="$(readlink "$SOURCE")" if [[ $SOURCE == /* ]]; then echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'" SOURCE="$TARGET" else DIR="$( dirname "$SOURCE" )" echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')" SOURCE="$DIR/$TARGET" fi done RDIR="$( dirname "$SOURCE" )" DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" if [ "$DIR" != "$RDIR" ]; then echo "local_path is '$DIR'" fi node $DEPLOYER_PATH local_path=$DIR deploy_path=$DEPLOY_PATH
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; } } });
Source: https://habr.com/ru/post/202728/
All Articles