diff --git a/AppTest.js b/AppTest.js new file mode 100644 index 0000000..c94e600 --- /dev/null +++ b/AppTest.js @@ -0,0 +1,15 @@ +/* eslint-disable no-console */ +'use strict'; + +// eslint-disable-next-line id-match +const { InteractiveOptions } = require ('./dist/lib/index.js'); + +(async () => { + const reader = new InteractiveOptions ([ + { name: 'str', type: 'string' }, + { name: 'bool', type: 'boolean' }, + { name: 'num', type: 'number' } + ]); + await reader.parse (); + console.log (reader.serialize (true)); +}) (); diff --git a/lib/InteractiveOptions.ts b/lib/InteractiveOptions.ts index f8cd4d3..c396147 100644 --- a/lib/InteractiveOptions.ts +++ b/lib/InteractiveOptions.ts @@ -12,242 +12,73 @@ /* eslint-disable max-statements */ /* eslint-disable no-process-env */ import { Persistent } from '@sapphirecode/modelling'; -import fs from 'fs-extra'; -import yargs, { Options } from 'yargs'; -import { Confirm, Input } from 'enquirer'; +import { TypeValidation } from './Types/TypeValidation'; +import { PathType } from './Types/PathType'; +import { Option, OptionProcess, OptionType } from './Types'; +import { OptionSource } from './Sources/OptionSource'; +import { EnvSource } from './Sources/EnvSource'; +import { ArgSource } from './Sources/ArgSource'; +import { InteractiveSource } from './Sources/InteractiveSource'; -type OptionType = - 'string' - | 'number' - | 'boolean' - | 'file' - | 'folder' - | 'path'; +const types: Record = { + string: new TypeValidation ('string'), + number: new TypeValidation ('number'), + boolean: new TypeValidation ('boolean'), + file: new PathType ('file'), + folder: new PathType ('folder'), + path: new PathType ('path') +}; -interface Option { - name: string; - type: OptionType; - required?: boolean; - default?: unknown; - alias?: string; - env?: string; - description?: string; - message?: string; -} - -interface OptionProcess extends Option { - filled: boolean; - value?: unknown; -} - -function get_string_type (type: OptionType): 'string'|'number'|'boolean' { - if ([ - 'string', - 'number', - 'boolean' - ].includes (type)) - return type as ('string'|'number'|'boolean'); - if ([ - 'file', - 'folder', - 'path' - ].includes (type)) - return 'string'; - throw new Error (`unknown option type ${type}`); +interface SourceConfig { + env: boolean; + args: boolean; + interactive: boolean; } export class InteractiveOptions extends Persistent { protected options: Array; protected quiet = false; + protected sources: OptionSource[] = []; - public constructor (options: Array