pm2/lib/DevCli.js

133 lines
3.3 KiB
JavaScript

'use strict';
var commander = require('commander');
var debug = require('debug')('pm2:cli');
var PM2 = require('..');
var Log = require('./API/Log');
var cst = require('../constants.js');
var pkg = require('../package.json');
var platform = require('os').platform();
var moment = require('moment');
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('--timestamp', 'print timestamp')
.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({
pm2_home : path.join(process.env.HOME, '.pm2-dev')
});
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;
commander.watch = true;
commander.autorestart = false;
commander.instances = 1;
if (commander.ignore) {
commander.ignore_watch = commander.ignore.split(',')
commander.ignore_watch.push('node_modules');
}
if (timestamp === true)
timestamp = 'YYYY-MM-DD-HH:mm:ss';
pm2.start(cmd, commander, function(err, procs) {
if (err) {
console.error(err);
pm2.destroy(function() {
process.exit(0);
});
return false;
}
fmt.sep();
fmt.title('PM2 development mode');
fmt.field('Apps started', procs.map(function(p) { return p.pm2_env.name } ));
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() {
console.log('>>>>> [PM2 DEV] Stopping current development session');
pm2.delete('all', function() {
pm2.destroy(function() {
process.exit(0);
});
});
});
});
}
commander.command('*')
.action(function(cmd, opts){
run(cmd, opts);
});
commander.command('start <file|json_file>')
.description('start target config file/script in development mode')
.action(function(cmd, opts) {
run(cmd, opts);
});
if (process.argv.length == 2) {
commander.outputHelp();
process.exit(1);
}