@@ -3,27 +3,38 @@ const path = require('path');
33
44import FsHelper from '../../helpers/FsHelper' ;
55
6- const APP_FOLDER = 'YadroTimeTracker' ;
7- const PROFILE_FOLDER = 'profile1' ;
6+ const APP_DIR = 'YadroTimeTracker' ;
87
98export default abstract class AbstractFileRepository < T = any > {
10- folderWithProfile : string = 'profile1' ;
9+ dirWithProfileData : string = 'profile1' ;
1110 fileName : string = 'defaultFileName.json' ;
11+ saveInRoot : boolean = false ;
12+
13+ private logPrefix = `Repository[${ this . fileName } ]:` ;
1214
1315 private static get appDataFolder ( ) {
1416 return process . env . APPDATA || '' ;
1517 }
1618
17- private static get profileFolder ( ) {
18- return path . join (
19- AbstractFileRepository . appDataFolder ,
20- APP_FOLDER ,
21- PROFILE_FOLDER ,
22- ) ;
19+ private get destFolder ( ) {
20+ const pathItems = [ AbstractFileRepository . appDataFolder , APP_DIR ] ;
21+ if ( ! this . saveInRoot ) {
22+ pathItems . push ( this . dirWithProfileData ) ;
23+ }
24+ return path . join ( ... pathItems ) ;
2325 }
2426
2527 private get filePath ( ) {
26- return path . join ( AbstractFileRepository . profileFolder , this . fileName ) ;
28+ return path . join ( this . destFolder , this . fileName ) ;
29+ }
30+
31+ public setProfile ( profile : string | null ) {
32+ if ( profile ) {
33+ this . dirWithProfileData = profile ;
34+ console . log ( `${ this . logPrefix } set profile=${ profile } ` ) ;
35+ } else {
36+ console . error ( `${ this . logPrefix } set profile=null` ) ;
37+ }
2738 }
2839
2940 public restore ( defaultValue : T ) : T {
@@ -36,7 +47,7 @@ export default abstract class AbstractFileRepository<T = any> {
3647 }
3748
3849 public save ( data : T ) {
39- FsHelper . mkdirIfNotExists ( AbstractFileRepository . profileFolder ) ;
50+ FsHelper . mkdirIfNotExists ( this . destFolder ) ;
4051 return FsHelper . writeFile ( this . filePath , data ) ;
4152 }
4253}
0 commit comments