Skip to content

Commit

Permalink
Merge pull request #3723 from livankrekh/development
Browse files Browse the repository at this point in the history
Add directory support for --ignore-watch, fixed --ignore-watch, implemented test and implemented flag --watch-delay
  • Loading branch information
wallet77 authored Aug 17, 2018
2 parents 4217b15 + 83294af commit 98f49dc
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 11 deletions.
1 change: 1 addition & 0 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ commander.version(pkg.version)
.option('--merge-logs', 'merge logs from different instances but keep error and out separated')
.option('--watch [paths]', 'watch application folder for changes', function(v, m) { m.push(v); return m;}, [])
.option('--ignore-watch <folders|files>', 'List of paths to ignore (name or regex)')
.option('--watch-delay <delay>', 'specify a restart delay after changing files (--watch-delay 4 (in sec) or 4000ms)')
.option('--no-color', 'skip colors')
.option('--no-vizion', 'start an app without vizion feature (versioning control)')
.option('--no-autorestart', 'start an app without automatic restart')
Expand Down
22 changes: 21 additions & 1 deletion lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var Modularizer = require('./API/Modules/Modularizer.js');
var path_structure = require('../paths.js');
var UX = require('./API/CliUx');
var pkg = require('../package.json');
var hf = require('./API/Modules/flagExt.js')
var flagWatch = require("./API/Modules/flagWatch.js");
var hf = require('./API/Modules/flagExt.js');

var IMMUTABLE_MSG = chalk.bold.blue('Use --update-env to update environment variables');

Expand Down Expand Up @@ -648,6 +649,8 @@ class API {
var app_conf = Config.transCMDToConf(opts);
var appConf = {};

var ignoreFileArray = [];

if (!!opts.executeCommand)
app_conf.exec_mode = 'fork';
else if (opts.instances !== undefined)
Expand All @@ -674,11 +677,28 @@ class API {

app_conf = appConf[0];


if (opts.ignoreWatch) {
flagWatch.handleFolders(opts.ignoreWatch, ignoreFileArray);
if (app_conf.ignore_watch) {
app_conf.ignore_watch = ignoreFileArray;
}
}

if (opts.watchDelay) {
if (typeof opts.watchDelay === "string" && opts.watchDelay.indexOf("ms") !== -1)
app_conf.watch_delay = parseInt(opts.watchDelay);
else {
app_conf.watch_delay = parseFloat(opts.watchDelay) * 1000;
}
}

var mas = [];
if(typeof opts.ext != 'undefined')
hf.make_available_extension(opts, mas); // for -e flag
mas.length > 0 ? app_conf.ignore_watch = mas : 0;


/**
* If -w option, write configuration to configuration.json file
*/
Expand Down
29 changes: 29 additions & 0 deletions lib/API/Modules/flagWatch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var fs = require('fs');

function handleFolders(folder, mas) {
if (!folder || !mas || folder.indexOf("node_modules") !== -1)
return ;

try {
fs.accessSync(folder, fs.constants.R_OK);
} catch (err) {
return ;
}

if (fs.statSync(folder) && fs.statSync(folder).isDirectory()) {
fs.readdirSync(folder).forEach(file => {
if (fs.statSync(folder)["mode"] & 4 === 0)
return ;
if (fs.existsSync(folder + file + '/'))
handleFolders(folder + file + '/', mas);
else
mas.push(folder + file);
});
} else {
if (fs.statSync(folder).isFile()) {
mas.push(folder);
}
}
}

module.exports.handleFolders = handleFolders;
22 changes: 12 additions & 10 deletions lib/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ module.exports = function ClusterMode(God) {

console.error('Change detected on path %s for app %s - restarting', path, pm2_env.name);

God.restartProcessName(pm2_env.name, function(err, list) {
self.restarting = false;

if (err) {
log('Error while restarting', err);
return false;
}

return log('Process restarted');
});
setTimeout(function() {
God.restartProcessName(pm2_env.name, function(err, list) {
self.restarting = false;

if (err) {
log('Error while restarting', err);
return false;
}

return log('Process restarted');
});
}, (pm2_env.watch_delay || 0));

return false;
});
Expand Down
55 changes: 55 additions & 0 deletions test/programmatic/flagWatch.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

var should = require('should');
var f_w = require('../../lib/API/Modules/flagWatch.js');
var fs = require('fs');

describe('Flag --ignore-watch', function() {

it('should return not empty result', function() {
var res = [];
f_w.handleFolders('./', res);
should(res).be.not.empty();
});
it('should not crash', function() {
var res = []
f_w.handleFolders();
f_w.handleFolders(res);
f_w.handleFolders('');
f_w.handleFolders('lsdldmcsdf/amfkdmfk');
});
it('should give different results', function() {
var tmp_res = [];
var res = [];
f_w.handleFolders('./lib', res);
f_w.handleFolders('./examples', tmp_res);
should(res).not.equal(tmp_res);
});
it('should not crash in case, when no access for file or directory by permissions', function() {
var fileStream;

if (!fs.existsSync("noAccessDir"))
fs.mkdirSync("noAccessDir", 0777);
if (!fs.existsSync("noAccessDir/checkPermissions.txt")) {
fileStream = fs.createWriteStream("noAccessDir/checkPermissions.txt");
fileStream.write("It's a temporary file for testing flag --ignore-watch in PM2");
fileStream.end();
}
fs.chmodSync('noAccessDir/checkPermissions.txt', 0000);
fs.chmodSync('noAccessDir', 0000);

after(function () {
fs.chmodSync('noAccessDir', 0777);
fs.chmodSync('noAccessDir/checkPermissions.txt', 0777);
fs.unlinkSync('noAccessDir/checkPermissions.txt');
fs.rmdirSync('noAccessDir/');
});

f_w.handleFolders('noAccessDir/', []);
f_w.handleFolders('noAccessDir/checkPermissions.txt', []);
});
it('should ignore node_modules folder', function() {
var res = [];
f_w.handleFolders('./node_modules', res);
should(res).be.empty();
});
});
10 changes: 10 additions & 0 deletions test/programmatic/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ describe('Watcher', function() {
})
})

it('should work with watch_delay', function(cb) {
testPM2Env('server-watch:online')({watch: true, watch_delay: 4000}, cb);
pm2.start(extend(json, {watch: true, watch_delay: 4000}), errShouldBeNull);
})

it('should not crash with watch_delay without watch', function(cb) {
testPM2Env('server-watch:online')({watch_delay: 4000}, cb);
pm2.start(extend(json, {watch_delay: 4000}), errShouldBeNull);
})

/**
* Test #1668
*/
Expand Down

0 comments on commit 98f49dc

Please sign in to comment.