index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /***************************
  2. *
  3. * Module methods
  4. *
  5. **************************/
  6. var cst = require('../../../constants.js');
  7. var Common = require('../../Common.js');
  8. var chalk = require('chalk');
  9. var forEachLimit = require('async/forEachLimit');
  10. var Modularizer = require('./Modularizer.js');
  11. module.exports = function(CLI) {
  12. /**
  13. * Install / Update a module
  14. */
  15. CLI.prototype.install = function(module_name, opts, cb) {
  16. var that = this;
  17. if (typeof(opts) == 'function') {
  18. cb = opts;
  19. opts = {};
  20. }
  21. Modularizer.install(this, module_name, opts, function(err, data) {
  22. if (err) {
  23. Common.printError(cst.PREFIX_MSG_ERR + (err.message || err));
  24. return cb ? cb(Common.retErr(err)) : that.speedList(cst.ERROR_EXIT);
  25. }
  26. return cb ? cb(null, data) : that.speedList(cst.SUCCESS_EXIT);
  27. });
  28. };
  29. /**
  30. * Uninstall a module
  31. */
  32. CLI.prototype.uninstall = function(module_name, cb) {
  33. var that = this;
  34. Modularizer.uninstall(this, module_name, function(err, data) {
  35. if (err)
  36. return cb ? cb(Common.retErr(err)) : that.speedList(cst.ERROR_EXIT);
  37. return cb ? cb(null, data) : that.speedList(cst.SUCCESS_EXIT);
  38. });
  39. };
  40. CLI.prototype.launchAll = function(CLI, cb) {
  41. Modularizer.launchModules(CLI, cb);
  42. };
  43. CLI.prototype.package = function(module_path, cb) {
  44. Modularizer.package(this, module_path, (err, res) => {
  45. if (err) {
  46. Common.errMod(err)
  47. return cb ? cb(err) : this.exitCli(1)
  48. }
  49. Common.logMod(`Module packaged in ${res.path}`)
  50. return cb ? cb(err) : this.exitCli(0)
  51. })
  52. };
  53. /**
  54. * Publish module on NPM + Git push
  55. */
  56. CLI.prototype.publish = function(folder, opts, cb) {
  57. var that = this;
  58. Modularizer.publish(this, folder, opts, function(err, data) {
  59. if (err)
  60. return cb ? cb(Common.retErr(err)) : that.speedList(cst.ERROR_EXIT);
  61. return cb ? cb(null, data) : that.speedList(cst.SUCCESS_EXIT);
  62. });
  63. };
  64. /**
  65. * Publish module on NPM + Git push
  66. */
  67. CLI.prototype.generateModuleSample = function(app_name, cb) {
  68. var that = this;
  69. Modularizer.generateSample(app_name, function(err, data) {
  70. if (err)
  71. return cb ? cb(Common.retErr(err)) : that.exitCli(cst.ERROR_EXIT);
  72. return cb ? cb(null, data) : that.exitCli(cst.SUCCESS_EXIT);
  73. });
  74. };
  75. /**
  76. * Special delete method
  77. */
  78. CLI.prototype.deleteModule = function(module_name, cb) {
  79. var that = this;
  80. var found_proc = [];
  81. this.Client.getAllProcess(function(err, procs) {
  82. if (err) {
  83. Common.printError('Error retrieving process list: ' + err);
  84. return cb(Common.retErr(err));
  85. }
  86. procs.forEach(function(proc) {
  87. if (proc.pm2_env.name == module_name && proc.pm2_env.pmx_module) {
  88. found_proc.push(proc.pm_id);
  89. }
  90. });
  91. if (found_proc.length == 0)
  92. return cb();
  93. that._operate('deleteProcessId', found_proc[0], function(err) {
  94. if (err) return cb(Common.retErr(err));
  95. Common.printOut('In memory process deleted');
  96. return cb();
  97. });
  98. });
  99. };
  100. };