📜 ⬆️ ⬇️

Phpstorm Life hacking: keep-alive automatic upload on the knee

Many have encountered the problem of Automatic Upload to PhpStorm (here you can enter another IDE with a similar problem) when working on a remote server. Calling it a “problem” is also a stretch, but the topic is still alive : PhpStorm, when uploading modified files, brings up a new connection each time, which takes quite a lot of time (I’m for sure: the hosting provider Hetzner), so you have to wait a few seconds before changing zalyutsya.

Under the cut, I'll tell you how on my knee I sketched a keep-alive connection script with an Automatic Upload project.


')
The text will be a lot of code and few comments. This is due to the fact that there is nothing to comment on: for newbies, guides and articles will help to understand NodeJS (but in this case, in any case, JavaScript), and for experienced users, they will say more about code-review than me.

The script itself is written in NodeJS - it is fairly simple and always at hand, so the choice was obvious. Connection I am using SFTP. So what we need:



I decided not to delve into the ideal for this library - I did not want to spend a lot of time on this task. A quick look at the Internet brought me to 2 simple modules:



I will not describe how to install them and how NodeJS works - there are many guides on the same habr.

I called the file plainly deploy.js . The first lines look simple:

var fs = require('fs'); var Connection = require('ssh2'); var hokidar = require('chokidar'); var c = new Connection(); 


So, the libraries hooked up. First you need to connect to the server:

 c.connect({ host: 'habrahabr.ru', port: 1500, username: 'habr', password: 'habrapassword' }); 


Like most modules, ssh2 has events. Print them to the log:

 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'); }); 


And of course with what we will work:

 c.on('ready', function() { .... }) 


There is a rather useful feature in this module - sftp : a fairly convenient and simple manager for working with files:

 c.sftp(function(err,sftp){ if(!err){ console.log('sftp start'); }else{ console.log('sftp err',err); } }); 


When the connection is raised - it's time to implement file monitoring:

  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 -        }); 


Yesterday there are events add , change , unlink , which work out when manipulating files. The case remains small: to force the ssh2 module to duplicate the changes of the project by sftp :

  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);}) 


Done! Files are poured faster than we are in the browser! As for the load, the script eats up the CPU like skype (MacBook Air mid 2012), so it's quite tolerable.

In order not to duplicate each project on the deplorer, I sketched a lightweight Bash script that I throw at the root of the project, it already runs our script.

 #!/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 


Well, in this case, of course, you need to get the parameters in deploy.js

 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; } } }); 


The script is simple and suitable for many other tasks. For example, you can restart the NodeJS application on a remote server after uploading a file, or duplicate pictures from the developer’s environment on a common dev server.

I attach the script file .

The script was written in about an hour, so it can be nonsense and trivial errors, so please do not judge strictly. The point of the post is not to provide an ideally working code, but to think about a possible solution. I spent hours on the Internet looking for plug-ins or hacks, but found nothing. The idea came by itself, with the release of the new PhpStorm, when I saw the console support in the IDE.

Thank you for your attention and constructive criticism!

Source: https://habr.com/ru/post/202728/


All Articles