refactoring

This commit is contained in:
2020-05-09 19:51:43 +02:00
parent 0ff924d2dd
commit 469afeb777
15 changed files with 431 additions and 240 deletions

55
lib/Sources/ArgSource.ts Normal file
View File

@ -0,0 +1,55 @@
/* eslint-disable no-console */
/* eslint-disable no-process-exit */
import yargs, { Options } from 'yargs';
import { OptionProcess } from '../Types';
import { OptionSource } from './OptionSource';
export class ArgSource extends OptionSource {
public async parse (options: OptionProcess[]): Promise<void> {
const yargs_config: Record<string, Options> = {
quiet: {
alias: 'q',
default: false,
type: 'boolean',
describe: 'do not ask for options interactively'
},
help: {
alias: 'h',
default: false,
type: 'boolean',
describe: ''
}
};
for (const opt of options) {
yargs_config[opt.name] = {
alias: opt.alias,
default: opt.default,
type: opt.type_validation.string_type,
describe: opt.description
};
}
const argv = yargs.options (yargs_config)
.parse ();
if (argv.help) {
yargs.options (yargs_config)
.showHelp ();
process.exit (0);
}
await Promise.all (options.map ((opt) => {
if (typeof argv[opt.name] !== 'undefined')
return this.assign_arg (opt, argv[opt.name]);
return Promise.resolve ();
}));
if (argv.quiet) {
const missing = options.filter ((o) => !o.filled && o.required)
.map ((o) => o.name);
if (missing.length > 0) {
console.error ('missing arguments:');
console.error (missing.join (', '));
process.exit (0);
}
}
}
}

16
lib/Sources/EnvSource.ts Normal file
View File

@ -0,0 +1,16 @@
/* eslint-disable no-process-env */
import { OptionProcess } from '../Types';
import { OptionSource } from './OptionSource';
export class EnvSource extends OptionSource {
public async parse (options: OptionProcess[]): Promise<void> {
await Promise.all (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 ();
}));
}
}

View File

@ -0,0 +1,50 @@
/* 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');
}
}
}
}

View File

@ -0,0 +1,18 @@
import { OptionProcess } from '../Types';
export abstract class OptionSource {
public abstract async parse(opt: OptionProcess[]): Promise<void>;
protected async assign_arg (
opt: OptionProcess,
value: unknown
): Promise<void> {
try {
opt.value = await opt.type_validation.to_type (value);
opt.filled = true;
}
catch (e) {
// could not assing
}
}
}