Skip to content

Commit ee5e6a0

Browse files
committed
refactor: faster cli with less require
1 parent 4576b4c commit ee5e6a0

13 files changed

+39
-54
lines changed

constants.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var debug = require('debug')('pm2:conf');
88
var p = require('path');
99
var util = require('util');
1010
var chalk = require('chalk');
11-
var semver = require('semver');
1211

1312
/**
1413
* Get PM2 path structure
@@ -79,10 +78,7 @@ var csts = {
7978

8079
// Concurrent actions when doing start/restart/reload
8180
CONCURRENT_ACTIONS : (function() {
82-
var default_concurrent_actions = 1;
83-
if (semver.satisfies(process.versions.node, '>= 4.0.0'))
84-
default_concurrent_actions = 2;
85-
var concurrent_actions = parseInt(process.env.PM2_CONCURRENT_ACTIONS) || default_concurrent_actions;
81+
var concurrent_actions = parseInt(process.env.PM2_CONCURRENT_ACTIONS) || 2;
8682
debug('Using %d parallelism (CONCURRENT_ACTIONS)', concurrent_actions);
8783
return concurrent_actions;
8884
})(),

lib/API/Containerizer.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var exec = require('child_process').exec;
44
var chalk = require('chalk');
55
var util = require('util');
66
var fmt = require('../tools/fmt.js');
7-
var vizion = require('vizion');
87
var fs = require('fs');
98
var path = require('path');
109
var cst = require('../../constants.js');
@@ -148,7 +147,7 @@ function handleExit(CLI, opts, mode) {
148147
if (err) {
149148
console.error(err);
150149
}
151-
vizion.analyze({folder : process.cwd()}, function recur_path(err, meta){
150+
require('vizion').analyze({folder : process.cwd()}, function recur_path(err, meta){
152151
if (!err && meta.revision) {
153152
var commit_id = util.format('#%s(%s) %s',
154153
meta.branch,

lib/API/Deploy.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66

77
var fs = require('fs');
8-
var Deploy = require('pm2-deploy');
9-
108
var cst = require('../../constants.js');
119
var Utility = require('../Utility.js');
1210
var Common = require('../Common.js');
@@ -104,7 +102,7 @@ module.exports = function(CLI) {
104102
json_conf.deploy[env]['post-deploy'] = 'pm2 startOrRestart ' + file + ' --env ' + env;
105103
}
106104

107-
Deploy.deployForEnv(json_conf.deploy, env, args, function(err, data) {
105+
require('pm2-deploy').deployForEnv(json_conf.deploy, env, args, function(err, data) {
108106
if (err) {
109107
Common.printError('Deploy failed');
110108
return cb ? cb(err) : that.exitCli(cst.ERROR_EXIT);

lib/API/Keymetrics/cli-api.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var async = require('async');
66
var path = require('path');
77
var fs = require('fs');
88
var KMDaemon = require('@pm2/agent/src/InteractorClient');
9-
var KM = require('./kmapi.js');
109
var Table = require('cli-table-redemption');
1110
var open = require('../../tools/open.js');
1211
var promptly = require('promptly');
@@ -144,6 +143,7 @@ module.exports = function(CLI) {
144143
* Open Browser
145144
*/
146145
function loginPrompt(cb) {
146+
var KM = require('./kmapi.js');
147147
console.log(chalk.bold('Log in to Keymetrics'));
148148
(function retry() {
149149
promptly.prompt('Username or Email: ', function(err, username) {
@@ -217,6 +217,8 @@ module.exports = function(CLI) {
217217
* Open Browser for access to monitoring dashboard
218218
*/
219219
function registerPrompt() {
220+
var KM = require('./kmapi.js');
221+
220222
console.log(chalk.bold('Now registering to Keymetrics'));
221223
promptly.prompt('Username: ', {
222224
validator : validateUsername,

lib/API/Keymetrics/kmapi.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var querystring = require('querystring');
2-
var https = require('https');
1+
32
var fs = require('fs');
43
var needle = require('needle');
54
var url = require('url');
@@ -19,6 +18,8 @@ var KM = function() {
1918
* @return promise
2019
*/
2120
KM.prototype.loginAndGetAccessToken = function (user_info, cb) {
21+
var querystring = require('querystring');
22+
2223
var that = this;
2324
var URL_AUTH = '/api/oauth/authorize?response_type=token&scope=all&client_id=' +
2425
that.CLIENT_ID + '&redirect_uri=' + that.CB_URI;

lib/API/Modules/Modularizer.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Use of this source code is governed by a license that
44
* can be found in the LICENSE file.
55
*/
6-
var shelljs = require('shelljs');
76
var path = require('path');
87
var fs = require('fs');
98
var os = require('os');
@@ -18,7 +17,6 @@ var Common = require('../../Common');
1817
var Utility = require('../../Utility.js');
1918
var ModularizerV1 = require('./Modularizerv1.js');
2019
var Modularizer = module.exports = {};
21-
var mkdirp = require('mkdirp');
2220

2321
var MODULE_CONF_PREFIX = 'module-db-v2';
2422

@@ -185,7 +183,7 @@ Modularizer.installModule = function(CLI, module_name, opts, cb) {
185183
var canonic_module_name = Utility.getCanonicModuleName(module_name);
186184
var install_path = path.join(cst.DEFAULT_MODULE_PATH, canonic_module_name);
187185

188-
mkdirp(install_path, function() {
186+
require('mkdirp')(install_path, function() {
189187
process.chdir(os.homedir());
190188

191189
var install_instance = spawn(cst.IS_WINDOWS ? 'npm.cmd' : 'npm', ['install', module_name, '--loglevel=error', '--prefix', install_path ], {
@@ -405,14 +403,14 @@ function uninstallModule(CLI, opts, cb) {
405403

406404
if (module_name != '.') {
407405
console.log(proc_path);
408-
shelljs.rm('-r', proc_path);
406+
require('shelljs').rm('-r', proc_path);
409407
}
410408

411409
return cb(err);
412410
}
413411

414412
if (module_name != '.') {
415-
shelljs.rm('-r', proc_path);
413+
require('shelljs').rm('-r', proc_path);
416414
}
417415

418416
return cb(null, data);
@@ -436,9 +434,9 @@ var Rollback = {
436434

437435
CLI.deleteModule(canonic_module_name, function() {
438436
// Delete failing module
439-
shelljs.rm('-r', module_path);
437+
require('shelljs').rm('-r', module_path);
440438
// Restore working version
441-
shelljs.cp('-r', backup_path, module_path);
439+
require('shelljs').cp('-r', backup_path, module_path);
442440

443441
var proc_path = path.join(module_path, 'node_modules', canonic_module_name);
444442
var package_json_path = path.join(proc_path, 'package.json');
@@ -456,7 +454,7 @@ var Rollback = {
456454
var tmpdir = require('os').tmpdir();
457455
var canonic_module_name = Utility.getCanonicModuleName(module_name);
458456
var module_path = path.join(cst.DEFAULT_MODULE_PATH, canonic_module_name);
459-
shelljs.cp('-r', module_path, tmpdir);
457+
require('shelljs').cp('-r', module_path, tmpdir);
460458
}
461459
}
462460

@@ -521,13 +519,13 @@ Modularizer.publish = function(cb) {
521519
package_json.name,
522520
package_json.version);
523521

524-
shelljs.exec('npm publish', function(code) {
522+
require('shelljs').exec('npm publish', function(code) {
525523
Common.printOut(cst.PREFIX_MSG_MOD + 'Module - %s@%s successfully published',
526524
package_json.name,
527525
package_json.version);
528526

529527
Common.printOut(cst.PREFIX_MSG_MOD + 'Pushing module on Git');
530-
shelljs.exec('git add . ; git commit -m "' + package_json.version + '"; git push origin master', function(code) {
528+
require('shelljs').exec('git add . ; git commit -m "' + package_json.version + '"; git push origin master', function(code) {
531529

532530
Common.printOut(cst.PREFIX_MSG_MOD + 'Installable with pm2 install %s', package_json.name);
533531
return cb(null, package_json);
@@ -550,11 +548,11 @@ Modularizer.generateSample = function(app_name, cb) {
550548
var cmd3 = 'cd ' + module_name + ' ; npm install';
551549

552550
Common.printOut(cst.PREFIX_MSG_MOD + 'Getting sample app');
553-
shelljs.exec(cmd1, function(err) {
551+
require('shelljs').exec(cmd1, function(err) {
554552
if (err) Common.printError(cst.PREFIX_MSG_MOD_ERR + err.message);
555-
shelljs.exec(cmd2, function(err) {
553+
require('shelljs').exec(cmd2, function(err) {
556554
console.log('');
557-
shelljs.exec(cmd3, function(err) {
555+
require('shelljs').exec(cmd3, function(err) {
558556
console.log('');
559557
Common.printOut(cst.PREFIX_MSG_MOD + 'Module sample created in folder: ', path.join(process.cwd(), module_name));
560558
console.log('');

lib/API/Modules/Modularizerv1.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Use of this source code is governed by a license that
44
* can be found in the LICENSE file.
55
*/
6-
var shelljs = require('shelljs');
76
var path = require('path');
87
var fs = require('fs');
98
var async = require('async');

lib/API/Modules/Modules.js

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var UX = require('../CliUx');
1111
var chalk = require('chalk');
1212
var async = require('async');
1313

14-
var shelljs = require('shelljs');
1514
var path = require('path');
1615
var fs = require('fs');
1716
var p = path;

lib/API/Startup.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ var exec = require('child_process').exec;
1212
var Common = require('../Common.js');
1313
var cst = require('../../constants.js');
1414
var spawn = require('child_process').spawn;
15-
var shelljs = require('shelljs');
1615

1716
module.exports = function(CLI) {
1817
/**
@@ -47,7 +46,7 @@ module.exports = function(CLI) {
4746
var init_systems = Object.keys(hash_map);
4847

4948
for (var i = 0; i < init_systems.length; i++) {
50-
if (shelljs.which(init_systems[i]) != null) {
49+
if (require('shelljs').which(init_systems[i]) != null) {
5150
break;
5251
}
5352
}
@@ -150,7 +149,7 @@ module.exports = function(CLI) {
150149
];
151150
};
152151

153-
shelljs.exec(commands.join('&& '), function(code, stdout, stderr) {
152+
require('shelljs').exec(commands.join('&& '), function(code, stdout, stderr) {
154153
Common.printOut(stdout);
155154
Common.printOut(stderr);
156155
if (code == 0) {
@@ -317,7 +316,7 @@ module.exports = function(CLI) {
317316

318317
async.forEachLimit(commands, 1, function(command, next) {
319318
Common.printOut(chalk.bold('>>> Executing %s'), command);
320-
shelljs.exec(command, function(code, stdout, stderr) {
319+
require('shelljs').exec(command, function(code, stdout, stderr) {
321320
if (code === 0) {
322321
Common.printOut(chalk.bold('[DONE] '));
323322
return next();

lib/API/Version.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var cst = require('../../constants.js');
33
var Common = require('../Common.js');
44
var fs = require('fs');
55
var async = require('async');
6-
var vizion = require('vizion');
76
var child = require('child_process');
87

98
var printError = Common.printError;
@@ -33,7 +32,7 @@ module.exports = function(CLI) {
3332
printOut(cst.PREFIX_MSG + 'No versioning system found for process %s', process_name);
3433
return cb ? cb({success:false, msg: 'No versioning system found for process'}) : that.exitCli(cst.SUCCESS_EXIT);
3534
}
36-
vizion.update({
35+
require('vizion').update({
3736
folder: proc.pm2_env.versioning.repo_path
3837
}, function(err, meta) {
3938
if (err !== null) {
@@ -91,10 +90,10 @@ module.exports = function(CLI) {
9190

9291
var proc = processes[0];
9392
if (proc.pm2_env.versioning) {
94-
vizion.isUpToDate({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) {
93+
require('vizion').isUpToDate({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) {
9594
if (err !== null)
9695
return cb ? cb({msg:err}) : that.exitCli(cst.ERROR_EXIT);
97-
vizion.revertTo(
96+
require('vizion').revertTo(
9897
{revision: commit_id,
9998
folder: proc.pm2_env.versioning.repo_path},
10099
function(err2, meta2) {
@@ -153,7 +152,7 @@ module.exports = function(CLI) {
153152
proc.pm2_env.versioning === null)
154153
return cb({msg : 'Versioning unknown'});
155154

156-
vizion.prev({
155+
require('vizion').prev({
157156
folder: proc.pm2_env.versioning.repo_path
158157
}, function(err, meta) {
159158
if (err)
@@ -167,7 +166,7 @@ module.exports = function(CLI) {
167166
getPostUpdateCmds(proc.pm2_env.versioning.repo_path, process_name, function (command_list) {
168167
execCommands(proc.pm2_env.versioning.repo_path, command_list, function(err, res) {
169168
if (err !== null) {
170-
vizion.next({folder: proc.pm2_env.versioning.repo_path}, function(err2, meta2) {
169+
require('vizion').next({folder: proc.pm2_env.versioning.repo_path}, function(err2, meta2) {
171170
printError(err);
172171
return cb ? cb({msg: meta.output + err}) : that.exitCli(cst.ERROR_EXIT);
173172
});
@@ -207,15 +206,15 @@ module.exports = function(CLI) {
207206
// in case user searched by id/pid
208207
process_name = proc.name;
209208
if (proc.pm2_env.versioning) {
210-
vizion.next({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) {
209+
require('vizion').next({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) {
211210
if (err !== null)
212211
return cb ? cb({msg:err}) : that.exitCli(cst.ERROR_EXIT);
213212
if (meta.success === true) {
214213
getPostUpdateCmds(proc.pm2_env.versioning.repo_path, process_name, function (command_list) {
215214
execCommands(proc.pm2_env.versioning.repo_path, command_list, function(err, res) {
216215
if (err !== null)
217216
{
218-
vizion.prev({folder: proc.pm2_env.versioning.repo_path}, function(err2, meta2) {
217+
require('vizion').prev({folder: proc.pm2_env.versioning.repo_path}, function(err2, meta2) {
219218
printError(err);
220219
return cb ? cb({msg:meta.output + err}) : that.exitCli(cst.ERROR_EXIT);
221220
});

lib/Client.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ var axon = require('pm2-axon');
1313
var util = require('util');
1414
var fs = require('fs');
1515
var path = require('path');
16-
var mkdirp = require('mkdirp');
17-
var shelljs = require('shelljs');
1816
var pkg = require('../package.json')
1917

2018
function noop() {}
@@ -131,15 +129,15 @@ Client.prototype.start = function(cb) {
131129
Client.prototype.initFileStructure = function (opts) {
132130
if (!fs.existsSync(opts.DEFAULT_LOG_PATH)) {
133131
try {
134-
mkdirp.sync(opts.DEFAULT_LOG_PATH);
132+
require('mkdirp').sync(opts.DEFAULT_LOG_PATH);
135133
} catch (e) {
136134
console.error(e.stack || e);
137135
}
138136
}
139137

140138
if (!fs.existsSync(opts.DEFAULT_PID_PATH)) {
141139
try {
142-
mkdirp.sync(opts.DEFAULT_PID_PATH);
140+
require('mkdirp').sync(opts.DEFAULT_PID_PATH);
143141
} catch (e) {
144142
console.error(e.stack || e);
145143
}
@@ -155,7 +153,7 @@ Client.prototype.initFileStructure = function (opts) {
155153

156154
if (!fs.existsSync(opts.DEFAULT_MODULE_PATH)) {
157155
try {
158-
mkdirp.sync(opts.DEFAULT_MODULE_PATH);
156+
require('mkdirp').sync(opts.DEFAULT_MODULE_PATH);
159157
} catch (e) {
160158
console.error(e.stack || e);
161159
}
@@ -243,7 +241,7 @@ Client.prototype.launchDaemon = function(opts, cb) {
243241

244242
var interpreter = 'node';
245243

246-
if (shelljs.which('node') == null)
244+
if (require('shelljs').which('node') == null)
247245
interpreter = process.execPath;
248246

249247
var child = require('child_process').spawn(interpreter, node_args, {

0 commit comments

Comments
 (0)