DevCLI.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict';
  2. process.env.PM2_NO_INTERACTION = 'true';
  3. // Do not print banner
  4. process.env.PM2_DISCRETE_MODE = true;
  5. var commander = require('commander');
  6. var PM2 = require('../..');
  7. var Log = require('../API/Log');
  8. var cst = require('../../constants.js');
  9. var pkg = require('../../package.json');
  10. var chalk = require('chalk');
  11. var path = require('path');
  12. var fmt = require('../tools/fmt.js');
  13. var exec = require('child_process').exec;
  14. var os = require('os');
  15. commander.version(pkg.version)
  16. .description('pm2-dev monitor for any file changes and automatically restart it')
  17. .option('--raw', 'raw log output')
  18. .option('--timestamp', 'print timestamp')
  19. .option('--node-args <node_args>', 'space delimited arguments to pass to node in cluster mode - e.g. --node-args="--debug=7001 --trace-deprecation"')
  20. .option('--ignore [files]', 'files to ignore while watching')
  21. .option('--post-exec [cmd]', 'execute extra command after change detected')
  22. .option('--silent-exec', 'do not output result of post command', false)
  23. .option('--test-mode', 'debug mode for test suit')
  24. .option('--interpreter <interpreter>', 'the interpreter pm2 should use for executing app (bash, python...)')
  25. .option('--env [name]', 'select env_[name] env variables in process config file')
  26. .option('--auto-exit', 'exit if all processes are errored/stopped or 0 apps launched')
  27. .usage('pm2-dev app.js');
  28. var pm2 = new PM2.custom({
  29. pm2_home : path.join(os.homedir ? os.homedir() : (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE), '.pm2-dev')
  30. });
  31. pm2.connect(function() {
  32. commander.parse(process.argv);
  33. });
  34. function postExecCmd(command, cb) {
  35. var exec_cmd = exec(command);
  36. if (commander.silentExec !== true) {
  37. exec_cmd.stdout.on('data', function(data) {
  38. process.stdout.write(data);
  39. });
  40. exec_cmd.stderr.on('data', function(data) {
  41. process.stderr.write(data);
  42. });
  43. }
  44. exec_cmd.on('close', function done() {
  45. if (cb) cb(null);
  46. });
  47. exec_cmd.on('error', function (err) {
  48. console.error(err.stack || err);
  49. });
  50. };
  51. function run(cmd, opts) {
  52. var timestamp = opts.timestamp;
  53. opts.watch = true;
  54. opts.autorestart = true;
  55. opts.restart_delay = 1000
  56. if (opts.autoExit)
  57. autoExit();
  58. if (opts.ignore) {
  59. opts.ignore_watch = opts.ignore.split(',')
  60. opts.ignore_watch.push('node_modules');
  61. }
  62. if (timestamp === true)
  63. timestamp = 'YYYY-MM-DD-HH:mm:ss';
  64. pm2.start(cmd, opts, function(err, procs) {
  65. if (err) {
  66. console.error(err);
  67. pm2.destroy(function() {
  68. process.exit(0);
  69. });
  70. return false;
  71. }
  72. if (opts.testMode) {
  73. return pm2.disconnect(function() {
  74. });
  75. }
  76. fmt.sep();
  77. fmt.title('PM2 development mode');
  78. fmt.field('Apps started', procs.map(function(p) { return p.pm2_env.name } ));
  79. fmt.field('Processes started', chalk.bold(procs.length));
  80. fmt.field('Watch and Restart', chalk.green('Enabled'));
  81. fmt.field('Ignored folder', opts.ignore_watch || 'node_modules');
  82. if (opts.postExec)
  83. fmt.field('Post restart cmd', opts.postExec);
  84. fmt.sep();
  85. setTimeout(function() {
  86. pm2.Client.launchBus(function(err, bus) {
  87. bus.on('process:event', function(packet) {
  88. if (packet.event == 'online') {
  89. if (opts.postExec)
  90. postExecCmd(opts.postExec);
  91. }
  92. });
  93. });
  94. }, 1000);
  95. Log.devStream(pm2.Client, 'all', opts.raw, timestamp, false);
  96. process.on('SIGINT', function() {
  97. console.log('>>>>> [PM2 DEV] Stopping current development session');
  98. pm2.delete('all', function() {
  99. pm2.destroy(function() {
  100. process.exit(0);
  101. });
  102. });
  103. });
  104. });
  105. }
  106. commander.command('*')
  107. .action(function(cmd, opts){
  108. run(cmd, commander);
  109. });
  110. commander.command('start <file|json_file>')
  111. .description('start target config file/script in development mode')
  112. .action(function(cmd, opts) {
  113. run(cmd, commander);
  114. });
  115. function exitPM2() {
  116. if (pm2 && pm2.connected == true) {
  117. console.log(chalk.green.bold('>>> Exiting PM2'));
  118. pm2.kill(function() {
  119. process.exit(0);
  120. });
  121. }
  122. else
  123. process.exit(0);
  124. }
  125. function autoExit(final) {
  126. setTimeout(function() {
  127. pm2.list(function(err, apps) {
  128. if (err) console.error(err.stack || err);
  129. var online_count = 0;
  130. apps.forEach(function(app) {
  131. if (app.pm2_env.status == cst.ONLINE_STATUS ||
  132. app.pm2_env.status == cst.LAUNCHING_STATUS)
  133. online_count++;
  134. });
  135. if (online_count == 0) {
  136. console.log('0 application online, exiting');
  137. if (final == true)
  138. process.exit(1);
  139. else
  140. autoExit(true);
  141. return false;
  142. }
  143. autoExit(false);
  144. });
  145. }, 3000);
  146. }
  147. if (process.argv.length == 2) {
  148. commander.outputHelp();
  149. exitPM2();
  150. }