From 8e491bfdc155c7c8e5fdea2bf574b786b68ff90a Mon Sep 17 00:00:00 2001 From: Igor Marakhov Date: Tue, 24 Jul 2018 16:04:22 +0300 Subject: [PATCH] added --ext flag --- bin/pm2 | 1 + lib/API.js | 8 ++++++ lib/API/Modules/flagExt.js | 46 ++++++++++++++++++++++++++++++ test/programmatic/flagExt.mocha.js | 43 ++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 lib/API/Modules/flagExt.js create mode 100644 test/programmatic/flagExt.mocha.js diff --git a/bin/pm2 b/bin/pm2 index 002657e7e..c81ad5aef 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -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 ', 'watch only this file extensions') .option('-n --name ', 'set a name for the process in the process list') .option('-m --mini-list', 'display a compacted list without formatting') .option('--interpreter ', 'set a specific interpreter to use for executing app, default: node') diff --git a/lib/API.js b/lib/API.js index 8424ef79a..14b8594f1 100644 --- a/lib/API.js +++ b/lib/API.js @@ -14,6 +14,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'); @@ -23,6 +24,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'); @@ -649,6 +651,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 */ @@ -911,6 +918,7 @@ class API { * and act on them depending on action */ async.eachLimit(Object.keys(proc_list), conf.CONCURRENT_ACTIONS, function(proc_name, next) { + // Skip app name (--only option) if (apps_name.indexOf(proc_name) == -1) return next(); diff --git a/lib/API/Modules/flagExt.js b/lib/API/Modules/flagExt.js new file mode 100644 index 000000000..d7d8e0f98 --- /dev/null +++ b/lib/API/Modules/flagExt.js @@ -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); + } +} diff --git a/test/programmatic/flagExt.mocha.js b/test/programmatic/flagExt.mocha.js new file mode 100644 index 000000000..11f1b1d30 --- /dev/null +++ b/test/programmatic/flagExt.mocha.js @@ -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/'); + }); +});