Skip to content

Commit

Permalink
Merge pull request #3823 from imarakho/flag--ext
Browse files Browse the repository at this point in the history
added --ext flag
  • Loading branch information
wallet77 authored Aug 17, 2018
2 parents bbcc85a + 79ab924 commit cc68dc1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var pm2 = new PM2();
commander.version(pkg.version)
.option('-v --version', 'print pm2 version')
.option('-s --silent', 'hide all messages', false)
.option('--ext <extensions>', 'watch only this file extensions')
.option('-n --name <name>', 'set a name for the process in the process list')
.option('-m --mini-list', 'display a compacted list without formatting')
.option('--interpreter <interpreter>', 'set a specific interpreter to use for executing app, default: node')
Expand Down
8 changes: 8 additions & 0 deletions lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const util = require('util');
const chalk = require('chalk');
const fclone = require('fclone');


var conf = require('../constants.js');
var Client = require('./Client');
var Common = require('./Common');
Expand All @@ -24,6 +25,7 @@ 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 IMMUTABLE_MSG = chalk.bold.blue('Use --update-env to update environment variables');

Expand Down Expand Up @@ -672,6 +674,11 @@ class API {

app_conf = appConf[0];

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 Expand Up @@ -938,6 +945,7 @@ class API {
* Auto detect application already started
* and act on them depending on action
*/

eachLimit(Object.keys(proc_list), conf.CONCURRENT_ACTIONS, function(proc_name, next) {

// Skip app name (--only option)
Expand Down
46 changes: 46 additions & 0 deletions lib/API/Modules/flagExt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var fs = require('fs');
var conf = require('../../../constants.js');

function find_extensions(folder, ext, ret)
{
try {
fs.accessSync(folder, fs.constants.R_OK);
} catch (err) {
return;
}
if(fs.statSync(folder).isDirectory() && folder.indexOf('node_modules') == -1 && (fs.statSync(folder)["mode"] & 4))
{
fs.readdirSync(folder).forEach(file => {
var tmp;
if(Number.parseInt(folder.lastIndexOf('/') + 1) === folder.length)
tmp = folder + file;
else
tmp = folder + '/' + file;
if(fs.statSync(tmp).isDirectory())
find_extensions(tmp, ext, ret);
else
{
var p = true;
for(var i = 0; i < ext.length;i++)
if(ext[i].test(file))
p = false;
if(p)
ret.push(folder + '/' + file);
}
});
}
}

module.exports.make_available_extension = function make_available_extension(opts, ret)
{
if(typeof opts == 'object' && typeof ret == 'object')
{
var mas = opts.ext.split(',');
for(var i = 0;i < mas.length;i++)
mas[i] = '.' + mas[i];
var res = [];
for(var i = 0;i < mas.length;i++)
res[i] = new RegExp(mas[i] + '$');
find_extensions(process.cwd(), res, ret);
}
}
43 changes: 43 additions & 0 deletions test/programmatic/flagExt.mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

var should = require('should');
var f_e = require('../../lib/API/Modules/flagExt.js');
var fs = require('fs');

describe('Flag -ext', function() {

var opts = {};
var res = [];
opts.ext = 'js,json';

it('should return not empty result', function() {
f_e.make_available_extension(opts, res);
should(res).be.not.empty();
});
it('should not crash', function() {
f_e.make_available_extension();
f_e.make_available_extension(res);
f_e.make_available_extension(opts);
});
it('should give different results', function() {
var tmp_res = [];
f_e.make_available_extension(opts, res);
opts.ext = 'sh,py';
f_e.make_available_extension(opts, 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 dir = fs.mkdirSync("noAccessDir", 0777);
opts.ext = 'txt'
var fileStream = fs.createWriteStream("noAccessDir/checkPermissions.txt");
fileStream.write("It's a temporary file for testing flag --ext in PM2");
fileStream.end();
fs.chmodSync('noAccessDir/checkPermissions.txt', 0000);
fs.chmodSync('noAccessDir', 0000);
f_e.make_available_extension(opts, []);
f_e.make_available_extension(opts, []);
fs.chmodSync('noAccessDir', 0777);
fs.chmodSync('noAccessDir/checkPermissions.txt', 0777);
fs.unlinkSync('noAccessDir/checkPermissions.txt');
fs.rmdirSync('noAccessDir/');
});
});

0 comments on commit cc68dc1

Please sign in to comment.