Skip to content

Commit

Permalink
Merge pull request #3807 from medanat/minimize-async-lib-footprint
Browse files Browse the repository at this point in the history
Minimize async lib footprint
  • Loading branch information
wallet77 authored Aug 17, 2018
2 parents c231e28 + b8b48e8 commit 7e92855
Show file tree
Hide file tree
Showing 40 changed files with 106 additions and 154 deletions.
24 changes: 12 additions & 12 deletions bin/pm2
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ if (semver.satisfies(process.versions.node, '>= 6.0.0'))

process.env.PM2_USAGE = 'CLI';

var cst = require('../constants.js');
var cst = require('../constants.js');

var commander = require('commander');
var chalk = require('chalk');
var async = require('async');
var commander = require('commander');
var chalk = require('chalk');
var forEachLimit = require('async/forEachLimit');

var debug = require('debug')('pm2:cli');
var PM2 = require('../lib/API.js');
var pkg = require('../package.json');
var tabtab = require('../lib/completion.js');
var debug = require('debug')('pm2:cli');
var PM2 = require('../lib/API.js');
var pkg = require('../package.json');
var tabtab = require('../lib/completion.js');

// Early detection of silent to avoid printing motd
if (process.argv.indexOf('--silent') > -1 ||
Expand Down Expand Up @@ -295,7 +295,7 @@ commander.command('start [name|file|ecosystem|id...]')
if (cmd.length === 0) {
cmd = [cst.APP_CONF_DEFAULT_FILE];
}
async.forEachLimit(cmd, 1, function(script, next) {
forEachLimit(cmd, 1, function(script, next) {
pm2.start(script, commander, next);
}, function(err) {
pm2.speedList(err ? 1 : 0);
Expand Down Expand Up @@ -346,7 +346,7 @@ commander.command('stop <id|name|all|json|stdin...>')
.option('--watch', 'Stop watching folder for changes')
.description('stop a process')
.action(function(param) {
async.forEachLimit(param, 1, function(script, next) {
forEachLimit(param, 1, function(script, next) {
pm2.stop(script, next);
}, function(err) {
pm2.speedList(err ? 1 : 0);
Expand All @@ -362,7 +362,7 @@ commander.command('restart <id|name|all|json|stdin...>')
.action(function(param) {
// Commander.js patch
param = patchCommanderArg(param);
async.forEachLimit(param, 1, function(script, next) {
forEachLimit(param, 1, function(script, next) {
pm2.restart(script, commander, next);
}, function(err) {
pm2.speedList(err ? 1 : 0);
Expand Down Expand Up @@ -433,7 +433,7 @@ commander.command('delete <name|id|script|all|json|stdin...>')
pm2.delete(param, 'pipe');
});
} else
async.forEachLimit(name, 1, function(script, next) {
forEachLimit(name, 1, function(script, next) {
pm2.delete(script,'', next);
}, function(err) {
pm2.speedList(err ? 1 : 0);
Expand Down
17 changes: 9 additions & 8 deletions examples/sourcemap-auto-resolve/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
var commander = require('commander');
var fs = require('fs');
var path = require('path');
var async = require('async');
var eachLimit = require('async/eachLimit');
var series = require('async/series');
var debug = require('debug')('pm2:cli');
var util = require('util');
var chalk = require('chalk');
Expand Down Expand Up @@ -317,7 +318,7 @@ API.prototype.reset = function(process_name, cb) {
var that = this;

function processIds(ids, cb) {
async.eachLimit(ids, conf.CONCURRENT_ACTIONS, function(id, next) {
eachLimit(ids, conf.CONCURRENT_ACTIONS, function(id, next) {
that.Client.executeRemote('resetMetaProcessId', id, function(err, res) {
if (err) console.error(err);
Common.printOut(conf.PREFIX_MSG + 'Resetting meta for process id %d', id);
Expand Down Expand Up @@ -777,7 +778,7 @@ API.prototype._startScript = function(script, opts, cb) {
});
}

async.series([
series([
restartExistingProcessName,
restartExistingProcessId,
restartExistingProcessPath
Expand Down Expand Up @@ -916,7 +917,7 @@ API.prototype._startJson = function(file, opts, action, pipe, cb) {
* Auto detect application already started
* and act on them depending on action
*/
async.eachLimit(Object.keys(proc_list), conf.CONCURRENT_ACTIONS, function(proc_name, next) {
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();
Expand Down Expand Up @@ -981,7 +982,7 @@ API.prototype._startJson = function(file, opts, action, pipe, cb) {
}
});

async.eachLimit(apps_to_start, conf.CONCURRENT_ACTIONS, function(app, next) {
eachLimit(apps_to_start, conf.CONCURRENT_ACTIONS, function(app, next) {
if (opts.cwd)
app.cwd = opts.cwd;
if (opts.force_name)
Expand Down Expand Up @@ -1096,7 +1097,7 @@ API.prototype.actionFromJson = function(action, file, opts, jsonVia, cb) {
if ((appConf = Common.verifyConfs(appConf)) instanceof Error)
return cb ? cb(appConf) : that.exitCli(conf.ERROR_EXIT);

async.eachLimit(appConf, conf.CONCURRENT_ACTIONS, function(proc, next1) {
eachLimit(appConf, conf.CONCURRENT_ACTIONS, function(proc, next1) {
var name = '';
var new_env;

Expand All @@ -1120,7 +1121,7 @@ API.prototype.actionFromJson = function(action, file, opts, jsonVia, cb) {
}
if (!ids) return next1();

async.eachLimit(ids, conf.CONCURRENT_ACTIONS, function(id, next2) {
eachLimit(ids, conf.CONCURRENT_ACTIONS, function(id, next2) {
var opts = {};

//stopProcessId could accept options to?
Expand Down Expand Up @@ -1212,7 +1213,7 @@ API.prototype._operate = function(action_name, process_name, envs, cb) {
if (action_name == 'deleteProcessId')
concurrent_actions = 10;

async.eachLimit(ids, concurrent_actions, function(id, next) {
eachLimit(ids, concurrent_actions, function(id, next) {
var opts;

// These functions need extra param to be passed
Expand Down
17 changes: 9 additions & 8 deletions lib/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
const commander = require('commander');
const fs = require('fs');
const path = require('path');
const async = require('async');
const eachLimit = require('async/eachLimit');
const series = require('async/series');
const debug = require('debug')('pm2:cli');
const util = require('util');
const chalk = require('chalk');
Expand Down Expand Up @@ -336,7 +337,7 @@ class API {
var that = this;

function processIds(ids, cb) {
async.eachLimit(ids, conf.CONCURRENT_ACTIONS, function(id, next) {
eachLimit(ids, conf.CONCURRENT_ACTIONS, function(id, next) {
that.Client.executeRemote('resetMetaProcessId', id, function(err, res) {
if (err) console.error(err);
Common.printOut(conf.PREFIX_MSG + 'Resetting meta for process id %d', id);
Expand Down Expand Up @@ -801,7 +802,7 @@ class API {
});
}

async.series([
series([
restartExistingProcessName,
restartExistingProcessId,
restartExistingProcessPath
Expand Down Expand Up @@ -937,7 +938,7 @@ class API {
* Auto detect application already started
* and act on them depending on action
*/
async.eachLimit(Object.keys(proc_list), conf.CONCURRENT_ACTIONS, function(proc_name, next) {
eachLimit(Object.keys(proc_list), conf.CONCURRENT_ACTIONS, function(proc_name, next) {

// Skip app name (--only option)
if (apps_name.indexOf(proc_name) == -1)
Expand Down Expand Up @@ -1003,7 +1004,7 @@ class API {
}
});

async.eachLimit(apps_to_start, conf.CONCURRENT_ACTIONS, function(app, next) {
eachLimit(apps_to_start, conf.CONCURRENT_ACTIONS, function(app, next) {
if (opts.cwd)
app.cwd = opts.cwd;
if (opts.force_name)
Expand Down Expand Up @@ -1118,7 +1119,7 @@ class API {
if ((appConf = Common.verifyConfs(appConf)) instanceof Error)
return cb ? cb(appConf) : that.exitCli(conf.ERROR_EXIT);

async.eachLimit(appConf, conf.CONCURRENT_ACTIONS, function(proc, next1) {
eachLimit(appConf, conf.CONCURRENT_ACTIONS, function(proc, next1) {
var name = '';
var new_env;

Expand All @@ -1142,7 +1143,7 @@ class API {
}
if (!ids) return next1();

async.eachLimit(ids, conf.CONCURRENT_ACTIONS, function(id, next2) {
eachLimit(ids, conf.CONCURRENT_ACTIONS, function(id, next2) {
var opts = {};

//stopProcessId could accept options to?
Expand Down Expand Up @@ -1234,7 +1235,7 @@ class API {
if (action_name == 'deleteProcessId')
concurrent_actions = 10;

async.eachLimit(ids, concurrent_actions, function(id, next) {
eachLimit(ids, concurrent_actions, function(id, next) {
var opts;

// These functions need extra param to be passed
Expand Down
1 change: 0 additions & 1 deletion lib/API/CliUx.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var p = require('path');
var chalk = require('chalk');
var Common = require('../Common');
var Spinner = require('./Spinner.js');
var os = require('os');
var UX = module.exports = {};

/**
Expand Down
3 changes: 0 additions & 3 deletions lib/API/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ var Common = require('../Common.js');
var cst = require('../../constants.js');
var UX = require('./CliUx');
var chalk = require('chalk');
var async = require('async');
var Configuration = require('../Configuration.js');
//@todo double check that imported methods works
var InteractorDaemonizer = require('@pm2/agent/src/InteractorClient');

module.exports = function(CLI) {

Expand Down
1 change: 0 additions & 1 deletion lib/API/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
var os = require('os');
var p = require('path');
var blessed = require('blessed');
var fs = require('fs');
var debug = require('debug')('pm2:monit');
var sprintf = require('sprintf-js').sprintf;

Expand Down
1 change: 0 additions & 1 deletion lib/API/Extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var cst = require('../../constants.js');
var Common = require('../Common.js');
var UX = require('./CliUx');
var chalk = require('chalk');
var async = require('async');
var path = require('path');
var fs = require('fs');
var fmt = require('../tools/fmt.js');
Expand Down
4 changes: 2 additions & 2 deletions lib/API/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var fs = require('fs'),
util = require('util'),
chalk = require('chalk'),
async = require('async'),
forEachLimit = require('async/forEachLimit'),
moment = require('moment');

var Log = module.exports = {};
Expand Down Expand Up @@ -48,7 +48,7 @@ Log.tail = function(apps_list, lines, raw, callback) {
(fs.existsSync(b.path) ? fs.statSync(b.path).mtime.valueOf() : 0);
});

async.forEachLimit(apps_list, 1, function(app, next) {
forEachLimit(apps_list, 1, function(app, next) {
if (!fs.existsSync(app.path || ''))
return next();

Expand Down
10 changes: 6 additions & 4 deletions lib/API/Modules/Modularizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
var path = require('path');
var fs = require('fs');
var os = require('os');
var async = require('async');
var parallel = require('async/parallel');
var eachLimit = require('async/eachLimit');
var forEachLimit = require('async/forEachLimit');
var p = path;
var readline = require('readline');
var spawn = require('child_process').spawn;
Expand Down Expand Up @@ -126,7 +128,7 @@ Modularizer.installMultipleInternalModules = function (modules, cb) {
})(i));
}

async.parallel(functionList, function (err, results) {
parallel(functionList, function (err, results) {
for (var i = 0; i < results.length; i++) {
var display = results[i].module.message || results[i].module.name;
if (results[i].err) {
Expand Down Expand Up @@ -267,7 +269,7 @@ Modularizer.launchModules = function(CLI, cb) {

if (!modules) return cb();

async.eachLimit(Object.keys(modules), 1, function(module_name, next) {
eachLimit(Object.keys(modules), 1, function(module_name, next) {
Common.printOut(cst.PREFIX_MSG_MOD + 'Starting module ' + module_name);

var install_path = path.join(cst.DEFAULT_MODULE_PATH, module_name);
Expand Down Expand Up @@ -366,7 +368,7 @@ Modularizer.uninstall = function(CLI, module_name, cb) {

if (!modules) return cb();

return async.forEachLimit(Object.keys(modules), 1, function(module_name, next) {
return forEachLimit(Object.keys(modules), 1, function(module_name, next) {
uninstallModule(CLI, {
module_name : module_name,
deep_uninstall : true
Expand Down
4 changes: 2 additions & 2 deletions lib/API/Modules/Modularizerv1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
var path = require('path');
var fs = require('fs');
var async = require('async');
var eachLimit = require('async/eachLimit');
var p = path;
var spawn = require('child_process').spawn;
var chalk = require('chalk');
Expand Down Expand Up @@ -72,7 +72,7 @@ Modularizer.launchModules = function(CLI, cb) {

if (!modules) return cb();

async.eachLimit(Object.keys(modules), 1, function(module, next) {
eachLimit(Object.keys(modules), 1, function(module, next) {
var pmod = p.join(module_folder, module, cst.DEFAULT_MODULE_JSON);

Common.printOut(cst.PREFIX_MSG_MOD + 'Starting module ' + module);
Expand Down
20 changes: 6 additions & 14 deletions lib/API/Modules/Modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@
*
**************************/

var cst = require('../../../constants.js');
var Common = require('../../Common.js');
var UX = require('../CliUx');
var chalk = require('chalk');
var async = require('async');

var path = require('path');
var fs = require('fs');
var p = path;
var Configuration = require('../../Configuration.js');
var Utility = require('../../Utility.js');

var MODULE_CONF_PREFIX = 'module-db';
var cst = require('../../../constants.js');
var Common = require('../../Common.js');
var UX = require('../CliUx');
var chalk = require('chalk');
var forEachLimit = require('async/forEachLimit');

var Modularizer = require('./Modularizer.js');
var ModularizerV1 = require('./Modularizerv1.js');
Expand Down Expand Up @@ -154,7 +146,7 @@ module.exports = function(CLI) {
var that = this;

this.Client.getAllModulesId(function(err, modules_id) {
async.forEachLimit(modules_id, 1, function(id, next) {
forEachLimit(modules_id, 1, function(id, next) {
that._operate('deleteProcessId', id, next);
}, function() {
return cb ? cb() : false;
Expand Down
21 changes: 10 additions & 11 deletions lib/API/Startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
var debug = require('debug')('pm2:cli:startup');
var chalk = require('chalk');
var path = require('path');
var fs = require('fs');
var async = require('async');
var exec = require('child_process').exec;
var Common = require('../Common.js');
var cst = require('../../constants.js');
var spawn = require('child_process').spawn;
var chalk = require('chalk');
var path = require('path');
var fs = require('fs');
var forEachLimit = require('async/forEachLimit');
var eachLimit = require('async/eachLimit');
var exec = require('child_process').exec;
var Common = require('../Common.js');
var cst = require('../../constants.js');

module.exports = function(CLI) {
/**
Expand Down Expand Up @@ -333,7 +332,7 @@ module.exports = function(CLI) {

Common.printOut(cst.PREFIX_MSG + 'Making script booting at startup...');

async.forEachLimit(commands, 1, function(command, next) {
forEachLimit(commands, 1, function(command, next) {
Common.printOut(cst.PREFIX_MSG + '[-] Executing: %s...', chalk.bold(command));
require('shelljs').exec(command, function(code, stdout, stderr) {
if (code === 0) {
Expand Down Expand Up @@ -553,7 +552,7 @@ module.exports = function(CLI) {
return Object.keys(current).indexOf(i) < 0;
})

async.eachLimit(processes, cst.CONCURRENT_ACTIONS, function(app, next) {
eachLimit(processes, cst.CONCURRENT_ACTIONS, function(app, next) {
if (tostart.indexOf(app.name) == -1)
return next();
that.Client.executeRemote('prepare', app, function(err, dt) {
Expand Down
Loading

0 comments on commit 7e92855

Please sign in to comment.