@@ -42,17 +42,17 @@ const defaultConfig: WinboatConfigObj = {
4242} ;
4343
4444export class WinboatConfig {
45+ private static readonly configPath : string = path . join ( WINBOAT_DIR , "winboat.config.json" ) ;
4546 private static instance : WinboatConfig | null = null ;
46- readonly #configPath: string = path . join ( WINBOAT_DIR , "winboat.config.json" ) ;
4747 #configData: WinboatConfigObj = { ...defaultConfig } ;
4848
4949 static getInstance ( ) {
5050 WinboatConfig . instance ??= new WinboatConfig ( ) ;
5151 return WinboatConfig . instance ;
5252 }
5353
54- constructor ( ) {
55- this . #configData = this . readConfig ( ) ! ;
54+ private constructor ( ) {
55+ this . #configData = WinboatConfig . readConfigObject ( ) ! ;
5656 console . log ( "Reading current config" , this . #configData) ;
5757 }
5858
@@ -63,7 +63,7 @@ export class WinboatConfig {
6363 set : ( target , key , value ) => {
6464 // @ts -expect-error This is valid
6565 target [ key as keyof WinboatConfigObj ] = value ;
66- this . writeConfig ( ) ;
66+ WinboatConfig . writeConfigObject ( target ) ;
6767 console . info ( "Wrote modified config to disk" ) ;
6868 return true ;
6969 } ,
@@ -72,29 +72,29 @@ export class WinboatConfig {
7272
7373 set config ( newConfig : WinboatConfigObj ) {
7474 this . #configData = { ...newConfig } ;
75- this . writeConfig ( ) ;
75+ WinboatConfig . writeConfigObject ( newConfig ) ;
7676 console . info ( "Wrote modified config to disk" ) ;
7777 }
7878
79- writeConfig ( ) : void {
80- console . log ( "writing data: " , this . #configData ) ;
81- fs . writeFileSync ( this . # configPath, JSON . stringify ( this . #configData , null , 4 ) , "utf-8" ) ;
79+ static writeConfigObject ( configObj : WinboatConfigObj ) : void {
80+ console . log ( "writing data: " , configObj ) ;
81+ fs . writeFileSync ( WinboatConfig . configPath , JSON . stringify ( configObj , null , 4 ) , "utf-8" ) ;
8282 }
8383
84- readConfig ( writeDefault = true ) : WinboatConfigObj | null {
85- if ( ! fs . existsSync ( this . # configPath) ) {
84+ static readConfigObject ( writeDefault = true ) : WinboatConfigObj | null {
85+ if ( ! fs . existsSync ( WinboatConfig . configPath ) ) {
8686 if ( ! writeDefault ) return null ;
8787 // Also the create the directory because we're not guaranteed to have it
8888 if ( ! fs . existsSync ( WINBOAT_DIR ) ) {
8989 fs . mkdirSync ( WINBOAT_DIR ) ;
9090 }
9191
92- fs . writeFileSync ( this . # configPath, JSON . stringify ( defaultConfig , null , 4 ) , "utf-8" ) ;
92+ fs . writeFileSync ( WinboatConfig . configPath , JSON . stringify ( defaultConfig , null , 4 ) , "utf-8" ) ;
9393 return { ...defaultConfig } ;
9494 }
9595
9696 try {
97- const rawConfig = fs . readFileSync ( this . # configPath, "utf-8" ) ;
97+ const rawConfig = fs . readFileSync ( WinboatConfig . configPath , "utf-8" ) ;
9898 const configObj = JSON . parse ( rawConfig ) as WinboatConfigObj ;
9999 console . log ( "Successfully read the config file" ) ;
100100
@@ -115,7 +115,7 @@ export class WinboatConfig {
115115 // If we have any missing keys, we should just write the config back to disk so those new keys are saved
116116 // We cannot use this.writeConfig() here since #configData is not populated yet
117117 if ( hasMissing ) {
118- fs . writeFileSync ( this . # configPath, JSON . stringify ( configObj , null , 4 ) , "utf-8" ) ;
118+ fs . writeFileSync ( WinboatConfig . configPath , JSON . stringify ( configObj , null , 4 ) , "utf-8" ) ;
119119 console . log ( "Wrote updated config with missing keys to disk" ) ;
120120 }
121121 }
0 commit comments