split interactive source
This commit is contained in:
parent
089519844f
commit
f7c03f82f1
@ -34,6 +34,20 @@ class OptionValue {
|
||||
public constructor (type_validation: TypeValidation) {
|
||||
this.type_validation = type_validation;
|
||||
}
|
||||
|
||||
public async assign_arg (
|
||||
opt: Option,
|
||||
value: unknown
|
||||
): Promise<void> {
|
||||
try {
|
||||
this.value = await this.type_validation.to_type (value);
|
||||
this.filled = true;
|
||||
}
|
||||
catch (e) {
|
||||
if (typeof opt.error_callback !== 'undefined')
|
||||
opt.error_callback (opt.name, value, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { Option, OptionValue };
|
||||
|
@ -45,6 +45,6 @@ export class ArgSource extends OptionSource {
|
||||
)
|
||||
return;
|
||||
|
||||
await this.assign_arg (opt, val, argv[opt.name]);
|
||||
await val.assign_arg (opt, argv[opt.name]);
|
||||
}
|
||||
}
|
||||
|
@ -64,6 +64,6 @@ export class ConfigSource extends OptionSource {
|
||||
const keys = Object.keys (data);
|
||||
|
||||
if (keys.includes (opt.name))
|
||||
await this.assign_arg (opt, val, data[opt.name]);
|
||||
await val.assign_arg (opt, data[opt.name]);
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,6 @@ export class EnvSource extends OptionSource {
|
||||
return;
|
||||
}
|
||||
|
||||
await this.assign_arg (opt, val, process.env[opt.env]);
|
||||
await val.assign_arg (opt, process.env[opt.env]);
|
||||
}
|
||||
}
|
||||
|
19
lib/Sources/Interactive/ArraySubSource.ts
Normal file
19
lib/Sources/Interactive/ArraySubSource.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { List } from 'enquirer';
|
||||
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||
|
||||
export class ArraySubSource extends InteractiveSubSource {
|
||||
protected condition ():boolean {
|
||||
return this.val.type_validation.option_type === 'array';
|
||||
}
|
||||
|
||||
protected async run ():Promise<void> {
|
||||
await this.val.assign_arg (
|
||||
this.opt,
|
||||
await new List ({
|
||||
message: this.get_message (),
|
||||
default: this.opt.default
|
||||
})
|
||||
.run ()
|
||||
);
|
||||
}
|
||||
}
|
19
lib/Sources/Interactive/BooleanSubSource.ts
Normal file
19
lib/Sources/Interactive/BooleanSubSource.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { Confirm } from 'enquirer';
|
||||
import { InteractiveSubSource } from './InteractiveSubSource';
|
||||
|
||||
export class BooleanSubSource extends InteractiveSubSource {
|
||||
protected condition ():boolean {
|
||||
return this.val.type_validation.option_type === 'boolean';
|
||||
}
|
||||
|
||||
protected async run ():Promise<void> {
|
||||
await this.val.assign_arg (
|
||||
this.opt,
|
||||
await new Confirm ({
|
||||
message: this.get_message (),
|
||||
default: this.opt.default
|
||||
})
|
||||
.run ()
|
||||
);
|
||||
}
|
||||
}
|
28
lib/Sources/Interactive/InteractiveSubSource.ts
Normal file
28
lib/Sources/Interactive/InteractiveSubSource.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { OptionValue, Option } from '../../Option';
|
||||
|
||||
export abstract class InteractiveSubSource {
|
||||
protected val: OptionValue;
|
||||
protected opt: Option;
|
||||
|
||||
protected abstract condition():boolean;
|
||||
protected abstract async run():Promise<void>;
|
||||
|
||||
public constructor (
|
||||
val:OptionValue,
|
||||
opt:Option
|
||||
) {
|
||||
this.val = val;
|
||||
this.opt = opt;
|
||||
}
|
||||
|
||||
public async parse ():Promise<void> {
|
||||
if (this.condition ())
|
||||
await this.run ();
|
||||
}
|
||||
|
||||
protected get_message (): string {
|
||||
return typeof this.opt.message === 'undefined'
|
||||
? `input ${this.opt.name}`
|
||||
: this.opt.message;
|
||||
}
|
||||
}
|
28
lib/Sources/Interactive/PresetSubSource.ts
Normal file
28
lib/Sources/Interactive/PresetSubSource.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { AutoComplete } from 'enquirer';
|
||||
import { StringOptionConfig } from '../../SubConfigs';
|
||||
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';
|
||||
}
|
||||
|
||||
protected async run ():Promise<void> {
|
||||
await this.val.assign_arg (
|
||||
this.opt,
|
||||
await new AutoComplete ({
|
||||
message: this.get_message (),
|
||||
default: this.opt.default,
|
||||
choices: (this.opt as StringOptionConfig).preset,
|
||||
limit: 10
|
||||
})
|
||||
.run ()
|
||||
);
|
||||
}
|
||||
}
|
27
lib/Sources/Interactive/StringSubSource.ts
Normal file
27
lib/Sources/Interactive/StringSubSource.ts
Normal file
@ -0,0 +1,27 @@
|
||||
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';
|
||||
}
|
||||
|
||||
protected async run ():Promise<void> {
|
||||
await this.val.assign_arg (
|
||||
this.opt,
|
||||
await new Input ({
|
||||
message: this.get_message (),
|
||||
default: this.opt.default
|
||||
})
|
||||
.run ()
|
||||
);
|
||||
}
|
||||
}
|
@ -7,11 +7,13 @@
|
||||
|
||||
/* eslint-disable no-console */
|
||||
/* eslint-disable no-process-exit */
|
||||
import { Confirm, Input, List, AutoComplete } from 'enquirer';
|
||||
import { ErrorCallback } from '../ErrorCallback';
|
||||
import { Option, OptionValue } from '../Option';
|
||||
import { StringOptionConfig } from '../SubConfigs';
|
||||
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;
|
||||
@ -24,62 +26,17 @@ export class InteractiveSource extends OptionSource {
|
||||
this._exit_on_interrupt = exit_on_interrupt;
|
||||
}
|
||||
|
||||
private get_message (opt: Option): string {
|
||||
return typeof opt.message === 'undefined'
|
||||
? `input ${opt.name}`
|
||||
: opt.message;
|
||||
}
|
||||
|
||||
private async prompt (opt: Option, val:OptionValue): Promise<void> {
|
||||
if (val.filled)
|
||||
return;
|
||||
let value = null;
|
||||
const { option_type } = val.type_validation;
|
||||
const { preset } = opt as StringOptionConfig;
|
||||
if (
|
||||
option_type === 'string'
|
||||
|| option_type === 'file'
|
||||
|| option_type === 'folder'
|
||||
|| option_type === 'path'
|
||||
|| option_type === 'number'
|
||||
) {
|
||||
if (typeof preset === 'undefined') {
|
||||
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,
|
||||
choices: preset,
|
||||
limit: 10
|
||||
})
|
||||
.run ();
|
||||
}
|
||||
}
|
||||
if (
|
||||
option_type === 'boolean'
|
||||
) {
|
||||
value = await new Confirm ({
|
||||
message: this.get_message (opt),
|
||||
default: opt.default
|
||||
})
|
||||
.run ();
|
||||
}
|
||||
if (option_type === 'array') {
|
||||
value = await new List ({
|
||||
message: this.get_message (opt),
|
||||
default: opt.default
|
||||
})
|
||||
.run ();
|
||||
}
|
||||
if (value === null)
|
||||
return;
|
||||
|
||||
await this.assign_arg (opt, val, value);
|
||||
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<void> {
|
||||
|
@ -16,19 +16,4 @@ export abstract class OptionSource {
|
||||
public constructor (error_callback?: ErrorCallback) {
|
||||
this.error_callback = error_callback;
|
||||
}
|
||||
|
||||
protected async assign_arg (
|
||||
opt: Option,
|
||||
val: OptionValue,
|
||||
value: unknown
|
||||
): Promise<void> {
|
||||
try {
|
||||
val.value = await val.type_validation.to_type (value);
|
||||
val.filled = true;
|
||||
}
|
||||
catch (e) {
|
||||
if (typeof this.error_callback !== 'undefined')
|
||||
this.error_callback (opt.name, value, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user