CLI refactor with commander (#6058)
* CLI refactor with commander
- Modularized the functionality
- All functionality done directly from `./nodebb` now
(still available from `app` for backwards compatibility)
- Moved all CLI code from `./nodebb` to `src/cli`
- Fixed `nodebb.bat` to work from any location, like `./nodebb`, and
also hides command output
- Overwrite some commander methods to add CLI color support
- Added `./nodebb info` for quick info including git hash, NodeBB
version, node version, and some database info
- Refactored `./nodebb reset` to allow multiple resets at once
- Changed `./nodebb restart` to essentially stop and start, as Windows
doesn't support signals
- Added `-l, --log` option which works on `./nodebb start` and `./nodebb
restart` to show logging, like `./nodebb slog`
- Expanded `-d, --dev` option which works on them as well, like
`./nodebb dev`
- Improvements to self-help. `./nodebb build -h` will output all
possible targets
- `./nodebb reset` explains usage better
* Fix some style inconsistencies
* Fix prestart being required before modules installed
* Fix travis failures
* Fix `help` command to output help for subcommands
* Pick steps of the upgrade process to run
* Fix formatting for upgrade help
* Fix web installer
7 years ago
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
// override commander functions
|
|
|
|
// to include color styling in the output
|
|
|
|
// so the CLI looks nice
|
|
|
|
|
|
|
|
var Command = require('commander').Command;
|
|
|
|
|
|
|
|
var commandColor = 'yellow';
|
|
|
|
var optionColor = 'cyan';
|
|
|
|
var argColor = 'magenta';
|
|
|
|
var subCommandColor = 'green';
|
|
|
|
var subOptionColor = 'blue';
|
|
|
|
var subArgColor = 'red';
|
|
|
|
|
|
|
|
Command.prototype.helpInformation = function () {
|
|
|
|
var desc = [];
|
|
|
|
if (this._description) {
|
|
|
|
desc = [
|
|
|
|
' ' + this._description,
|
|
|
|
'',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdName = this._name;
|
|
|
|
if (this._alias) {
|
|
|
|
cmdName = cmdName + ' | ' + this._alias;
|
|
|
|
}
|
|
|
|
var usage = [
|
|
|
|
'',
|
|
|
|
' Usage: ' + cmdName[commandColor] + ' '.reset + this.usage(),
|
|
|
|
'',
|
|
|
|
];
|
|
|
|
|
|
|
|
var cmds = [];
|
|
|
|
var commandHelp = this.commandHelp();
|
|
|
|
if (commandHelp) {
|
|
|
|
cmds = [commandHelp];
|
|
|
|
}
|
|
|
|
|
|
|
|
var options = [
|
|
|
|
'',
|
|
|
|
' Options:',
|
|
|
|
'',
|
|
|
|
'' + this.optionHelp().replace(/^/gm, ' '),
|
|
|
|
'',
|
|
|
|
];
|
|
|
|
|
|
|
|
return usage
|
|
|
|
.concat(desc)
|
|
|
|
.concat(options)
|
|
|
|
.concat(cmds)
|
|
|
|
.join('\n'.reset);
|
|
|
|
};
|
|
|
|
|
|
|
|
function humanReadableArgName(arg) {
|
|
|
|
var nameOutput = arg.name + (arg.variadic === true ? '...' : '');
|
|
|
|
|
|
|
|
return arg.required ? '<' + nameOutput + '>' : '[' + nameOutput + ']';
|
|
|
|
}
|
|
|
|
|
|
|
|
Command.prototype.usage = function () {
|
|
|
|
var args = this._args.map(function (arg) {
|
|
|
|
return humanReadableArgName(arg);
|
|
|
|
});
|
|
|
|
|
|
|
|
var usage = '[options]'[optionColor]
|
|
|
|
+ (this.commands.length ? ' [command]' : '')[subCommandColor]
|
|
|
|
+ (this._args.length ? ' ' + args.join(' ') : '')[argColor];
|
CLI refactor with commander (#6058)
* CLI refactor with commander
- Modularized the functionality
- All functionality done directly from `./nodebb` now
(still available from `app` for backwards compatibility)
- Moved all CLI code from `./nodebb` to `src/cli`
- Fixed `nodebb.bat` to work from any location, like `./nodebb`, and
also hides command output
- Overwrite some commander methods to add CLI color support
- Added `./nodebb info` for quick info including git hash, NodeBB
version, node version, and some database info
- Refactored `./nodebb reset` to allow multiple resets at once
- Changed `./nodebb restart` to essentially stop and start, as Windows
doesn't support signals
- Added `-l, --log` option which works on `./nodebb start` and `./nodebb
restart` to show logging, like `./nodebb slog`
- Expanded `-d, --dev` option which works on them as well, like
`./nodebb dev`
- Improvements to self-help. `./nodebb build -h` will output all
possible targets
- `./nodebb reset` explains usage better
* Fix some style inconsistencies
* Fix prestart being required before modules installed
* Fix travis failures
* Fix `help` command to output help for subcommands
* Pick steps of the upgrade process to run
* Fix formatting for upgrade help
* Fix web installer
7 years ago
|
|
|
|
|
|
|
return usage;
|
|
|
|
};
|
|
|
|
|
|
|
|
function pad(str, width) {
|
|
|
|
var len = Math.max(0, width - str.length);
|
|
|
|
return str + Array(len + 1).join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
Command.prototype.commandHelp = function () {
|
|
|
|
if (!this.commands.length) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
var commands = this.commands.filter(function (cmd) {
|
|
|
|
return !cmd._noHelp;
|
|
|
|
}).map(function (cmd) {
|
|
|
|
var args = cmd._args.map(function (arg) {
|
|
|
|
return humanReadableArgName(arg);
|
|
|
|
}).join(' ');
|
|
|
|
|
|
|
|
return [
|
|
|
|
cmd._name[subCommandColor]
|
|
|
|
+ (cmd._alias ? ' | ' + cmd._alias : '')[subCommandColor]
|
|
|
|
+ (cmd.options.length ? ' [options]' : '')[subOptionColor]
|
|
|
|
+ ' ' + args[subArgColor],
|
CLI refactor with commander (#6058)
* CLI refactor with commander
- Modularized the functionality
- All functionality done directly from `./nodebb` now
(still available from `app` for backwards compatibility)
- Moved all CLI code from `./nodebb` to `src/cli`
- Fixed `nodebb.bat` to work from any location, like `./nodebb`, and
also hides command output
- Overwrite some commander methods to add CLI color support
- Added `./nodebb info` for quick info including git hash, NodeBB
version, node version, and some database info
- Refactored `./nodebb reset` to allow multiple resets at once
- Changed `./nodebb restart` to essentially stop and start, as Windows
doesn't support signals
- Added `-l, --log` option which works on `./nodebb start` and `./nodebb
restart` to show logging, like `./nodebb slog`
- Expanded `-d, --dev` option which works on them as well, like
`./nodebb dev`
- Improvements to self-help. `./nodebb build -h` will output all
possible targets
- `./nodebb reset` explains usage better
* Fix some style inconsistencies
* Fix prestart being required before modules installed
* Fix travis failures
* Fix `help` command to output help for subcommands
* Pick steps of the upgrade process to run
* Fix formatting for upgrade help
* Fix web installer
7 years ago
|
|
|
cmd._description,
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
var width = commands.reduce(function (max, command) {
|
|
|
|
return Math.max(max, command[0].length);
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'',
|
|
|
|
' Commands:',
|
|
|
|
'',
|
|
|
|
commands.map(function (cmd) {
|
|
|
|
var desc = cmd[1] ? ' ' + cmd[1] : '';
|
|
|
|
return pad(cmd[0], width) + desc;
|
|
|
|
}).join('\n').replace(/^/gm, ' '),
|
|
|
|
'',
|
|
|
|
].join('\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
Command.prototype.optionHelp = function () {
|
|
|
|
var width = this.largestOptionLength();
|
|
|
|
|
|
|
|
// Append the help information
|
|
|
|
return this.options
|
|
|
|
.map(function (option) {
|
|
|
|
return pad(option.flags, width)[optionColor] + ' ' + option.description;
|
|
|
|
})
|
|
|
|
.concat([pad('-h, --help', width)[optionColor] + ' output usage information'])
|
|
|
|
.join('\n');
|
|
|
|
};
|