From d4771249731b1403b397a0109ef5cb7e5cd476f1 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Tue, 16 Jun 2020 09:40:12 +0200 Subject: [PATCH] more dynamic interactive source register --- .../Interactive/InteractiveSubSource.ts | 7 +++++-- lib/Sources/Interactive/PresetSubSource.ts | 8 +------- lib/Sources/Interactive/StringSubSource.ts | 10 +--------- lib/Sources/Interactive/index.ts | 11 ++++++++++ lib/Sources/InteractiveSource.ts | 20 ++++++++----------- 5 files changed, 26 insertions(+), 30 deletions(-) create mode 100644 lib/Sources/Interactive/index.ts diff --git a/lib/Sources/Interactive/InteractiveSubSource.ts b/lib/Sources/Interactive/InteractiveSubSource.ts index 02c9830..7ece10b 100644 --- a/lib/Sources/Interactive/InteractiveSubSource.ts +++ b/lib/Sources/Interactive/InteractiveSubSource.ts @@ -15,9 +15,12 @@ export abstract class InteractiveSubSource { this.opt = opt; } - public async parse ():Promise { - if (this.condition ()) + public async parse ():Promise { + if (this.condition ()) { await this.run (); + return true; + } + return false; } protected get_message (): string { diff --git a/lib/Sources/Interactive/PresetSubSource.ts b/lib/Sources/Interactive/PresetSubSource.ts index 18cc052..e502300 100644 --- a/lib/Sources/Interactive/PresetSubSource.ts +++ b/lib/Sources/Interactive/PresetSubSource.ts @@ -4,13 +4,7 @@ import { InteractiveSubSource } from './InteractiveSubSource'; export class PresetSubSource extends InteractiveSubSource { protected condition ():boolean { - return [ - 'string', - 'file', - 'folder', - 'path' - ].includes (this.val.type_validation.option_type) - && typeof (this.opt as StringOptionConfig).preset !== 'undefined'; + return typeof (this.opt as StringOptionConfig).preset !== 'undefined'; } protected async run ():Promise { diff --git a/lib/Sources/Interactive/StringSubSource.ts b/lib/Sources/Interactive/StringSubSource.ts index cf16f66..4bb1433 100644 --- a/lib/Sources/Interactive/StringSubSource.ts +++ b/lib/Sources/Interactive/StringSubSource.ts @@ -1,17 +1,9 @@ import { Input } from 'enquirer'; -import { StringOptionConfig } from '../../SubConfigs'; import { InteractiveSubSource } from './InteractiveSubSource'; export class StringSubSource extends InteractiveSubSource { protected condition ():boolean { - return [ - 'string', - 'file', - 'folder', - 'path', - 'number' - ].includes (this.val.type_validation.option_type) - && typeof (this.opt as StringOptionConfig).preset === 'undefined'; + return true; } protected async run ():Promise { diff --git a/lib/Sources/Interactive/index.ts b/lib/Sources/Interactive/index.ts new file mode 100644 index 0000000..dba59a3 --- /dev/null +++ b/lib/Sources/Interactive/index.ts @@ -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 +]; diff --git a/lib/Sources/InteractiveSource.ts b/lib/Sources/InteractiveSource.ts index cb16756..9ed5313 100644 --- a/lib/Sources/InteractiveSource.ts +++ b/lib/Sources/InteractiveSource.ts @@ -10,10 +10,7 @@ import { ErrorCallback } from '../ErrorCallback'; import { Option, OptionValue } from '../Option'; import { OptionSource } from './OptionSource'; -import { ArraySubSource } from './Interactive/ArraySubSource'; -import { BooleanSubSource } from './Interactive/BooleanSubSource'; -import { PresetSubSource } from './Interactive/PresetSubSource'; -import { StringSubSource } from './Interactive/StringSubSource'; +import { sources } from './Interactive'; export class InteractiveSource extends OptionSource { private _exit_on_interrupt: boolean; @@ -29,14 +26,13 @@ export class InteractiveSource extends OptionSource { private async prompt (opt: Option, val:OptionValue): Promise { if (val.filled) return; - await new StringSubSource (val, opt) - .parse (); - await new PresetSubSource (val, opt) - .parse (); - await new BooleanSubSource (val, opt) - .parse (); - await new ArraySubSource (val, opt) - .parse (); + + for (const src of sources) { + // eslint-disable-next-line no-await-in-loop + if (await new src (val, opt) + .parse ()) + break; + } } public async parse (opt: Option, val:OptionValue): Promise {