@@ -13,6 +13,11 @@ export default abstract class AbstractFileRepository<T = any> {
1313
1414 writeFileQueue = new PromiseQueue ( ) ;
1515
16+ private get logPrefix ( ) {
17+ const filePath = ! this . saveInRoot ? this . dirWithProfileData : '' ;
18+ return `FileRepository [${ filePath } /${ this . fileName } ]:` ;
19+ }
20+
1621 private static get appDataFolder ( ) {
1722 return process . env . APPDATA || '' ;
1823 }
@@ -34,8 +39,9 @@ export default abstract class AbstractFileRepository<T = any> {
3439 }
3540
3641 public restore ( defaultValue : T ) : T {
42+ console . log ( `${ this . logPrefix } restore` ) ;
3743 if ( fs . existsSync ( this . filePath ) ) {
38- const data = fs . readFileSync ( this . filePath ) ;
44+ const data = fs . readFileSync ( this . filePath , { encoding : 'utf-8' } ) ;
3945 // TODO handle parse error. Backup file with issues and return defaultValue
4046 return JSON . parse ( data ) ;
4147 }
@@ -44,13 +50,12 @@ export default abstract class AbstractFileRepository<T = any> {
4450
4551 public save ( data : T ) {
4652 FsHelper . mkdirIfNotExists ( this . destFolder ) ;
47- this . writeFileQueue . add ( ( ) =>
48- FsHelper . writeFile ( this . filePath , data ) . catch ( ( ) => {
49- console . error (
50- `AbstractFileRepository: can't save file ${ this . fileName } ${ this . filePath } `
51- ) ;
52- } )
53- ) ;
54- this . writeFileQueue . run ( ) ;
53+ this . writeFileQueue . add ( ( ) => {
54+ console . log ( `${ this . logPrefix } save` ) ;
55+ return FsHelper . writeFile ( this . filePath , data ) . catch ( ( ) => {
56+ console . error ( `${ this . logPrefix } can't save file '${ this . filePath } '` ) ;
57+ } ) ;
58+ } ) ;
59+ this . writeFileQueue . execute ( ) ;
5560 }
5661}
0 commit comments