mirror of
https://github.com/Unitech/pm2.git
synced 2025-12-08 20:35:53 +00:00
106 lines
2.5 KiB
JavaScript
Executable File
106 lines
2.5 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
var commander = require('commander');
|
|
|
|
var debug = require('debug')('pm2:cli');
|
|
var PM2 = require('..');
|
|
var CLI = require('../lib/CLI');
|
|
var Log = require('../lib/CLI/Log');
|
|
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');
|
|
var path = require('path');
|
|
|
|
process.env.PM2_SILENT = 'true';
|
|
|
|
commander.version(pkg.version)
|
|
.option('--raw', 'raw log output')
|
|
.option('--auto-exit', 'exit if all processes are errored/stopped or 0 apps launched')
|
|
.option('--timestamp [format]', 'add timestamps to logs')
|
|
.usage('[cmd] app');
|
|
|
|
var pm2 = new PM2.custom({
|
|
pm2_home : path.join(process.env.HOME, '.pm2')
|
|
});
|
|
|
|
pm2.connect(function() {
|
|
commander.parse(process.argv);
|
|
});
|
|
|
|
process.on('SIGINT', function() {
|
|
exitPM2();
|
|
});
|
|
|
|
function run(cmd, opts) {
|
|
var needRaw = opts.parent.rawArgs.indexOf('--raw') > -1 || false;
|
|
var timestamp = opts.parent.timestamp;
|
|
|
|
if (timestamp === true)
|
|
timestamp = 'YYYY-MM-DD-HH:mm:ss';
|
|
|
|
pm2.start(cmd, commander, function(err, obj) {
|
|
Log.stream(pm2.Client, 'all', needRaw, timestamp, false);
|
|
});
|
|
}
|
|
|
|
function exitPM2() {
|
|
console.log('Exiting PM2');
|
|
pm2.delete('all', function() {
|
|
pm2.disconnect(function() {
|
|
process.exit(0);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Exit current PM2 instance if 0 app is online
|
|
* function activated via --auto-exit
|
|
*/
|
|
function autoExit() {
|
|
setTimeout(function() {
|
|
pm2.list(function(err, apps) {
|
|
if (err) console.error(err.stack || err);
|
|
|
|
var online_count = 0;
|
|
|
|
apps.forEach(function(app) {
|
|
if (app.pm2_env.status == cst.ONLINE_STATUS ||
|
|
app.pm2_env.status == cst.LAUNCHING_STATUS)
|
|
online_count++;
|
|
});
|
|
|
|
if (online_count == 0) {
|
|
console.log('0 application online, exiting');
|
|
exitPM2();
|
|
}
|
|
autoExit();
|
|
});
|
|
}, 3000);
|
|
}
|
|
|
|
commander.command('*')
|
|
.description('run <file|json_file> in development mode')
|
|
.action(function(cmd, opts){
|
|
run(cmd, opts);
|
|
});
|
|
|
|
// @todo need to allow passing same option than pm2 start
|
|
commander.command('run <file|json_file>')
|
|
.alias('start')
|
|
.description('run <file|json_file> in development mode')
|
|
.action(function(cmd, opts) {
|
|
if (commander.autoExit)
|
|
autoExit();
|
|
run(cmd, opts);
|
|
});
|
|
|
|
if (process.argv.length == 2) {
|
|
commander.outputHelp();
|
|
process.exit(1);
|
|
}
|