console-app/lib/Sources/InteractiveSource.ts

99 lines
2.6 KiB
TypeScript
Raw Normal View History

2020-05-15 16:53:45 +02:00
/*
* 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 <timo@scode.ovh>, May 2020
*/
2020-05-09 19:51:43 +02:00
/* eslint-disable no-console */
/* eslint-disable no-process-exit */
2020-05-18 12:34:44 +02:00
import { Confirm, Input, List, AutoComplete } from 'enquirer';
2020-06-09 13:13:27 +02:00
import { ErrorCallback } from '../ErrorCallback';
2020-06-09 21:03:10 +02:00
import { Option, OptionValue } from '../Option';
import { StringOptionConfig } from '../SubConfigs';
2020-05-09 19:51:43 +02:00
import { OptionSource } from './OptionSource';
export class InteractiveSource extends OptionSource {
2020-05-22 11:50:07 +02:00
private _exit_on_interrupt: boolean;
public constructor (
2020-05-28 18:54:34 +02:00
exit_on_interrupt: boolean,
error_callback?:ErrorCallback
) {
super (error_callback);
2020-05-22 11:50:07 +02:00
this._exit_on_interrupt = exit_on_interrupt;
}
2020-05-09 21:30:37 +02:00
private get_message (opt: Option): string {
return typeof opt.message === 'undefined'
? `input ${opt.name}`
: opt.message;
}
2020-06-09 21:03:10 +02:00
private async prompt (opt: Option, val:OptionValue): Promise<void> {
if (val.filled)
2020-05-09 19:51:43 +02:00
return;
2020-05-09 21:30:37 +02:00
let value = null;
2020-06-09 21:03:10 +02:00
const { option_type } = val.type_validation;
const { preset } = opt as StringOptionConfig;
2020-05-09 19:51:43 +02:00
if (
2020-06-09 21:03:10 +02:00
option_type === 'string'
|| option_type === 'file'
|| option_type === 'folder'
|| option_type === 'path'
|| option_type === 'number'
2020-05-09 19:51:43 +02:00
) {
2020-06-09 21:03:10 +02:00
if (typeof preset === 'undefined') {
2020-05-18 12:34:44 +02:00
value = await new Input ({
message: this.get_message (opt),
default: opt.default
})
.run ();
}
else {
value = await new AutoComplete ({
message: this.get_message (opt),
default: opt.default,
2020-06-09 21:03:10 +02:00
choices: preset,
2020-05-18 13:08:05 +02:00
limit: 10
2020-05-18 12:34:44 +02:00
})
.run ();
}
2020-05-09 19:51:43 +02:00
}
if (
2020-06-09 21:03:10 +02:00
option_type === 'boolean'
2020-05-09 19:51:43 +02:00
) {
2020-05-09 21:30:37 +02:00
value = await new Confirm ({
message: this.get_message (opt),
2020-05-09 19:51:43 +02:00
default: opt.default
})
.run ();
}
2020-06-09 21:03:10 +02:00
if (option_type === 'array') {
2020-05-09 21:30:37 +02:00
value = await new List ({
message: this.get_message (opt),
default: opt.default
})
.run ();
}
if (value === null)
return;
2020-06-09 21:03:10 +02:00
await this.assign_arg (opt, val, value);
2020-05-09 19:51:43 +02:00
}
2020-06-09 21:03:10 +02:00
public async parse (opt: Option, val:OptionValue): Promise<void> {
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');
2020-05-09 19:51:43 +02:00
}
}
}