Skip to content

Commit

Permalink
refactor: faster cli with less require
Browse files Browse the repository at this point in the history
  • Loading branch information
Unitech committed May 17, 2018
1 parent 4576b4c commit ee5e6a0
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 54 deletions.
6 changes: 1 addition & 5 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var debug = require('debug')('pm2:conf');
var p = require('path');
var util = require('util');
var chalk = require('chalk');
var semver = require('semver');

/**
* Get PM2 path structure
Expand Down Expand Up @@ -79,10 +78,7 @@ var csts = {

// Concurrent actions when doing start/restart/reload
CONCURRENT_ACTIONS : (function() {
var default_concurrent_actions = 1;
if (semver.satisfies(process.versions.node, '>= 4.0.0'))
default_concurrent_actions = 2;
var concurrent_actions = parseInt(process.env.PM2_CONCURRENT_ACTIONS) || default_concurrent_actions;
var concurrent_actions = parseInt(process.env.PM2_CONCURRENT_ACTIONS) || 2;
debug('Using %d parallelism (CONCURRENT_ACTIONS)', concurrent_actions);
return concurrent_actions;
})(),
Expand Down
3 changes: 1 addition & 2 deletions lib/API/Containerizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var exec = require('child_process').exec;
var chalk = require('chalk');
var util = require('util');
var fmt = require('../tools/fmt.js');
var vizion = require('vizion');
var fs = require('fs');
var path = require('path');
var cst = require('../../constants.js');
Expand Down Expand Up @@ -148,7 +147,7 @@ function handleExit(CLI, opts, mode) {
if (err) {
console.error(err);
}
vizion.analyze({folder : process.cwd()}, function recur_path(err, meta){
require('vizion').analyze({folder : process.cwd()}, function recur_path(err, meta){
if (!err && meta.revision) {
var commit_id = util.format('#%s(%s) %s',
meta.branch,
Expand Down
4 changes: 1 addition & 3 deletions lib/API/Deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/

var fs = require('fs');
var Deploy = require('pm2-deploy');

var cst = require('../../constants.js');
var Utility = require('../Utility.js');
var Common = require('../Common.js');
Expand Down Expand Up @@ -104,7 +102,7 @@ module.exports = function(CLI) {
json_conf.deploy[env]['post-deploy'] = 'pm2 startOrRestart ' + file + ' --env ' + env;
}

Deploy.deployForEnv(json_conf.deploy, env, args, function(err, data) {
require('pm2-deploy').deployForEnv(json_conf.deploy, env, args, function(err, data) {
if (err) {
Common.printError('Deploy failed');
return cb ? cb(err) : that.exitCli(cst.ERROR_EXIT);
Expand Down
4 changes: 3 additions & 1 deletion lib/API/Keymetrics/cli-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var async = require('async');
var path = require('path');
var fs = require('fs');
var KMDaemon = require('@pm2/agent/src/InteractorClient');
var KM = require('./kmapi.js');
var Table = require('cli-table-redemption');
var open = require('../../tools/open.js');
var promptly = require('promptly');
Expand Down Expand Up @@ -144,6 +143,7 @@ module.exports = function(CLI) {
* Open Browser
*/
function loginPrompt(cb) {
var KM = require('./kmapi.js');
console.log(chalk.bold('Log in to Keymetrics'));
(function retry() {
promptly.prompt('Username or Email: ', function(err, username) {
Expand Down Expand Up @@ -217,6 +217,8 @@ module.exports = function(CLI) {
* Open Browser for access to monitoring dashboard
*/
function registerPrompt() {
var KM = require('./kmapi.js');

console.log(chalk.bold('Now registering to Keymetrics'));
promptly.prompt('Username: ', {
validator : validateUsername,
Expand Down
5 changes: 3 additions & 2 deletions lib/API/Keymetrics/kmapi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var querystring = require('querystring');
var https = require('https');

var fs = require('fs');
var needle = require('needle');
var url = require('url');
Expand All @@ -19,6 +18,8 @@ var KM = function() {
* @return promise
*/
KM.prototype.loginAndGetAccessToken = function (user_info, cb) {
var querystring = require('querystring');

var that = this;
var URL_AUTH = '/api/oauth/authorize?response_type=token&scope=all&client_id=' +
that.CLIENT_ID + '&redirect_uri=' + that.CB_URI;
Expand Down
24 changes: 11 additions & 13 deletions lib/API/Modules/Modularizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
var shelljs = require('shelljs');
var path = require('path');
var fs = require('fs');
var os = require('os');
Expand All @@ -18,7 +17,6 @@ var Common = require('../../Common');
var Utility = require('../../Utility.js');
var ModularizerV1 = require('./Modularizerv1.js');
var Modularizer = module.exports = {};
var mkdirp = require('mkdirp');

var MODULE_CONF_PREFIX = 'module-db-v2';

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

mkdirp(install_path, function() {
require('mkdirp')(install_path, function() {
process.chdir(os.homedir());

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

if (module_name != '.') {
console.log(proc_path);
shelljs.rm('-r', proc_path);
require('shelljs').rm('-r', proc_path);
}

return cb(err);
}

if (module_name != '.') {
shelljs.rm('-r', proc_path);
require('shelljs').rm('-r', proc_path);
}

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

CLI.deleteModule(canonic_module_name, function() {
// Delete failing module
shelljs.rm('-r', module_path);
require('shelljs').rm('-r', module_path);
// Restore working version
shelljs.cp('-r', backup_path, module_path);
require('shelljs').cp('-r', backup_path, module_path);

var proc_path = path.join(module_path, 'node_modules', canonic_module_name);
var package_json_path = path.join(proc_path, 'package.json');
Expand All @@ -456,7 +454,7 @@ var Rollback = {
var tmpdir = require('os').tmpdir();
var canonic_module_name = Utility.getCanonicModuleName(module_name);
var module_path = path.join(cst.DEFAULT_MODULE_PATH, canonic_module_name);
shelljs.cp('-r', module_path, tmpdir);
require('shelljs').cp('-r', module_path, tmpdir);
}
}

Expand Down Expand Up @@ -521,13 +519,13 @@ Modularizer.publish = function(cb) {
package_json.name,
package_json.version);

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

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

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

Common.printOut(cst.PREFIX_MSG_MOD + 'Getting sample app');
shelljs.exec(cmd1, function(err) {
require('shelljs').exec(cmd1, function(err) {
if (err) Common.printError(cst.PREFIX_MSG_MOD_ERR + err.message);
shelljs.exec(cmd2, function(err) {
require('shelljs').exec(cmd2, function(err) {
console.log('');
shelljs.exec(cmd3, function(err) {
require('shelljs').exec(cmd3, function(err) {
console.log('');
Common.printOut(cst.PREFIX_MSG_MOD + 'Module sample created in folder: ', path.join(process.cwd(), module_name));
console.log('');
Expand Down
1 change: 0 additions & 1 deletion lib/API/Modules/Modularizerv1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Use of this source code is governed by a license that
* can be found in the LICENSE file.
*/
var shelljs = require('shelljs');
var path = require('path');
var fs = require('fs');
var async = require('async');
Expand Down
1 change: 0 additions & 1 deletion lib/API/Modules/Modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var UX = require('../CliUx');
var chalk = require('chalk');
var async = require('async');

var shelljs = require('shelljs');
var path = require('path');
var fs = require('fs');
var p = path;
Expand Down
7 changes: 3 additions & 4 deletions lib/API/Startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var exec = require('child_process').exec;
var Common = require('../Common.js');
var cst = require('../../constants.js');
var spawn = require('child_process').spawn;
var shelljs = require('shelljs');

module.exports = function(CLI) {
/**
Expand Down Expand Up @@ -47,7 +46,7 @@ module.exports = function(CLI) {
var init_systems = Object.keys(hash_map);

for (var i = 0; i < init_systems.length; i++) {
if (shelljs.which(init_systems[i]) != null) {
if (require('shelljs').which(init_systems[i]) != null) {
break;
}
}
Expand Down Expand Up @@ -150,7 +149,7 @@ module.exports = function(CLI) {
];
};

shelljs.exec(commands.join('&& '), function(code, stdout, stderr) {
require('shelljs').exec(commands.join('&& '), function(code, stdout, stderr) {
Common.printOut(stdout);
Common.printOut(stderr);
if (code == 0) {
Expand Down Expand Up @@ -317,7 +316,7 @@ module.exports = function(CLI) {

async.forEachLimit(commands, 1, function(command, next) {
Common.printOut(chalk.bold('>>> Executing %s'), command);
shelljs.exec(command, function(code, stdout, stderr) {
require('shelljs').exec(command, function(code, stdout, stderr) {
if (code === 0) {
Common.printOut(chalk.bold('[DONE] '));
return next();
Expand Down
15 changes: 7 additions & 8 deletions lib/API/Version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var cst = require('../../constants.js');
var Common = require('../Common.js');
var fs = require('fs');
var async = require('async');
var vizion = require('vizion');
var child = require('child_process');

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

var proc = processes[0];
if (proc.pm2_env.versioning) {
vizion.isUpToDate({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) {
require('vizion').isUpToDate({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) {
if (err !== null)
return cb ? cb({msg:err}) : that.exitCli(cst.ERROR_EXIT);
vizion.revertTo(
require('vizion').revertTo(
{revision: commit_id,
folder: proc.pm2_env.versioning.repo_path},
function(err2, meta2) {
Expand Down Expand Up @@ -153,7 +152,7 @@ module.exports = function(CLI) {
proc.pm2_env.versioning === null)
return cb({msg : 'Versioning unknown'});

vizion.prev({
require('vizion').prev({
folder: proc.pm2_env.versioning.repo_path
}, function(err, meta) {
if (err)
Expand All @@ -167,7 +166,7 @@ module.exports = function(CLI) {
getPostUpdateCmds(proc.pm2_env.versioning.repo_path, process_name, function (command_list) {
execCommands(proc.pm2_env.versioning.repo_path, command_list, function(err, res) {
if (err !== null) {
vizion.next({folder: proc.pm2_env.versioning.repo_path}, function(err2, meta2) {
require('vizion').next({folder: proc.pm2_env.versioning.repo_path}, function(err2, meta2) {
printError(err);
return cb ? cb({msg: meta.output + err}) : that.exitCli(cst.ERROR_EXIT);
});
Expand Down Expand Up @@ -207,15 +206,15 @@ module.exports = function(CLI) {
// in case user searched by id/pid
process_name = proc.name;
if (proc.pm2_env.versioning) {
vizion.next({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) {
require('vizion').next({folder: proc.pm2_env.versioning.repo_path}, function(err, meta) {
if (err !== null)
return cb ? cb({msg:err}) : that.exitCli(cst.ERROR_EXIT);
if (meta.success === true) {
getPostUpdateCmds(proc.pm2_env.versioning.repo_path, process_name, function (command_list) {
execCommands(proc.pm2_env.versioning.repo_path, command_list, function(err, res) {
if (err !== null)
{
vizion.prev({folder: proc.pm2_env.versioning.repo_path}, function(err2, meta2) {
require('vizion').prev({folder: proc.pm2_env.versioning.repo_path}, function(err2, meta2) {
printError(err);
return cb ? cb({msg:meta.output + err}) : that.exitCli(cst.ERROR_EXIT);
});
Expand Down
10 changes: 4 additions & 6 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ var axon = require('pm2-axon');
var util = require('util');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var shelljs = require('shelljs');
var pkg = require('../package.json')

function noop() {}
Expand Down Expand Up @@ -131,15 +129,15 @@ Client.prototype.start = function(cb) {
Client.prototype.initFileStructure = function (opts) {
if (!fs.existsSync(opts.DEFAULT_LOG_PATH)) {
try {
mkdirp.sync(opts.DEFAULT_LOG_PATH);
require('mkdirp').sync(opts.DEFAULT_LOG_PATH);
} catch (e) {
console.error(e.stack || e);
}
}

if (!fs.existsSync(opts.DEFAULT_PID_PATH)) {
try {
mkdirp.sync(opts.DEFAULT_PID_PATH);
require('mkdirp').sync(opts.DEFAULT_PID_PATH);
} catch (e) {
console.error(e.stack || e);
}
Expand All @@ -155,7 +153,7 @@ Client.prototype.initFileStructure = function (opts) {

if (!fs.existsSync(opts.DEFAULT_MODULE_PATH)) {
try {
mkdirp.sync(opts.DEFAULT_MODULE_PATH);
require('mkdirp').sync(opts.DEFAULT_MODULE_PATH);
} catch (e) {
console.error(e.stack || e);
}
Expand Down Expand Up @@ -243,7 +241,7 @@ Client.prototype.launchDaemon = function(opts, cb) {

var interpreter = 'node';

if (shelljs.which('node') == null)
if (require('shelljs').which('node') == null)
interpreter = process.execPath;

var child = require('child_process').spawn(interpreter, node_args, {
Expand Down
Loading

0 comments on commit ee5e6a0

Please sign in to comment.