Skip to content

Commit d50c9f5

Browse files
committed
feat: add unit test and TailwindcssPatcher class
1 parent a732381 commit d50c9f5

File tree

16 files changed

+173
-109
lines changed

16 files changed

+173
-109
lines changed

apps/next-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"prepare": "tw-patch"
1111
},
1212
"dependencies": {
13-
"@types/node": "18.16.1",
13+
"@types/node": "18.16.2",
1414
"@types/react": "18.2.0",
1515
"@types/react-dom": "18.2.1",
1616
"autoprefixer": "10.4.14",

apps/nuxt-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"prepare": "tw-patch"
1111
},
1212
"devDependencies": {
13-
"@types/node": "^18",
13+
"@types/node": "^18.16.2",
1414
"autoprefixer": "^10.4.14",
1515
"nuxt": "^3.4.2",
1616
"postcss": "^8.4.23",

apps/vite-vue/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
"typescript": "^5.0.4",
2222
"unplugin-tailwindcss-mangle": "workspace:*",
2323
"vite": "^4.3.3",
24-
"vue-tsc": "^1.6.0"
24+
"vue-tsc": "^1.6.1"
2525
}
2626
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@icebreakers/tsconfig": "^0.0.6",
2424
"@tsconfig/recommended": "^1.0.2",
2525
"@types/jest": "^29.5.1",
26-
"@types/node": "^18.16.1",
26+
"@types/node": "^18.16.2",
2727
"cross-env": "^7.0.3",
2828
"eslint": "^8.39.0",
2929
"jest": "^29.5.0",

packages/tailwindcss-patch/src/cache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import { pkgName } from './constants'
44
import type { CacheOptions } from './type'
5-
5+
import { log } from './logger'
66
export function mkCacheDirectory(cacheDirectory: string) {
77
const exists = fs.existsSync(cacheDirectory)
88
if (!exists) {
@@ -15,7 +15,7 @@ export function mkCacheDirectory(cacheDirectory: string) {
1515

1616
export function getCacheOptions(options: CacheOptions = {}): Required<CacheOptions> & { filename: string } {
1717
const cwd = options.cwd ?? process.cwd()
18-
const dir = options.dir ?? path.resolve(cwd, 'node_modules', '.cache', pkgName)
18+
const dir = options.dir ?? path.resolve(cwd, 'node_modules/.cache', pkgName)
1919
const file = options.file ?? 'index.json'
2020
const filename = path.resolve(dir, file)
2121
return {
@@ -33,7 +33,7 @@ export function writeCache(data: Set<string>, options: CacheOptions = {}) {
3333
fs.writeFileSync(filename, JSON.stringify(Array.from(data), null, 2), 'utf-8')
3434
return filename
3535
} catch (error) {
36-
console.log(error)
36+
log('write cache file fail!')
3737
}
3838
}
3939

@@ -45,11 +45,11 @@ export function readCache(options: CacheOptions = {}) {
4545
return new Set<string>(JSON.parse(data))
4646
}
4747
} catch (error) {
48-
console.log(error)
48+
log('parse cache content fail! path:' + filename)
4949
try {
5050
fs.unlinkSync(filename)
5151
} catch (error) {
52-
console.log(error)
52+
log('delete cache file fail! path:' + filename)
5353
}
5454
}
5555
}

packages/tailwindcss-patch/src/class.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ export interface TailwindcssPatcherOptions {
1010

1111
export class TailwindcssPatcher {
1212
public rawOptions: TailwindcssPatcherOptions
13-
public cache: InternalCacheOptions
14-
public patch: Function
13+
public cacheOptions: InternalCacheOptions
14+
public patchOptions?: PatchOptions
15+
public patch: () => void
1516
constructor(options: TailwindcssPatcherOptions = {}) {
1617
this.rawOptions = options
1718
let cache: InternalCacheOptions
@@ -33,7 +34,8 @@ export class TailwindcssPatcher {
3334
break
3435
}
3536
}
36-
this.cache = cache
37+
this.cacheOptions = cache
38+
this.patchOptions = options.patch
3739
this.patch = createPatch(options.patch)
3840
}
3941

@@ -42,14 +44,14 @@ export class TailwindcssPatcher {
4244
}
4345

4446
setCache(set: Set<string>) {
45-
if (this.cache.enable) {
46-
return writeCache(set, this.cache)
47+
if (this.cacheOptions.enable) {
48+
return writeCache(set, this.cacheOptions)
4749
}
4850
}
4951

5052
getCache() {
5153
// if(this.cache.enable){
52-
return readCache(this.cache)
54+
return readCache(this.cacheOptions)
5355
// }
5456
}
5557

packages/tailwindcss-patch/src/patcher.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@ export function getInstalledPkgJsonPath(options: PatchOptions = {}) {
2727
}
2828

2929
export function createPatch(options: PatchOptions = {}) {
30-
const opt = defu(options, defaultOptions) as InternalPatchOptions
30+
const opt = defu(
31+
options,
32+
{
33+
basedir: process.cwd()
34+
},
35+
defaultOptions
36+
) as InternalPatchOptions
3137
return () => {
3238
try {
33-
const pkgJsonPath = getInstalledPkgJsonPath(options)
39+
const pkgJsonPath = getInstalledPkgJsonPath(opt)
3440
return internalPatch(pkgJsonPath, opt)
3541
} catch (error) {
3642
console.warn(`patch tailwindcss failed:` + (<Error>error).message)

packages/tailwindcss-patch/src/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface CacheOptions {
55
file?: string
66
}
77

8-
export type InternalCacheOptions = CacheOptions & { enable: boolean }
8+
export type InternalCacheOptions = CacheOptions & { enable?: boolean }
99

1010
export interface PatchOptions {
1111
overwrite?: boolean

packages/tailwindcss-patch/test/cache.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ describe('cache', () => {
2121
// const opt = getCacheOptions()
2222
let cache: Set<string> | undefined
2323
cache = readCache()
24-
expect(cache).toBe(undefined)
25-
writeCache(new Set(['a', 'b', 'c']))
24+
// expect(cache).toBe(undefined)
25+
writeCache(new Set(['a', 'b', 'c']), {
26+
dir: path.resolve(__dirname, 'fixtures/cache'),
27+
file: 'raw-method.json'
28+
})
2629
cache = readCache()
2730
expect(cache).toBeDefined()
2831
if (cache) {

0 commit comments

Comments
 (0)