1
- const _ = require ( 'lodash' )
2
- const Promise = require ( 'bluebird' )
3
- const { globalPubSub } = require ( '@packages/data-context' )
4
-
5
- const { fs } = require ( './util/fs' )
6
- const appData = require ( './util/app_data' )
7
- const FileUtil = require ( './util/file' )
1
+ import _ from 'lodash'
2
+ import Promise from 'bluebird'
3
+ import { globalPubSub } from '@packages/data-context'
4
+ import { fs } from './util/fs'
5
+ import appData from './util/app_data'
6
+ import FileUtil from './util/file'
7
+ import type { Cache , CachedUser , Preferences , Cohort } from '@packages/types'
8
+
9
+ interface Transaction {
10
+ get : < T = Cache > ( key ?: string , defaultValue ?: T ) => Promise < T >
11
+ set : ( key : string | Partial < Cache > , value ?: any ) => Promise < void >
12
+ }
8
13
9
14
const fileUtil = new FileUtil ( {
10
15
path : appData . path ( 'cache' ) ,
@@ -14,90 +19,52 @@ globalPubSub.on('test:cleanup', () => {
14
19
fileUtil . __resetForTest ( )
15
20
} )
16
21
17
- const convertProjectsToArray = function ( obj ) {
18
- // if our project structure is not
19
- // an array then its legacy and we
20
- // need to convert it
21
- if ( ! _ . isArray ( obj . PROJECTS ) ) {
22
- obj . PROJECTS = _ . chain ( obj . PROJECTS ) . values ( ) . map ( 'PATH' ) . compact ( ) . value ( )
23
-
24
- return obj
25
- }
26
- }
27
-
28
- const renameSessionToken = function ( obj ) {
29
- let st
30
-
31
- if ( obj . USER && ( st = obj . USER . session_token ) ) {
32
- delete obj . USER . session_token
33
- obj . USER . sessionToken = st
34
-
35
- return obj
36
- }
37
- }
38
-
39
- module . exports = {
22
+ export const cache = {
40
23
path : fileUtil . path ,
41
24
42
- defaults ( ) {
25
+ defaults ( ) : Cache {
43
26
return {
44
- USER : { } ,
27
+ USER : {
28
+ authToken : '' ,
29
+ name : '' ,
30
+ email : '' ,
31
+ } ,
45
32
PROJECTS : [ ] ,
46
33
PROJECT_PREFERENCES : { } ,
47
34
PROJECTS_CONFIG : { } ,
48
35
COHORTS : { } ,
49
36
}
50
37
} ,
51
38
52
- _applyRewriteRules ( obj = { } ) {
53
- return _ . reduce ( [ convertProjectsToArray , renameSessionToken ] , ( memo , fn ) => {
54
- let ret
55
-
56
- ret = fn ( memo )
57
-
58
- if ( ret ) {
59
- return ret
60
- }
61
-
62
- return memo
63
- }
64
- , _ . cloneDeep ( obj ) )
65
- } ,
66
-
67
- read ( ) {
39
+ _read ( ) : Promise < Cache > {
68
40
return fileUtil . get ( ) . then ( ( contents ) => {
69
41
return _ . defaults ( contents , this . defaults ( ) )
70
42
} )
71
43
} ,
72
44
73
- write ( obj = { } ) {
74
- return fileUtil . set ( obj ) . return ( obj )
75
- } ,
76
-
77
- _getProjects ( tx ) {
45
+ _getProjects ( tx : Transaction ) : Promise < string [ ] > {
78
46
return tx . get ( 'PROJECTS' , [ ] )
79
47
} ,
80
48
81
- _removeProjects ( tx , projects , paths ) {
82
- // normalize paths in array
83
- projects = _ . without ( projects , ...[ ] . concat ( paths ) )
49
+ _removeProjects ( tx : Transaction , projects : string [ ] , paths : string | string [ ] ) : Promise < void > {
50
+ const pathsArray = Array . isArray ( paths ) ? paths : [ paths ]
51
+
52
+ projects = _ . without ( projects , ...pathsArray )
84
53
85
54
return tx . set ( { PROJECTS : projects } )
86
55
} ,
87
56
88
- /**
89
- * @return {Promise<string[]> }
90
- */
91
- getProjectRoots ( ) {
92
- return fileUtil . transaction ( ( tx ) => {
57
+ getProjectRoots ( ) : Promise < string [ ] > {
58
+ return fileUtil . transaction ( ( tx : Transaction ) => {
93
59
return this . _getProjects ( tx ) . then ( ( projects ) => {
94
- const pathsToRemove = Promise . reduce ( projects , ( memo , path ) => {
60
+ const pathsToRemove = Promise . reduce ( projects , ( memo : string [ ] , path ) => {
95
61
return fs . statAsync ( path )
96
62
. catch ( ( ) => {
97
- return memo . push ( path )
63
+ memo . push ( path )
64
+
65
+ return memo
98
66
} ) . return ( memo )
99
- }
100
- , [ ] )
67
+ } , [ ] )
101
68
102
69
return pathsToRemove . then ( ( removedPaths ) => {
103
70
return this . _removeProjects ( tx , projects , removedPaths )
@@ -108,16 +75,16 @@ module.exports = {
108
75
} )
109
76
} ,
110
77
111
- removeProject ( path ) {
112
- return fileUtil . transaction ( ( tx ) => {
78
+ removeProject ( path : string ) : Promise < void > {
79
+ return fileUtil . transaction ( ( tx : Transaction ) => {
113
80
return this . _getProjects ( tx ) . then ( ( projects ) => {
114
81
return this . _removeProjects ( tx , projects , path )
115
82
} )
116
83
} )
117
84
} ,
118
85
119
- insertProject ( path ) {
120
- return fileUtil . transaction ( ( tx ) => {
86
+ insertProject ( path : string ) : Promise < void > {
87
+ return fileUtil . transaction ( ( tx : Transaction ) => {
121
88
return this . _getProjects ( tx ) . then ( ( projects ) => {
122
89
// projects are sorted by most recently used, so add a project to
123
90
// the start or move it to the start if it already exists
@@ -136,28 +103,28 @@ module.exports = {
136
103
} )
137
104
} ,
138
105
139
- getUser ( ) {
106
+ getUser ( ) : Promise < CachedUser > {
140
107
return fileUtil . get ( 'USER' , { } )
141
108
} ,
142
109
143
- setUser ( user ) {
110
+ setUser ( user : CachedUser ) : Promise < void > {
144
111
return fileUtil . set ( { USER : user } )
145
112
} ,
146
113
147
- removeUser ( ) {
114
+ removeUser ( ) : Promise < void > {
148
115
return fileUtil . set ( { USER : { } } )
149
116
} ,
150
117
151
- removeLatestProjects ( ) {
118
+ removeLatestProjects ( ) : Promise < void > {
152
119
return fileUtil . set ( { PROJECTS : [ ] } )
153
120
} ,
154
121
155
- getProjectPreferences ( ) {
122
+ getProjectPreferences ( ) : Promise < Record < string , Preferences > > {
156
123
return fileUtil . get ( 'PROJECT_PREFERENCES' , { } )
157
124
} ,
158
125
159
- insertProjectPreferences ( projectTitle , projectPreferences ) {
160
- return fileUtil . transaction ( ( tx ) => {
126
+ insertProjectPreferences ( projectTitle : string , projectPreferences : Preferences ) : Promise < void > {
127
+ return fileUtil . transaction ( ( tx : Transaction ) => {
161
128
return tx . get ( 'PROJECT_PREFERENCES' , { } ) . then ( ( preferences ) => {
162
129
return tx . set ( 'PROJECT_PREFERENCES' , {
163
130
...preferences ,
@@ -170,11 +137,11 @@ module.exports = {
170
137
} )
171
138
} ,
172
139
173
- removeAllProjectPreferences ( ) {
140
+ removeAllProjectPreferences ( ) : Promise < void > {
174
141
return fileUtil . set ( { PROJECT_PREFERENCES : { } } )
175
142
} ,
176
143
177
- removeProjectPreferences ( projectTitle ) {
144
+ removeProjectPreferences ( projectTitle : string ) : Promise < void > {
178
145
const preferences = fileUtil . get ( 'PROJECT_PREFERENCES' , { } )
179
146
180
147
const updatedPreferences = {
@@ -185,7 +152,7 @@ module.exports = {
185
152
return fileUtil . set ( { PROJECT_PREFERENCES : updatedPreferences } )
186
153
} ,
187
154
188
- getCohorts ( ) {
155
+ getCohorts ( ) : Promise < Record < string , Cohort > > {
189
156
return fileUtil . get ( 'COHORTS' , { } ) . then ( ( cohorts ) => {
190
157
Object . keys ( cohorts ) . forEach ( ( key ) => {
191
158
cohorts [ key ] . name = key
@@ -195,8 +162,8 @@ module.exports = {
195
162
} )
196
163
} ,
197
164
198
- insertCohort ( cohort ) {
199
- return fileUtil . transaction ( ( tx ) => {
165
+ insertCohort ( cohort : Cohort ) : Promise < void > {
166
+ return fileUtil . transaction ( ( tx : Transaction ) => {
200
167
return tx . get ( 'COHORTS' , { } ) . then ( ( cohorts ) => {
201
168
return tx . set ( 'COHORTS' , {
202
169
...cohorts ,
@@ -208,11 +175,10 @@ module.exports = {
208
175
} )
209
176
} ,
210
177
211
- remove ( ) {
178
+ remove ( ) : Promise < void > {
212
179
return fileUtil . remove ( )
213
180
} ,
214
181
215
182
// for testing purposes
216
-
217
- __get : fileUtil . get . bind ( fileUtil ) ,
183
+ __get : fileUtil . get . bind ( fileUtil ) as < T = Cache > ( key ?: string , defaultValue ?: T ) => Promise < T > ,
218
184
}
0 commit comments