From f372e1ea1708333103df339dd488b32a97479dc3 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Tue, 16 Jun 2020 12:52:41 +0200 Subject: [PATCH] path selector --- AppTest.js | 18 +++++++------ .../Interactive/InteractiveSubSource.ts | 5 +++- lib/Sources/Interactive/PathCustomPrompt.ts | 25 +++++++++++++++++++ lib/Sources/Interactive/PathSubSource.ts | 23 +++++++++++++++++ lib/Sources/Interactive/index.ts | 2 ++ tsconfig.json | 2 +- 6 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 lib/Sources/Interactive/PathCustomPrompt.ts create mode 100644 lib/Sources/Interactive/PathSubSource.ts diff --git a/AppTest.js b/AppTest.js index ae8507a..bdb2f01 100644 --- a/AppTest.js +++ b/AppTest.js @@ -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 (); diff --git a/lib/Sources/Interactive/InteractiveSubSource.ts b/lib/Sources/Interactive/InteractiveSubSource.ts index 7ece10b..627b87d 100644 --- a/lib/Sources/Interactive/InteractiveSubSource.ts +++ b/lib/Sources/Interactive/InteractiveSubSource.ts @@ -17,7 +17,10 @@ export abstract class InteractiveSubSource { public async parse ():Promise { if (this.condition ()) { - await this.run (); + await this.run () + .catch ((e) => { + console.log (e); + }); return true; } return false; diff --git a/lib/Sources/Interactive/PathCustomPrompt.ts b/lib/Sources/Interactive/PathCustomPrompt.ts new file mode 100644 index 0000000..da23c49 --- /dev/null +++ b/lib/Sources/Interactive/PathCustomPrompt.ts @@ -0,0 +1,25 @@ +import { Prompt } from 'enquirer'; + +export class PathPrompt extends Prompt { + private _index = 0; + private _value = ''; + + public constructor (options:Record = {}) { + 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}`); + } +} diff --git a/lib/Sources/Interactive/PathSubSource.ts b/lib/Sources/Interactive/PathSubSource.ts new file mode 100644 index 0000000..b81c6fc --- /dev/null +++ b/lib/Sources/Interactive/PathSubSource.ts @@ -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 { + await this.val.assign_arg ( + this.opt, + await new PathPrompt ({ + message: this.get_message (), + default: this.opt.default + }) + .run () + ); + } +} diff --git a/lib/Sources/Interactive/index.ts b/lib/Sources/Interactive/index.ts index dba59a3..21d2bd7 100644 --- a/lib/Sources/Interactive/index.ts +++ b/lib/Sources/Interactive/index.ts @@ -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 ]; diff --git a/tsconfig.json b/tsconfig.json index 3d4b744..394439b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es5", + "target": "es6", "module": "commonjs", "outDir": "./dist", "rootDir": "./",