2017-10-07 19:07:38 +02:00
|
|
|
import { _ } from 'lib/locale.js';
|
2017-10-09 21:57:00 +02:00
|
|
|
import { reg } from 'lib/registry.js';
|
2017-10-07 19:07:38 +02:00
|
|
|
|
2017-07-10 22:03:46 +02:00
|
|
|
class BaseCommand {
|
|
|
|
|
2017-10-07 18:30:27 +02:00
|
|
|
constructor() {
|
|
|
|
this.stdout_ = null;
|
2017-10-07 19:07:38 +02:00
|
|
|
this.prompt_ = null;
|
2017-10-07 18:30:27 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 22:03:46 +02:00
|
|
|
usage() {
|
|
|
|
throw new Error('Usage not defined');
|
|
|
|
}
|
|
|
|
|
|
|
|
description() {
|
|
|
|
throw new Error('Description not defined');
|
|
|
|
}
|
|
|
|
|
|
|
|
async action(args) {
|
|
|
|
throw new Error('Action not defined');
|
|
|
|
}
|
|
|
|
|
|
|
|
aliases() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
options() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:21:03 +02:00
|
|
|
hidden() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-19 00:14:20 +02:00
|
|
|
enabled() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-20 16:29:18 +02:00
|
|
|
cancellable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:03:46 +02:00
|
|
|
async cancel() {}
|
|
|
|
|
2017-08-04 19:51:01 +02:00
|
|
|
name() {
|
|
|
|
let r = this.usage().split(' ');
|
|
|
|
return r[0];
|
|
|
|
}
|
|
|
|
|
2017-10-07 18:30:27 +02:00
|
|
|
setStdout(fn) {
|
|
|
|
this.stdout_ = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout(...object) {
|
|
|
|
if (this.stdout_) this.stdout_(...object);
|
|
|
|
}
|
|
|
|
|
2017-10-14 20:03:23 +02:00
|
|
|
setForceRender(fn) {
|
|
|
|
this.forceRender_ = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
async forceRender() {
|
|
|
|
if (this.forceRender_) await this.forceRender_();
|
|
|
|
}
|
|
|
|
|
2017-10-07 19:07:38 +02:00
|
|
|
setPrompt(fn) {
|
|
|
|
this.prompt_ = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
async prompt(message, options = null) {
|
|
|
|
if (!this.prompt_) throw new Error('Prompt is undefined');
|
|
|
|
if (!options) options = {};
|
|
|
|
if (!options.type) options.type = 'boolean';
|
|
|
|
if (!options.answers) options.answers = [_('Y'), _('n')];
|
|
|
|
return await this.prompt_(message, options);
|
|
|
|
}
|
|
|
|
|
2017-08-04 19:51:01 +02:00
|
|
|
metadata() {
|
|
|
|
return {
|
|
|
|
name: this.name(),
|
|
|
|
usage: this.usage(),
|
|
|
|
options: this.options(),
|
|
|
|
hidden: this.hidden(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-10-09 21:57:00 +02:00
|
|
|
logger() {
|
|
|
|
return reg.logger();
|
|
|
|
}
|
|
|
|
|
2017-07-10 22:03:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { BaseCommand };
|