147 lines
3.9 KiB
JavaScript
Executable File
147 lines
3.9 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
var commander = require('commander'),
|
|
path = p = require('path'),
|
|
fs = require('fs'),
|
|
chalk = require('chalk'),
|
|
_ = require('lodash'),
|
|
pkg = require('../package.json'),
|
|
Monitor = require('../lib/mon'),
|
|
crypto = require('crypto'),
|
|
conf = require('../lib/util/conf'),
|
|
interface = require('../web/index');
|
|
|
|
commander.version(pkg.version, '-v, --version')
|
|
.usage('[cmd] [options]');
|
|
|
|
commander.on('--help', function(){
|
|
console.log(' Basic Examples:\n\n' +
|
|
' Start the web server, by default port (8088):\n' +
|
|
chalk.grey(' $ pm2-gui start\n') +
|
|
'\n' +
|
|
' Start the web server, by specific port (8090):\n' +
|
|
chalk.grey(' $ pm2-gui start 8090\n') +
|
|
'\n' +
|
|
' Start the web server, by specific configuration file (pm2-gui.ini):\n' +
|
|
chalk.grey(' $ pm2-gui start --config\n') +
|
|
'\n' +
|
|
' Start the web server, by specific configuration file:\n' +
|
|
chalk.grey(' $ pm2-gui start --config my-config.ini\n')
|
|
);
|
|
});
|
|
|
|
/**
|
|
* Run web interface.
|
|
*/
|
|
commander.command('start [port]')
|
|
.option('--config [file]', 'pass ".ini" configuration file (with options)')
|
|
.option('--no-debug', 'hide stdout / stderr information')
|
|
.description('Launch the web server, port default by 8088')
|
|
.action(function(port, cmd){
|
|
if (cmd.config) {
|
|
var jsonFile;
|
|
if (typeof cmd.config != 'string') {
|
|
jsonFile = 'pm2-gui.ini';
|
|
} else {
|
|
jsonFile = cmd.config;
|
|
if (jsonFile.indexOf('.') < 0) {
|
|
jsonFile += '.ini';
|
|
}
|
|
}
|
|
if (!fs.existsSync(jsonFile)) {
|
|
console.log(chalk.red('✘ .ini configuration file does not exist!\n'));
|
|
process.exit();
|
|
}
|
|
|
|
try {
|
|
var config = conf.File(path.resolve(process.cwd(), jsonFile)).loadSync().valueOf();
|
|
setConfig(config);
|
|
} catch (err) {
|
|
console.log(chalk.red('✘ .ini configuration file is invalid!\n'));
|
|
process.exit();
|
|
}
|
|
}
|
|
port && setConfig('port', port);
|
|
interface(cmd.debug);
|
|
});
|
|
|
|
commander.command('config')
|
|
.description('show all configs')
|
|
.action(showConfigs);
|
|
|
|
commander.command('set <key> <value>')
|
|
.description('set config by key-value pairs')
|
|
.action(function(key, value, cmd){
|
|
var mon = setConfig(key, value);
|
|
mon && showConfigs(cmd, mon);
|
|
});
|
|
|
|
commander.command('rm <key>')
|
|
.description('remove config by key')
|
|
.action(function(key, cmd){
|
|
var mon = Monitor();
|
|
mon.config(key, null);
|
|
showConfigs(cmd, mon);
|
|
});
|
|
|
|
commander.parse(process.argv);
|
|
|
|
if (process.argv.length == 2) {
|
|
commander.outputHelp();
|
|
process.exit(0);
|
|
}
|
|
|
|
/**
|
|
* Set configuration.
|
|
* @param key
|
|
* @param value
|
|
* @returns {*}
|
|
*/
|
|
function setConfig(key, value){
|
|
var mon = Monitor(),
|
|
acceptKeys = Object.keys(mon.options).filter(function(key){
|
|
return !~['socketio', 'pm2Conf'].indexOf(key);
|
|
});
|
|
|
|
acceptKeys.push('password');
|
|
|
|
(function config(pairs){
|
|
if (pairs.length == 0) {
|
|
return;
|
|
}
|
|
var pair = pairs.shift();
|
|
if (!~acceptKeys.indexOf(pair[0])) {
|
|
console.log(chalk.bold.yellow('[WARN]'), chalk.cyan(pair[0]), 'is not an acceptable configuration.');
|
|
return config(pairs);
|
|
}
|
|
if (pair[0] == 'password') {
|
|
var md5 = crypto.createHash('md5');
|
|
md5.update(pair[1]);
|
|
pair[1] = md5.digest('hex');
|
|
}
|
|
mon.config(pair[0], pair[1]);
|
|
config(pairs);
|
|
})(typeof key == 'object' ? _.pairs(key) : [[key, value]]);
|
|
|
|
return mon;
|
|
}
|
|
/**
|
|
* Show all configurations.
|
|
* @param cmd
|
|
* @param mon
|
|
*/
|
|
function showConfigs(cmd, mon){
|
|
if (!mon) {
|
|
mon = Monitor();
|
|
}
|
|
var storage = mon._config.valueOf(), prints = '';
|
|
var maxLen = 0;
|
|
for (var k in storage) {
|
|
maxLen = Math.max(k.length, maxLen);
|
|
}
|
|
maxLen += 1;
|
|
for (var k in storage) {
|
|
prints += chalk.bold(Array(maxLen - k.length).join(' ') + k + ': ') + ' ' + chalk.blue(storage[k] + '\n');
|
|
}
|
|
console.log(prints);
|
|
} |