path selector

This commit is contained in:
Timo Hocker 2020-06-16 12:52:41 +02:00
parent d477124973
commit f372e1ea17
6 changed files with 65 additions and 10 deletions

View File

@ -20,14 +20,16 @@ const {
} = require ('./dist/lib/index.js');
(async () => {
const str = await new StringOption ({ name: 'str' })
.parse ();
const bool = await new BooleanOption ({ name: 'bool' })
.parse ();
const num = await new NumberOption ({ name: 'num' })
.parse ();
const arr = await new ArrayOption ({ name: 'arr' })
.parse ();
/*
* const str = await new StringOption ({ name: 'str' })
*.parse ();
*const bool = await new BooleanOption ({ name: 'bool' })
*.parse ();
*const num = await new NumberOption ({ name: 'num' })
*.parse ();
*const arr = await new ArrayOption ({ name: 'arr' })
*.parse ();
*/
const fld = await new FolderOption ({ name: 'fld' })
.parse ();

View File

@ -17,7 +17,10 @@ export abstract class InteractiveSubSource {
public async parse ():Promise<boolean> {
if (this.condition ()) {
await this.run ();
await this.run ()
.catch ((e) => {
console.log (e);
});
return true;
}
return false;

View File

@ -0,0 +1,25 @@
import { Prompt } from 'enquirer';
export class PathPrompt extends Prompt {
private _index = 0;
private _value = '';
public constructor (options:Record<string, unknown> = {}) {
super (options);
}
public up (): void {
this._index--;
this.render ();
}
public down (): void {
this._index++;
this.render ();
}
public render (): void {
this.clear ();
this.write (`${this.state.message}: ${this._value}`);
}
}

View File

@ -0,0 +1,23 @@
import { InteractiveSubSource } from './InteractiveSubSource';
import { PathPrompt } from './PathCustomPrompt';
export class PathSubSource extends InteractiveSubSource {
protected condition ():boolean {
return [
'path',
'file',
'folder'
].includes (this.val.type_validation.option_type);
}
protected async run (): Promise<void> {
await this.val.assign_arg (
this.opt,
await new PathPrompt ({
message: this.get_message (),
default: this.opt.default
})
.run ()
);
}
}

View File

@ -2,10 +2,12 @@ import { ArraySubSource } from './ArraySubSource';
import { BooleanSubSource } from './BooleanSubSource';
import { PresetSubSource } from './PresetSubSource';
import { StringSubSource } from './StringSubSource';
import { PathSubSource } from './PathSubSource';
export const sources = [
ArraySubSource,
BooleanSubSource,
PresetSubSource,
PathSubSource,
StringSubSource
];

View File

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es6",
"module": "commonjs",
"outDir": "./dist",
"rootDir": "./",