@ashnov/hephaestus-cli
Version:
Your AI powered CLI-based coding assistant. Powered by OpenAI davinci models.
151 lines (150 loc) • 5.06 kB
JavaScript
var _a;
import { CommandError } from './errors/CommandError.js';
export class CommandValidator {
constructor() { }
static validateCommand() {
switch (this.command) {
case undefined:
case 'help':
case '-h':
return this.validateHelpCommand();
case 'about':
case '-ab':
return this.validateAboutCommand();
case 'version':
case '-v':
return this.validateVersionCommand();
case 'answer':
case '-a':
return this.validateAnswerCommand();
case 'configure':
case '-c':
return this.validateConfigureCommand();
case 'config-info':
case '-ci':
return this.validateConfigInfoCommand();
default:
throw new CommandError('UNKNOWN_COMMAND', undefined, this._args[0]);
}
}
static validateHelpCommand() {
if (this._args.length > 1) {
throw new CommandError('ENCOUNTER_EXTRA_ARGS', 'help', this._args[1]);
}
const executable = {
command: 'help',
};
return executable;
}
static validateAboutCommand() {
if (this._args.length > 2) {
throw new CommandError('ENCOUNTER_EXTRA_ARGS', 'about', this._args[2]);
}
const executable = {
command: 'about',
};
if (this._args[1]) {
if (this.HELP_COMMAND_TYPES.includes(this._args[1])) {
executable.describe = true;
}
else {
throw new CommandError('UNKNOWN_COMMAND', 'about', this._args[1]);
}
}
return executable;
}
static validateVersionCommand() {
if (this._args.length > 2) {
throw new CommandError('ENCOUNTER_EXTRA_ARGS', 'version', this._args[2]);
}
const executable = {
command: 'version',
};
if (this._args[1]) {
if (this.HELP_COMMAND_TYPES.includes(this._args[1])) {
executable.describe = true;
}
else {
throw new CommandError('UNKNOWN_COMMAND', 'version', this._args[1]);
}
}
return executable;
}
static validateAnswerCommand() {
var _b;
if (this._args.length > 3) {
throw new CommandError('ENCOUNTER_EXTRA_ARGS', 'about', this._args[3]);
}
let executable = {
command: 'answer',
};
const query = (_b = this._args[1]) === null || _b === void 0 ? void 0 : _b.trim();
const flag = this._args[2];
const flagOptions = ['--text', '-t', '--code', '-c'];
if (this.HELP_COMMAND_TYPES.includes(query)) {
if (flag) {
throw new CommandError('ENCOUNTER_EXTRA_ARGS', 'answer', flag);
}
executable.describe = true;
return executable;
}
if (query) {
if (query.slice(0, 3) !== '-q=' &&
!this.HELP_COMMAND_TYPES.includes(query)) {
throw new CommandError('OPTION_NOT_RECOGNIZED', 'answer', query);
}
executable.data = {
query: query.slice(3),
};
}
else {
throw new CommandError('EMPTY_QUERY', 'answer');
}
if (flag) {
if (!flagOptions.includes(flag)) {
throw new CommandError('INVALID_FLAG', 'answer', flag);
}
executable.responseType =
flag === '-t' || flag === '--text' ? 'text' : 'code';
}
return executable;
}
static validateConfigureCommand() {
if (this._args.length > 2) {
throw new CommandError('ENCOUNTER_EXTRA_ARGS', 'configure', this._args[2]);
}
const executable = {
command: 'configure',
};
if (this._args[1]) {
if (this.HELP_COMMAND_TYPES.includes(this._args[1])) {
executable.describe = true;
}
else {
throw new CommandError('UNKNOWN_COMMAND', 'configure', this._args[1]);
}
}
return executable;
}
static validateConfigInfoCommand() {
if (this._args.length > 2) {
throw new CommandError('ENCOUNTER_EXTRA_ARGS', 'config-info', this._args[2]);
}
const executable = {
command: 'config-info',
};
if (this._args[1]) {
if (this.HELP_COMMAND_TYPES.includes(this._args[1])) {
executable.describe = true;
}
else {
throw new CommandError('UNKNOWN_COMMAND', 'config-info', this._args[1]);
}
}
return executable;
}
}
_a = CommandValidator;
CommandValidator.HELP_COMMAND_TYPES = ['help', '--help', '-h'];
CommandValidator._args = process.argv.slice(2);
CommandValidator.command = _a._args[0];