@@ -13,15 +13,37 @@ export function mkCacheDirectory(cacheDirectory: string) {
13
13
return cacheDirectory
14
14
}
15
15
16
+ export function getCacheOptions ( options : CacheOptions = { } ) : Required < CacheOptions > & { filename : string } {
17
+ const cwd = options . cwd ?? process . cwd ( )
18
+ const dir = options . dir ?? path . resolve ( cwd , 'node_modules' , '.cache' , pkgName )
19
+ const file = options . file ?? 'classSet.json'
20
+ const filename = path . resolve ( dir , file )
21
+ return {
22
+ cwd,
23
+ dir,
24
+ file,
25
+ filename
26
+ }
27
+ }
28
+
16
29
export function writeCache ( data : Set < string > , options : CacheOptions = { } ) {
17
30
try {
18
- const cwd = options . cwd ?? process . cwd ( )
19
- const cacheDirectory = options . dir ?? path . resolve ( cwd , 'node_modules' , '.cache' , pkgName )
20
- const filename = path . resolve ( cacheDirectory , options . file ?? 'classSet.json' )
21
- mkCacheDirectory ( cacheDirectory )
31
+ const { dir, filename } = getCacheOptions ( options )
32
+ mkCacheDirectory ( dir )
22
33
fs . writeFileSync ( filename , JSON . stringify ( Array . from ( data ) , null , 2 ) , 'utf-8' )
23
34
return filename
24
35
} catch ( error ) {
25
36
console . log ( error )
26
37
}
27
38
}
39
+
40
+ export function readCache ( options : CacheOptions = { } ) {
41
+ const { filename } = getCacheOptions ( options )
42
+ try {
43
+ const data = fs . readFileSync ( filename , 'utf-8' )
44
+ return new Set < string > ( JSON . parse ( data ) )
45
+ } catch ( error ) {
46
+ console . log ( error )
47
+ fs . unlinkSync ( filename )
48
+ }
49
+ }
0 commit comments