From 4dcd448a9fe8985bc045e8ae4e263fd3c1b66173 Mon Sep 17 00:00:00 2001 From: Unitech Date: Wed, 27 Jul 2016 11:38:45 -0700 Subject: [PATCH] postExec command on PMD --- CHANGELOG.md | 2 +- lib/DevCli.js | 42 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72f3e194..9f923063 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - Much better Module system with raw NPM feedback - Better Windows support - **pm2-docker** command with his official [Docker image](https://github.com/keymetrics/pm2-docker-alpine) -- **pm2-dev** command enhanced +- **pm2-dev -> pmd** command enhanced (better log output, post-exec cmd) - Watch and Reload instead of Watch and Restart - New PM2 API, backward compatible with previous PM2 versions diff --git a/lib/DevCli.js b/lib/DevCli.js index a1e91794..0cd04576 100644 --- a/lib/DevCli.js +++ b/lib/DevCli.js @@ -14,12 +14,15 @@ var Common = require('./Common'); var chalk = require('chalk'); var path = require('path'); var fmt = require('./tools/fmt.js'); +var exec = require('child_process').exec; process.env.PM2_SILENT = 'true'; commander.version(pkg.version) .option('--raw', 'raw log output') .option('--ignore [files]', 'files to ignore while watching') + .option('--post-exec [cmd]', 'execute extra command after change detected') + .option('--silent-exec', 'do not output result of post command', false) .usage('[cmd] app'); var pm2 = new PM2.custom({ @@ -30,6 +33,28 @@ pm2.connect(function() { commander.parse(process.argv); }); +function postExecCmd(command, cb) { + var exec_cmd = exec(command); + + if (commander.silentExec !== true) { + exec_cmd.stdout.on('data', function(data) { + process.stdout.write(data); + }); + + exec_cmd.stderr.on('data', function(data) { + process.stderr.write(data); + }); + } + + exec_cmd.on('close', function done() { + if (cb) cb(null); + }); + + exec_cmd.on('error', function (err) { + console.error(err.stack || err); + }); +}; + function run(cmd, opts) { var timestamp = commander.timestamp; @@ -61,8 +86,20 @@ function run(cmd, opts) { fmt.field('Processes started', chalk.bold(procs.length)); fmt.field('Watch and Restart', chalk.green('Enabled')); fmt.field('Ignored folder', commander.ignore_watch || 'node_modules'); + if (commander.postExec) + fmt.field('Post restart cmd', commander.postExec); fmt.sep(); + setTimeout(function() { + pm2.Client.launchBus(function(err, bus) { + bus.on('process:event', function(packet) { + if (packet.event == 'online') { + postExecCmd(commander.postExec); + } + }); + }); + }, 1000); + Log.devStream(pm2.Client, 'all', commander.raw, timestamp, false); process.on('SIGINT', function() { @@ -82,9 +119,8 @@ commander.command('*') run(cmd, opts); }); -commander.command('run ') - .alias('start') - .description('run in development mode') +commander.command('start ') + .description('start target config file/script in development mode') .action(function(cmd, opts) { run(cmd, opts); });