1
- const EXTENSION_NAME = 'gitProjectManager' ;
2
-
3
1
const fs = require ( 'fs' ) ;
4
2
const vscode = require ( 'vscode' ) ;
5
3
const path = require ( 'path' ) ;
@@ -18,32 +16,18 @@ class GitProjectManager {
18
16
* Creates an instance of GitProjectManager.
19
17
*
20
18
* @param {object } config
19
+ * @param {Memento } state
21
20
*/
22
- constructor ( config ) {
21
+ constructor ( config , state ) {
23
22
this . config = config ;
23
+ this . state = state ;
24
24
this . loadedRepoListFromFile = false ;
25
25
this . repoList = [ ] ;
26
26
this . storedLists = new Map ( ) ;
27
- this . baseDir = this . getBaseDir ( ) ;
28
- this . gpmRepoListFile = path . join ( this . baseDir , this . getChannelPath ( ) , "User/gpm_projects.json" ) ;
29
- this . recentList = new RecentItems ( path . join ( this . baseDir , this . getChannelPath ( ) , "User/" ) ) ;
30
- this . recentList . listSize = vscode . workspace . getConfiguration ( EXTENSION_NAME ) . get ( 'gitProjectManager.recentProjectsListSize' , 5 ) ;
27
+ this . recentList = new RecentItems ( this . state , this . config . get ( 'gitProjectManager.recentProjectsListSize' , 5 ) ) ;
31
28
32
29
this . updateRepoList = this . updateRepoList . bind ( this ) ;
33
30
this . addRepoInRepoList = this . addRepoInRepoList . bind ( this ) ;
34
-
35
- }
36
- /**
37
- * Get the base user cfg directory
38
- *
39
- * @returns {string }
40
- */
41
- getBaseDir ( ) {
42
- if ( process . platform == "linux" ) {
43
- return path . join ( os . homedir ( ) , '.config' ) ;
44
- } else {
45
- return process . env . APPDATA || ( process . platform === 'darwin' ? process . env . HOME + '/Library/Application Support' : '/var/local' ) ;
46
- }
47
31
}
48
32
getQuickPickList ( ) {
49
33
this . repoList = this . repoList . sort ( ( a , b ) => {
@@ -77,47 +61,40 @@ class GitProjectManager {
77
61
get storeDataBetweenSessions ( ) {
78
62
return this . config . storeRepositoriesBetweenSessions ;
79
63
}
80
- saveListToDisc ( ) {
64
+ saveList ( ) {
81
65
if ( ! this . storeDataBetweenSessions )
82
66
return ;
83
67
84
- let lists = { } ;
85
- this . storedLists . forEach ( ( value , key ) => { lists [ key ] = value } )
86
- fs . writeFileSync ( this . gpmRepoListFile , JSON . stringify ( lists ) , {
87
- encoding : 'utf8'
88
- } ) ;
68
+ const lists = Array . from ( this . storedLists . entries ( ) ) . reduce (
69
+ ( storage , [ hash , repos ] ) => ( { ... storage , [ hash ] : repos } ) ,
70
+ { } ,
71
+ ) ;
72
+ this . state . update ( 'lists' , lists ) ;
89
73
}
90
- loadlListFromDisc ( ) {
74
+ loadList ( ) {
91
75
if ( ! this . storeDataBetweenSessions )
92
76
return false ;
93
77
94
- if ( ! fs . existsSync ( this . gpmRepoListFile ) )
95
- return false ;
96
-
97
- let list = JSON . parse ( fs . readFileSync ( this . gpmRepoListFile , 'utf8' ) ) ;
98
- if ( list instanceof Array ) {
99
- fs . unlinkSync ( this . gpmRepoListFile ) ;
78
+ const list = this . state . get ( 'lists' , false ) ;
79
+ if ( ! list )
100
80
return false ;
101
- }
102
81
103
- this . storedLists = new Map ( ) ;
104
- for ( let key in list )
105
- this . storedLists . set ( key , list [ key ] ) ;
82
+ this . storedLists = new Map ( Array . from ( Object . entries ( list ) ) ) ;
106
83
107
84
this . loadedRepoListFromFile = true ;
108
85
return true ;
109
86
}
110
87
saveRepositoryInfo ( directories ) {
111
88
this . storedLists . set ( this . getDirectoriesHash ( directories ) , this . repoList ) ;
112
- this . saveListToDisc ( ) ;
89
+ this . saveList ( ) ;
113
90
}
114
91
115
92
loadRepositoryInfo ( ) {
116
93
if ( this . loadedRepoListFromFile ) {
117
94
return false ;
118
95
}
119
96
120
- return this . loadlListFromDisc ( ) ;
97
+ return this . loadList ( ) ;
121
98
}
122
99
123
100
addRepoInRepoList ( repoInfo ) {
0 commit comments