From ee5e6a06cbf93f2d1fa7fa022d6bdcad55a39695 Mon Sep 17 00:00:00 2001 From: Unitech Date: Thu, 17 May 2018 23:34:48 +0200 Subject: [PATCH] refactor: faster cli with less require --- constants.js | 6 +----- lib/API/Containerizer.js | 3 +-- lib/API/Deploy.js | 4 +--- lib/API/Keymetrics/cli-api.js | 4 +++- lib/API/Keymetrics/kmapi.js | 5 +++-- lib/API/Modules/Modularizer.js | 24 +++++++++++------------- lib/API/Modules/Modularizerv1.js | 1 - lib/API/Modules/Modules.js | 1 - lib/API/Startup.js | 7 +++---- lib/API/Version.js | 15 +++++++-------- lib/Client.js | 10 ++++------ lib/Common.js | 11 ++++------- package.json | 2 +- 13 files changed, 39 insertions(+), 54 deletions(-) diff --git a/constants.js b/constants.js index 48d7a9a09..3bbcb07cb 100644 --- a/constants.js +++ b/constants.js @@ -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 @@ -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; })(), diff --git a/lib/API/Containerizer.js b/lib/API/Containerizer.js index 28f4d5728..686edfd1d 100644 --- a/lib/API/Containerizer.js +++ b/lib/API/Containerizer.js @@ -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'); @@ -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, diff --git a/lib/API/Deploy.js b/lib/API/Deploy.js index 9a19b5163..7cc7845ac 100644 --- a/lib/API/Deploy.js +++ b/lib/API/Deploy.js @@ -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'); @@ -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); diff --git a/lib/API/Keymetrics/cli-api.js b/lib/API/Keymetrics/cli-api.js index 308521439..596c0f964 100644 --- a/lib/API/Keymetrics/cli-api.js +++ b/lib/API/Keymetrics/cli-api.js @@ -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'); @@ -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) { @@ -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, diff --git a/lib/API/Keymetrics/kmapi.js b/lib/API/Keymetrics/kmapi.js index af5d91f8c..6cd75dcff 100644 --- a/lib/API/Keymetrics/kmapi.js +++ b/lib/API/Keymetrics/kmapi.js @@ -1,5 +1,4 @@ -var querystring = require('querystring'); -var https = require('https'); + var fs = require('fs'); var needle = require('needle'); var url = require('url'); @@ -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; diff --git a/lib/API/Modules/Modularizer.js b/lib/API/Modules/Modularizer.js index ea1efd320..900afc1e5 100644 --- a/lib/API/Modules/Modularizer.js +++ b/lib/API/Modules/Modularizer.js @@ -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'); @@ -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'; @@ -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 ], { @@ -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); @@ -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'); @@ -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); } } @@ -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); @@ -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(''); diff --git a/lib/API/Modules/Modularizerv1.js b/lib/API/Modules/Modularizerv1.js index 00d489917..ad63a5609 100644 --- a/lib/API/Modules/Modularizerv1.js +++ b/lib/API/Modules/Modularizerv1.js @@ -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'); diff --git a/lib/API/Modules/Modules.js b/lib/API/Modules/Modules.js index 77412b01d..3d9f6f2a3 100644 --- a/lib/API/Modules/Modules.js +++ b/lib/API/Modules/Modules.js @@ -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; diff --git a/lib/API/Startup.js b/lib/API/Startup.js index 356197f64..e27469c1d 100644 --- a/lib/API/Startup.js +++ b/lib/API/Startup.js @@ -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) { /** @@ -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; } } @@ -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) { @@ -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(); diff --git a/lib/API/Version.js b/lib/API/Version.js index 844953174..aefe9352b 100644 --- a/lib/API/Version.js +++ b/lib/API/Version.js @@ -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; @@ -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) { @@ -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) { @@ -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) @@ -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); }); @@ -207,7 +206,7 @@ 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) { @@ -215,7 +214,7 @@ module.exports = function(CLI) { 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); }); diff --git a/lib/Client.js b/lib/Client.js index cf5eb2229..6edb18f77 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -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() {} @@ -131,7 +129,7 @@ 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); } @@ -139,7 +137,7 @@ Client.prototype.initFileStructure = function (opts) { 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); } @@ -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); } @@ -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, { diff --git a/lib/Common.js b/lib/Common.js index bae553738..662df66bb 100644 --- a/lib/Common.js +++ b/lib/Common.js @@ -12,9 +12,7 @@ var fs = require('fs'); var path = require('path'); var os = require('os'); var util = require('util'); -var mkdirp = require('mkdirp'); var async = require('async'); -var shelljs = require('shelljs'); var chalk = require('chalk'); var fclone = require('fclone'); var semver = require('semver'); @@ -23,7 +21,6 @@ var isBinary = require('./tools/isbinaryfile.js'); var cst = require('../constants.js'); var extItps = require('./API/interpreter.json'); var Config = require('./tools/Config'); -var KMDaemon = require('@pm2/agent/src/InteractorClient'); var Common = module.exports; @@ -151,7 +148,7 @@ Common.prepareAppConf = function(opts, app) { if (!fs.existsSync(app.pm_exec_path)) { var ckd; // Try resolve command available in $PATH - if ((ckd = shelljs.which(app.script))) { + if ((ckd = require('shelljs').which(app.script))) { if (typeof(ckd) !== 'string') ckd = ckd.toString(); app.pm_exec_path = ckd; @@ -226,7 +223,7 @@ Common.prepareAppConf = function(opts, app) { if (!fs.existsSync(dir)) { Common.printError(cst.PREFIX_MSG_WARNING + 'Folder does not exists: ' + dir); Common.printOut(cst.PREFIX_MSG + 'Creating folder: ' + dir); - mkdirp(dir, function(err) { + require('mkdirp')(dir, function(err) { if (!err) return; Common.printError(cst.PREFIX_MSG_ERR + 'Could not create folder: ' + path.dirname(af)); throw new Error('Could not create folder'); @@ -368,7 +365,7 @@ var resolveNodeInterpreter = function(app) { var nvm_cmd = '. ' + nvm_bin + ' ; nvm install ' + node_version; Common.printOut(cst.PREFIX_MSG + 'Executing: %s', nvm_cmd); - shelljs.exec(nvm_cmd); + require('shelljs').exec(nvm_cmd); } Common.printOut(cst.PREFIX_MSG + chalk.green.bold('Setting Node to v%s (path=%s)'), @@ -411,7 +408,7 @@ Common.sink.resolveInterpreter = function(app) { app.exec_interpreter = path.resolve(__dirname, '../node_modules/.bin/coffee'); } - if (app.exec_interpreter != 'none' && shelljs.which(app.exec_interpreter) == null) { + if (app.exec_interpreter != 'none' && require('shelljs').which(app.exec_interpreter) == null) { Common.printError(cst.PREFIX_MSG_ERR + 'Interpreter ' + app.exec_interpreter + ' does not seem to be available'); } return app; diff --git a/package.json b/package.json index 7e325cc88..9d6a6970c 100644 --- a/package.json +++ b/package.json @@ -158,8 +158,8 @@ "pm2-runtime": "./bin/pm2-runtime" }, "dependencies": { - "@pm2/io": "latest", "@pm2/agent": "^0.4.0", + "@pm2/io": "latest", "async": "^2.6", "blessed": "^0.1.81", "chalk": "^2.4.1",