complete type contructs

This commit is contained in:
2020-06-09 13:58:29 +02:00
parent 3f0b9fad79
commit 742d77d29f
10 changed files with 182 additions and 104 deletions

View File

@ -5,6 +5,8 @@
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import { TypeValidation } from './TypeValidation/TypeValidation';
interface SourceConfig {
console?: boolean,
configs?: string[],
@ -17,19 +19,20 @@ interface Option {
required?: boolean;
default?: unknown;
sources?: SourceConfig;
/*
* alias?: string;
* env?: string;
* message?: string;
* preset?: unknown[];
* error?: string;
*/
alias?: string;
env?: string;
message?: string;
error?: string;
}
interface OptionValue {
filled: boolean;
value?: unknown;
class OptionValue {
public filled = false;
public value?: unknown;
public readonly type_validation: TypeValidation;
public constructor (type_validation: TypeValidation) {
this.type_validation = type_validation;
}
}
export { Option, OptionValue };

View File

@ -0,0 +1,8 @@
import { TypeValidation } from '../TypeValidation/TypeValidation';
import { BaseOption } from './BaseOption';
export class ArrayOption extends BaseOption {
protected get validation ():TypeValidation {
return new TypeValidation ('array');
}
}

View File

@ -1,5 +1,5 @@
import { OptionSource } from '../Sources/OptionSource';
import { Option } from '../Option';
import { Option, OptionValue } from '../Option';
import { EnvSource } from '../Sources/EnvSource';
import { ErrorCallback } from '../ErrorCallback';
import { ArgSource } from '../Sources/ArgSource';
@ -37,9 +37,15 @@ export abstract class BaseOption<T> {
protected abstract get validation(): TypeValidation;
public async parse(): Promise<T> {
for (let source of this.sources) {
source.parse(this._config);
}
};
public async parse (): Promise<T> {
const val = new OptionValue (this.validation);
for (const source of this.sources)
// eslint-disable-next-line no-await-in-loop
await source.parse (this._config, val);
if (!val.assinged)
return this._config.default;
return val.value;
}
}

View File

@ -0,0 +1,8 @@
import { TypeValidation } from '../TypeValidation/TypeValidation';
import { BaseOption } from './BaseOption';
export class BooleanOption extends BaseOption {
protected get validation ():TypeValidation {
return new TypeValidation ('boolean');
}
}

View File

@ -0,0 +1,8 @@
import { PathType } from '../TypeValidation/PathType';
import { StringOption } from './StringOption';
export class FileOption extends StringOption {
protected get validation ():TypeValidation {
return new PathType ('file');
}
}

View File

@ -0,0 +1,8 @@
import { PathType } from '../TypeValidation/PathType';
import { StringOption } from './StringOption';
export class FolderOption extends StringOption {
protected get validation ():TypeValidation {
return new PathType ('folder');
}
}

View File

@ -0,0 +1,8 @@
import { TypeValidation } from '../TypeValidation/TypeValidation';
import { BaseOption } from './BaseOption';
export class NumberOption extends BaseOption {
protected get validation ():TypeValidation {
return new TypeValidation ('number');
}
}

View File

@ -0,0 +1,8 @@
import { PathType } from '../TypeValidation/PathType';
import { StringOption } from './StringOption';
export class PathOption extends StringOption {
protected get validation ():TypeValidation {
return new PathType ('path');
}
}

View File

@ -1,14 +1,18 @@
import { TypeValidation } from '../TypeValidation/TypeValidation';
import { Option } from '../Option';
import { BaseOption } from './BaseOption';
export class StringOption extends BaseOption<string> {
private _config: Option;
interface StringOptionConfig extends Option {
preset?: string[]
}
public constructor (config: Option) {
this._config = config;
export class StringOption extends BaseOption {
protected get validation ():TypeValidation {
return new TypeValidation ('string');
}
public async parse (): Promise<string> {
// eslint-disable-next-line no-useless-constructor
public constructor (config: StringOptionConfig) {
super (config);
}
}