Skip to content

Commit a732381

Browse files
committed
feat: add cache unit cases
1 parent 6927c5a commit a732381

File tree

7 files changed

+75
-7
lines changed

7 files changed

+75
-7
lines changed

packages/tailwindcss-patch/src/cache.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function mkCacheDirectory(cacheDirectory: string) {
1616
export function getCacheOptions(options: CacheOptions = {}): Required<CacheOptions> & { filename: string } {
1717
const cwd = options.cwd ?? process.cwd()
1818
const dir = options.dir ?? path.resolve(cwd, 'node_modules', '.cache', pkgName)
19-
const file = options.file ?? 'classSet.json'
19+
const file = options.file ?? 'index.json'
2020
const filename = path.resolve(dir, file)
2121
return {
2222
cwd,
@@ -40,10 +40,16 @@ export function writeCache(data: Set<string>, options: CacheOptions = {}) {
4040
export function readCache(options: CacheOptions = {}) {
4141
const { filename } = getCacheOptions(options)
4242
try {
43-
const data = fs.readFileSync(filename, 'utf-8')
44-
return new Set<string>(JSON.parse(data))
43+
if (fs.existsSync(filename)) {
44+
const data = fs.readFileSync(filename, 'utf-8')
45+
return new Set<string>(JSON.parse(data))
46+
}
4547
} catch (error) {
4648
console.log(error)
47-
fs.unlinkSync(filename)
49+
try {
50+
fs.unlinkSync(filename)
51+
} catch (error) {
52+
console.log(error)
53+
}
4854
}
4955
}

packages/tailwindcss-patch/src/class.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { getClassCacheSet, getContexts, getTailwindcssEntry } from './exposeContext'
2-
import type { InternalCacheOptions } from './type'
2+
import type { InternalCacheOptions, PatchOptions } from './type'
33
import { writeCache, readCache } from './cache'
4+
import { createPatch } from './patcher'
45

56
export interface TailwindcssPatcherOptions {
67
cache?: InternalCacheOptions
8+
patch?: PatchOptions
79
}
810

911
export class TailwindcssPatcher {
1012
public rawOptions: TailwindcssPatcherOptions
1113
public cache: InternalCacheOptions
14+
public patch: Function
1215
constructor(options: TailwindcssPatcherOptions = {}) {
1316
this.rawOptions = options
1417
let cache: InternalCacheOptions
@@ -31,6 +34,7 @@ export class TailwindcssPatcher {
3134
}
3235
}
3336
this.cache = cache
37+
this.patch = createPatch(options.patch)
3438
}
3539

3640
getPkgEntry(basedir?: string) {

packages/tailwindcss-patch/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './class'
12
export * from './exposeContext'
23
export * from './inspector'
34
export * from './patcher'

packages/tailwindcss-patch/src/log.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { pkgName } from './constants'
2+
3+
export function log(message?: any, ...optionalParams: any[]) {
4+
return console.log(`[${pkgName}]:` + message, ...optionalParams)
5+
}

packages/tailwindcss-patch/src/type.ts

-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ export interface PatchOptions {
1212
paths?: string[]
1313
basedir?: string
1414
custom?: (dir: string, ctx: Record<string, any>) => void
15-
cache?: boolean | CacheOptions
1615
}
1716

1817
export interface InternalPatchOptions {
1918
overwrite: boolean
2019
paths?: string[]
2120
basedir?: string
2221
custom?: (dir: string, ctx: Record<string, any>) => void
23-
cache?: CacheOptions
2422
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { getCacheOptions, mkCacheDirectory, readCache, writeCache } from '../src/cache'
2+
import path from 'path'
3+
import { pkgName } from '../src/constants'
4+
import fs from 'fs'
5+
describe('cache', () => {
6+
it('getCacheOptions', () => {
7+
expect(getCacheOptions).toBeDefined()
8+
expect(getCacheOptions().dir).toBe(path.resolve(process.cwd(), './node_modules/.cache', pkgName))
9+
})
10+
11+
it('mkCacheDirectory', () => {
12+
const dir = path.resolve(__dirname, './fixtures', pkgName)
13+
expect(mkCacheDirectory(dir)).toBe(dir)
14+
expect(fs.existsSync(dir)).toBe(true)
15+
16+
fs.rmdirSync(dir)
17+
expect(fs.existsSync(dir)).toBe(false)
18+
})
19+
20+
it('write and read cache default option', () => {
21+
// const opt = getCacheOptions()
22+
let cache: Set<string> | undefined
23+
cache = readCache()
24+
expect(cache).toBe(undefined)
25+
writeCache(new Set(['a', 'b', 'c']))
26+
cache = readCache()
27+
expect(cache).toBeDefined()
28+
if (cache) {
29+
expect(cache.size).toBe(3)
30+
}
31+
})
32+
33+
it('read broken cache', () => {
34+
// const opt = getCacheOptions()
35+
36+
const dir = path.resolve(__dirname, './fixtures', pkgName + '-broken')
37+
const filepath = path.resolve(dir, 'index.json')
38+
mkCacheDirectory(dir)
39+
fs.writeFileSync(
40+
filepath,
41+
`{
42+
[ '2',"fuck you",{s:'12}
43+
}`,
44+
'utf-8'
45+
)
46+
expect(fs.existsSync(filepath)).toBe(true)
47+
const cache = readCache({
48+
dir
49+
})
50+
expect(cache).toBe(undefined)
51+
expect(fs.existsSync(filepath)).toBe(false)
52+
})
53+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
describe('class', () => {})

0 commit comments

Comments
 (0)