This commit is contained in:
Timo Hocker 2020-05-28 18:54:34 +02:00
parent 5dfdb39af6
commit 8eaa02d96f
4 changed files with 15 additions and 9 deletions

View File

@ -19,7 +19,11 @@ const { InteractiveOptions } = require ('./dist/lib/index.js');
{ name: 'num', type: 'number', env: 'TEST_NUM' }, { name: 'num', type: 'number', env: 'TEST_NUM' },
{ name: 'arr', type: 'array', env: 'TEST_ARR' }, { name: 'arr', type: 'array', env: 'TEST_ARR' },
{ name: 'fld', type: 'folder', env: 'TEST_FOLDER' } { name: 'fld', type: 'folder', env: 'TEST_FOLDER' }
], { exit_on_interrupt: true, configs: [ 'test.json' ] }); ], {
exit_on_interrupt: true,
configs: [ 'test.json' ],
error_callback: console.log
});
await reader.parse (); await reader.parse ();
console.log (reader.serialize (true)); console.log (reader.serialize (true));
}) (); }) ();

View File

@ -75,8 +75,8 @@ export class InteractiveOptions extends Persistent {
&& Array.isArray (source_config.configs) && Array.isArray (source_config.configs)
) { ) {
this.sources.push (new ConfigSource ( this.sources.push (new ConfigSource (
source_config.error_callback, source_config.configs,
source_config.configs source_config.error_callback
)); ));
} }
if (source_config.env !== false) if (source_config.env !== false)
@ -85,8 +85,8 @@ export class InteractiveOptions extends Persistent {
this.sources.push (new ArgSource (source_config.error_callback)); this.sources.push (new ArgSource (source_config.error_callback));
if (source_config.interactive !== false) { if (source_config.interactive !== false) {
this.sources.push (new InteractiveSource ( this.sources.push (new InteractiveSource (
source_config.error_callback, exit_on_interrupt,
exit_on_interrupt source_config.error_callback
)); ));
} }
} }

View File

@ -17,7 +17,7 @@ import { OptionSource } from './OptionSource';
export class ConfigSource extends OptionSource { export class ConfigSource extends OptionSource {
private _config_files: string[]; private _config_files: string[];
public constructor (error_callback?:ErrorCallback, config_files: string[]) { public constructor (config_files: string[], error_callback?:ErrorCallback) {
super (error_callback); super (error_callback);
this._config_files = config_files; this._config_files = config_files;
} }
@ -55,6 +55,7 @@ export class ConfigSource extends OptionSource {
data[key] = json[key]; data[key] = json[key];
} }
catch (e) { catch (e) {
if (typeof this.error_callback !== 'undefined')
this.error_callback ('*', `config file: ${f}`, e); this.error_callback ('*', `config file: ${f}`, e);
continue; continue;
} }

View File

@ -9,14 +9,15 @@
/* eslint-disable no-process-exit */ /* eslint-disable no-process-exit */
import { Confirm, Input, List, AutoComplete } from 'enquirer'; import { Confirm, Input, List, AutoComplete } from 'enquirer';
import { OptionProcess, Option } from '../Option'; import { OptionProcess, Option } from '../Option';
import { ErrorCallback } from '../Types/ErrorCallback';
import { OptionSource } from './OptionSource'; import { OptionSource } from './OptionSource';
export class InteractiveSource extends OptionSource { export class InteractiveSource extends OptionSource {
private _exit_on_interrupt: boolean; private _exit_on_interrupt: boolean;
public constructor ( public constructor (
error_callback?:ErrorCallback, exit_on_interrupt: boolean,
exit_on_interrupt: boolean error_callback?:ErrorCallback
) { ) {
super (error_callback); super (error_callback);
this._exit_on_interrupt = exit_on_interrupt; this._exit_on_interrupt = exit_on_interrupt;