2021-12-09 16:25:14 +00:00
const { readFileSync , statSync , writeFileSync } = require ( 'fs' ) ;
const { join } = require ( 'path' ) ;
class Settings { // Heavily based on original for compat, but simplified and tweaked
constructor ( root ) {
this . path = join ( root , 'settings.json' ) ;
try {
this . lastSaved = readFileSync ( this . path ) ;
this . settings = JSON . parse ( this . lastSaved ) ;
2021-12-11 21:50:23 +00:00
} catch ( _e ) {
2021-12-09 16:25:14 +00:00
this . lastSaved = '' ;
this . settings = { } ;
}
this . lastModified = this . getLastModified ( ) ;
log ( 'AppSettings' , 'Loaded settings.json with path' , this . path , 'with settings' , this . settings , 'and last modified' , this . lastModified ) ;
}
getLastModified ( ) {
try {
return statSync ( this . path ) . mtime . getTime ( ) ;
} catch ( e ) {
return 0 ;
}
}
get ( key , defaultValue = false ) {
2021-12-11 21:44:25 +00:00
return this . settings [ key ] ? ? defaultValue ;
2021-12-09 16:25:14 +00:00
}
set ( key , value ) {
this . settings [ key ] = value ;
}
save ( ) {
2021-12-11 21:50:23 +00:00
if ( this . lastModified && this . lastModified !== this . getLastModified ( ) ) { // File was last modified after Settings was made, so was externally edited therefore we don't save over
2021-12-09 16:25:14 +00:00
log ( 'AppSettings' , 'Refusing to save settings.json due to last modified date mismatch' ) ;
return ;
}
try {
const toSave = JSON . stringify ( this . settings , null , 2 ) ;
2021-12-11 21:50:23 +00:00
if ( this . lastSaved != toSave ) { // Settings has changed
2021-12-09 16:25:14 +00:00
this . lastSaved = toSave ;
writeFileSync ( this . path , toSave ) ;
this . lastModified = this . getLastModified ( ) ;
}
2021-12-11 21:50:23 +00:00
log ( 'AppSettings' , 'Saved' , this . path ) ;
} catch ( e ) {
log ( 'AppSettings' , 'Failed to save' , this . path , e ) ;
2021-12-09 16:25:14 +00:00
}
}
}
module . exports = Settings ;