more dynamic interactive source register

This commit is contained in:
Timo Hocker 2020-06-16 09:40:12 +02:00
parent f7c03f82f1
commit d477124973
5 changed files with 26 additions and 30 deletions

View File

@ -15,9 +15,12 @@ export abstract class InteractiveSubSource {
this.opt = opt; this.opt = opt;
} }
public async parse ():Promise<void> { public async parse ():Promise<boolean> {
if (this.condition ()) if (this.condition ()) {
await this.run (); await this.run ();
return true;
}
return false;
} }
protected get_message (): string { protected get_message (): string {

View File

@ -4,13 +4,7 @@ import { InteractiveSubSource } from './InteractiveSubSource';
export class PresetSubSource extends InteractiveSubSource { export class PresetSubSource extends InteractiveSubSource {
protected condition ():boolean { protected condition ():boolean {
return [ return typeof (this.opt as StringOptionConfig).preset !== 'undefined';
'string',
'file',
'folder',
'path'
].includes (this.val.type_validation.option_type)
&& typeof (this.opt as StringOptionConfig).preset !== 'undefined';
} }
protected async run ():Promise<void> { protected async run ():Promise<void> {

View File

@ -1,17 +1,9 @@
import { Input } from 'enquirer'; import { Input } from 'enquirer';
import { StringOptionConfig } from '../../SubConfigs';
import { InteractiveSubSource } from './InteractiveSubSource'; import { InteractiveSubSource } from './InteractiveSubSource';
export class StringSubSource extends InteractiveSubSource { export class StringSubSource extends InteractiveSubSource {
protected condition ():boolean { protected condition ():boolean {
return [ return true;
'string',
'file',
'folder',
'path',
'number'
].includes (this.val.type_validation.option_type)
&& typeof (this.opt as StringOptionConfig).preset === 'undefined';
} }
protected async run ():Promise<void> { protected async run ():Promise<void> {

View File

@ -0,0 +1,11 @@
import { ArraySubSource } from './ArraySubSource';
import { BooleanSubSource } from './BooleanSubSource';
import { PresetSubSource } from './PresetSubSource';
import { StringSubSource } from './StringSubSource';
export const sources = [
ArraySubSource,
BooleanSubSource,
PresetSubSource,
StringSubSource
];

View File

@ -10,10 +10,7 @@
import { ErrorCallback } from '../ErrorCallback'; import { ErrorCallback } from '../ErrorCallback';
import { Option, OptionValue } from '../Option'; import { Option, OptionValue } from '../Option';
import { OptionSource } from './OptionSource'; import { OptionSource } from './OptionSource';
import { ArraySubSource } from './Interactive/ArraySubSource'; import { sources } from './Interactive';
import { BooleanSubSource } from './Interactive/BooleanSubSource';
import { PresetSubSource } from './Interactive/PresetSubSource';
import { StringSubSource } from './Interactive/StringSubSource';
export class InteractiveSource extends OptionSource { export class InteractiveSource extends OptionSource {
private _exit_on_interrupt: boolean; private _exit_on_interrupt: boolean;
@ -29,14 +26,13 @@ export class InteractiveSource extends OptionSource {
private async prompt (opt: Option, val:OptionValue): Promise<void> { private async prompt (opt: Option, val:OptionValue): Promise<void> {
if (val.filled) if (val.filled)
return; return;
await new StringSubSource (val, opt)
.parse (); for (const src of sources) {
await new PresetSubSource (val, opt) // eslint-disable-next-line no-await-in-loop
.parse (); if (await new src (val, opt)
await new BooleanSubSource (val, opt) .parse ())
.parse (); break;
await new ArraySubSource (val, opt) }
.parse ();
} }
public async parse (opt: Option, val:OptionValue): Promise<void> { public async parse (opt: Option, val:OptionValue): Promise<void> {