pm2/bin/pm2-dev
2015-06-02 10:29:26 +02:00

89 lines
2.6 KiB
JavaScript
Executable File

#!/usr/bin/env node
'use strict';
var commander = require('commander');
var debug = require('debug')('pm2:cli');
var Satan = require('../lib/Satan');
var CLI = require('../lib/CLI');
var cst = require('../constants.js');
var pkg = require('../package.json');
var platform = require('os').platform();
var moment = require('moment');
var Common = require('../lib/Common');
var chalk = require('chalk');
process.env.PM2_SILENT = true;
commander.version(pkg.version)
.option('--raw', 'raw log output')
.option('--next-gen-js', 'enable es6/es7 compatibility')
.usage('[cmd] app');
CLI.pm2Init();
Satan.start(false, function() {
commander.parse(process.argv);
});
function run(cmd, opts) {
commander.watch = true;
commander.autorestart = false;
var needRaw = opts.parent.rawArgs.indexOf('--raw') > -1 || false;
CLI.start(cmd, commander, function() {
CLI.launchBus(function(err, bus) {
if (err) throw new Error(err);
bus.on('log:out', function(data) {
process.stdout.write((needRaw ? '' : chalk.green(data.process.name) + ': ') + data.data.str);
});
bus.on('log:err', function(data) {
process.stderr.write((needRaw ? '' : chalk.red(data.process.name) + ': ') + data.process.name + data.data.str);
});
bus.on('process:event', function(data) {
if (data.event == 'exit' && data.process.exit_code != 0) {
console.log(moment().format('h:mm:ss a') + chalk.bold.red(' [Crash] Process ' + data.process.name + ' crashed with code ' + data.process.exit_code + ' - waiting for file changes'));
}
else if (data.event == 'exit') {
console.log(moment().format('h:mm:ss a') + chalk.bold.blue(' [Exited] Process ' + data.process.name + ' exited with code ' + data.process.exit_code + ' - waiting for file changes'));
}
else if (data.event == 'online') {
console.log(moment().format('h:mm:ss a') + chalk.bold.green(' [Online] Process ' + data.process.name + ' started'));
}
});
});
process.on('SIGINT', function() {
CLI.delete(cmd, '', function() {
Common.exitCli(cst.SUCCESS_EXIT);
});
});
});
}
commander.command('*')
.description('run <file|json_file> in development mode')
.action(function(cmd, opts){
run(cmd, opts);
});
commander.command('run <file|json_file>')
.alias('start')
.description('run <file|json_file> in development mode')
.action(function(cmd, opts) {
run(cmd, opts);
});
if (process.argv.length == 2) {
commander.outputHelp();
process.exit(1);
}