Skip to content

Commit 6927c5a

Browse files
committed
feat: add cache utils
1 parent f993762 commit 6927c5a

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

packages/tailwindcss-patch/src/cache.ts

+26-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,37 @@ export function mkCacheDirectory(cacheDirectory: string) {
1313
return cacheDirectory
1414
}
1515

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+
1629
export function writeCache(data: Set<string>, options: CacheOptions = {}) {
1730
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)
2233
fs.writeFileSync(filename, JSON.stringify(Array.from(data), null, 2), 'utf-8')
2334
return filename
2435
} catch (error) {
2536
console.log(error)
2637
}
2738
}
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+
}

packages/tailwindcss-patch/src/class.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { getClassCacheSet, getContexts, getTailwindcssEntry } from './exposeContext'
2-
import type { CacheOptions } from './type'
3-
4-
type InternalCacheOptions = CacheOptions & { enable: boolean }
2+
import type { InternalCacheOptions } from './type'
3+
import { writeCache, readCache } from './cache'
54

65
export interface TailwindcssPatcherOptions {
76
cache?: InternalCacheOptions
@@ -28,7 +27,6 @@ export class TailwindcssPatcher {
2827
}
2928
case 'object': {
3029
cache = { ...options.cache, enable: true }
31-
3230
break
3331
}
3432
}
@@ -39,8 +37,21 @@ export class TailwindcssPatcher {
3937
return getTailwindcssEntry(basedir)
4038
}
4139

40+
setCache(set: Set<string>) {
41+
if (this.cache.enable) {
42+
return writeCache(set, this.cache)
43+
}
44+
}
45+
46+
getCache() {
47+
// if(this.cache.enable){
48+
return readCache(this.cache)
49+
// }
50+
}
51+
4252
getClassSet(basedir?: string) {
4353
const set = getClassCacheSet(basedir)
54+
set.size && this.setCache(set)
4455
return set
4556
}
4657

packages/tailwindcss-patch/src/type.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface CacheOptions {
55
file?: string
66
}
77

8+
export type InternalCacheOptions = CacheOptions & { enable: boolean }
9+
810
export interface PatchOptions {
911
overwrite?: boolean
1012
paths?: string[]

packages/tailwindcss-patch/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ export function ensureFileContent(filepaths: string | string[]) {
2020

2121
export function requireResolve(id: string, opts?: SyncOpts) {
2222
return sync(id, opts)
23-
}
23+
}

0 commit comments

Comments
 (0)