/* * Copyright (C) Sapphirecode - All Rights Reserved * This file is part of console-app which is released under MIT. * See file 'LICENSE' for full license details. * Created by Timo Hocker , May 2020 */ /* eslint-disable no-console */ /* eslint-disable no-process-exit */ 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'; export class InteractiveSource extends OptionSource { private _exit_on_interrupt: boolean; public constructor ( exit_on_interrupt: boolean, error_callback?:ErrorCallback ) { super (error_callback); this._exit_on_interrupt = exit_on_interrupt; } 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 (); } public async parse (opt: Option, val:OptionValue): Promise { while (!val.filled) { // eslint-disable-next-line no-await-in-loop await this.prompt (opt, val) .catch ((e) => { if (this._exit_on_interrupt) process.exit (0); throw e; }); if (!val.filled) console.log (opt.error || 'input was invalid'); } } }