1- import PromiseQueue from '../../helpers/PromiseQueueHellper' ;
2-
31const fs = require ( 'fs' ) ;
42const path = require ( 'path' ) ;
53
64import FsHelper from '../../helpers/FsHelper' ;
5+ import PromiseQueue from '../../helpers/PromiseQueueHellper' ;
76
8- const APP_FOLDER = 'YadroTimeTracker' ;
9- const PROFILE_FOLDER = 'profile1' ;
7+ const APP_DIR = 'YadroTimeTracker' ;
108
119export default abstract class AbstractFileRepository < T = any > {
12- folderWithProfile : string = 'profile1' ;
10+ dirWithProfileData : string = 'profile1' ;
1311 fileName : string = 'defaultFileName.json' ;
12+ saveInRoot : boolean = false ;
13+
14+ private logPrefix = `Repository[${ this . fileName } ]:` ;
1415
1516 writeFileQueue = new PromiseQueue ( ) ;
1617
1718 private static get appDataFolder ( ) {
1819 return process . env . APPDATA || '' ;
1920 }
2021
21- private static get profileFolder ( ) {
22- return path . join (
23- AbstractFileRepository . appDataFolder ,
24- APP_FOLDER ,
25- PROFILE_FOLDER
26- ) ;
22+ private get destFolder ( ) {
23+ const pathItems = [ AbstractFileRepository . appDataFolder , APP_DIR ] ;
24+ if ( ! this . saveInRoot ) {
25+ pathItems . push ( this . dirWithProfileData ) ;
26+ }
27+ return path . join ( ... pathItems ) ;
2728 }
2829
2930 private get filePath ( ) {
30- return path . join ( AbstractFileRepository . profileFolder , this . fileName ) ;
31+ return path . join ( this . destFolder , this . fileName ) ;
32+ }
33+
34+ public setProfile ( profile : string | null ) {
35+ if ( profile ) {
36+ this . dirWithProfileData = profile ;
37+ console . log ( `${ this . logPrefix } set profile=${ profile } ` ) ;
38+ } else {
39+ console . error ( `${ this . logPrefix } set profile=null` ) ;
40+ }
3141 }
3242
3343 public restore ( defaultValue : T ) : T {
@@ -40,7 +50,7 @@ export default abstract class AbstractFileRepository<T = any> {
4050 }
4151
4252 public save ( data : T ) {
43- FsHelper . mkdirIfNotExists ( AbstractFileRepository . profileFolder ) ;
53+ FsHelper . mkdirIfNotExists ( this . destFolder ) ;
4454 this . writeFileQueue . add ( ( ) =>
4555 FsHelper . writeFile ( this . filePath , data ) . catch ( ( ) => {
4656 console . error (
0 commit comments