1
- ' use strict' ;
1
+ " use strict" ;
2
2
3
- import fs from 'fs' ;
3
+ import fs from "fs" ;
4
4
5
- import { ArgumentParser } from ' argparse' ;
5
+ import { ArgumentParser } from " argparse" ;
6
6
7
- import logger from ' ./logger.js' ;
7
+ import logger from " ./logger.js" ;
8
8
import { Option , StringListOption } from "./options.js" ;
9
9
10
10
export class ConfigurationParser {
@@ -24,7 +24,7 @@ export class ConfigurationParser {
24
24
25
25
// Parse arguments
26
26
const parser = new ArgumentParser ( ) ;
27
- parser . add_argument ( ' --config' , '-c' , { default : ' config.json' } ) ;
27
+ parser . add_argument ( " --config" , "-c" , { default : " config.json" } ) ;
28
28
for ( const option of this . #options) {
29
29
if ( option . alias ) {
30
30
parser . add_argument ( option . name , option . alias , option . argparseOptions ) ;
@@ -35,8 +35,8 @@ export class ConfigurationParser {
35
35
const args = parser . parse_args ( ) ;
36
36
37
37
const getJsonKey = ( option : Option < any > ) : string => {
38
- return option . name . replace ( / ^ - + / g, '' ) . replace ( / - / g, '_' ) ;
39
- }
38
+ return option . name . replace ( / ^ - + / g, "" ) . replace ( / - / g, "_" ) ;
39
+ } ;
40
40
41
41
const getOptionByName = ( name : string ) : Option < any > | null => {
42
42
for ( const option of this . #options) {
@@ -45,15 +45,15 @@ export class ConfigurationParser {
45
45
}
46
46
}
47
47
return null ;
48
- }
48
+ } ;
49
49
50
50
// Load config from file if it exists
51
51
let config : any = { } ;
52
- logger . info ( ' Loading config file: ' + args [ ' config' ] ) ;
53
- const configFileExists = fs . existsSync ( args [ ' config' ] ) ;
52
+ logger . info ( " Loading config file: " + args [ " config" ] ) ;
53
+ const configFileExists = fs . existsSync ( args [ " config" ] ) ;
54
54
if ( configFileExists ) {
55
55
try {
56
- config = JSON . parse ( fs . readFileSync ( args [ ' config' ] , { encoding : ' utf-8' } ) ) ;
56
+ config = JSON . parse ( fs . readFileSync ( args [ " config" ] , { encoding : " utf-8" } ) ) ;
57
57
58
58
// Check for unknown options
59
59
for ( const key of Object . keys ( config ) ) {
@@ -71,19 +71,19 @@ export class ConfigurationParser {
71
71
continue ;
72
72
}
73
73
for ( const item of value ) {
74
- if ( typeof item !== ' string' ) {
74
+ if ( typeof item !== " string" ) {
75
75
throw new Error ( `Error parsing option "${ key } ": Item is not a string: ${ item } ` ) ;
76
76
}
77
77
}
78
78
}
79
79
}
80
80
} catch ( error ) {
81
- logger . error ( ' Failed to read config file!' ) ;
81
+ logger . error ( " Failed to read config file!" ) ;
82
82
logger . error ( error ) ;
83
83
process . exit ( 1 ) ;
84
84
}
85
85
} else {
86
- logger . warn ( ' Config file not found! Creating a default one...' ) ;
86
+ logger . warn ( " Config file not found! Creating a default one..." ) ;
87
87
}
88
88
89
89
// Override options from config with options from arguments and set defaults
@@ -92,14 +92,14 @@ export class ConfigurationParser {
92
92
if ( args [ key ] === undefined ) {
93
93
if ( config [ key ] === undefined ) {
94
94
const defaultValue = option . defaultValue ;
95
- if ( typeof defaultValue === ' function' ) {
95
+ if ( typeof defaultValue === " function" ) {
96
96
config [ key ] = defaultValue ( ) ;
97
97
} else {
98
98
config [ key ] = defaultValue ;
99
99
}
100
100
}
101
101
} else {
102
- if ( typeof args [ key ] === ' string' ) {
102
+ if ( typeof args [ key ] === " string" ) {
103
103
config [ key ] = option . parse ( args [ key ] ) ;
104
104
} else {
105
105
config [ key ] = args [ key ] ;
@@ -110,8 +110,8 @@ export class ConfigurationParser {
110
110
// Save config file if it didn't exist
111
111
if ( this . #saveIfNotExist) {
112
112
if ( ! configFileExists ) {
113
- fs . writeFileSync ( args [ ' config' ] , JSON . stringify ( config , null , 4 ) ) ;
114
- logger . info ( ' Config saved to ' + args [ ' config' ] ) ;
113
+ fs . writeFileSync ( args [ " config" ] , JSON . stringify ( config , null , 4 ) ) ;
114
+ logger . info ( " Config saved to " + args [ " config" ] ) ;
115
115
}
116
116
}
117
117
0 commit comments