console-app/lib/Sources/InteractiveSource.ts
2020-05-09 19:51:43 +02:00

51 lines
1.3 KiB
TypeScript

/* eslint-disable no-console */
/* eslint-disable no-process-exit */
import { Confirm, Input } from 'enquirer';
import { OptionProcess } from '../Types';
import { OptionSource } from './OptionSource';
export class InteractiveSource extends OptionSource {
private async prompt (opt: OptionProcess): Promise<void> {
if (opt.filled)
return;
if (
opt.type === 'string'
|| opt.type === 'file'
|| opt.type === 'folder'
|| opt.type === 'path'
|| opt.type === 'number'
) {
const value = await new Input ({
message: typeof opt.message === 'undefined'
? `input ${opt.name}`
: opt.message,
default: opt.default
})
.run ();
await this.assign_arg (opt, value);
return;
}
if (
opt.type === 'boolean'
) {
const value = await new Confirm ({
message: opt.message,
default: opt.default
})
.run ();
await this.assign_arg (opt, value);
}
}
public async parse (options: OptionProcess[]): Promise<void> {
for (const opt of options) {
while (!opt.filled) {
// eslint-disable-next-line no-await-in-loop
await this.prompt (opt);
if (!opt.filled)
console.log ('input was invalid');
}
}
}
}