more types

This commit is contained in:
Timo Hocker
2020-05-05 12:48:36 +02:00
parent 0e12cd0fb5
commit 8dd3d75a7c
3 changed files with 109 additions and 8 deletions

View File

@ -1,22 +1,30 @@
/* eslint-disable complexity */
/* eslint-disable max-statements */
/* eslint-disable no-process-env */
import { Persistent } from '@scode/modelling';
import fs from 'fs-extra';
enum OptionType {
string = 'string',
number = 'number',
boolean = 'boolean'
boolean = 'boolean',
file = 'file',
folder = 'folder',
path = 'path'
}
interface Option {
name: string;
type: OptionType;
required?: boolean;
default: unknown;
alias: string;
env: string;
default?: unknown;
alias?: string;
env?: string;
}
interface OptionProcess extends Option {
filled: boolean;
value?: unknown;
}
export class InteractiveOptions extends Persistent {
@ -42,11 +50,66 @@ export class InteractiveOptions extends Persistent {
await this.get_interactive_options ();
}
private get_env_options (): void {
private get unfilled (): Array<OptionProcess> {
return this.options.filter ((o) => !o.filled);
}
private get_args_options (): void {
private async assign_arg (opt: OptionProcess, value: unknown): Promise<void> {
if (opt.type === OptionType.string) {
opt.value = value;
opt.filled = true;
return;
}
if (opt.type === OptionType.number) {
const as_num = parseInt (value);
const is_num = !isNaN (as_num);
if (is_num) {
opt.value = as_num;
opt.filled = true;
}
return;
}
if (opt.type === OptionType.boolean) {
const is_boo = (/^(?:true|false)$/ui).test (value);
if (is_boo) {
const as_boo = (/true/ui).test (value);
opt.value = as_boo;
opt.filled = true;
}
return;
}
if (
opt.type === OptionType.path
|| opt.type === OptionType.file
|| opt.type === OptionType.folder
) {
if (!await fs.pathExists (value))
return;
if (opt.type === OptionType.path) {
opt.value = value;
opt.filled = true;
return;
}
const stat = await fs.stat (value);
if (stat.isDirectory () === (opt.type === OptionType.folder)) {
opt.value = value;
opt.filled = true;
}
}
}
private async get_env_options (): void {
await Promise.all (this.options.map ((opt) => {
if (
typeof opt.env !== 'undefined'
&& typeof process.env[opt.env] !== 'undefined'
)
return this.assign_arg (opt, process.env[opt.env]);
return Promise.resolve ();
}));
}
private async get_args_options (): void {
}