postExec command on PMD

This commit is contained in:
Unitech 2016-07-27 11:38:45 -07:00
parent c79dc951c8
commit 4dcd448a9f
2 changed files with 40 additions and 4 deletions

View File

@ -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

View File

@ -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 <file|json_file>')
.alias('start')
.description('run <file|json_file> in development mode')
commander.command('start <file|json_file>')
.description('start target config file/script in development mode')
.action(function(cmd, opts) {
run(cmd, opts);
});