Skip to content

Commit d74ad68

Browse files
committed
feat(tailwindcss-patch): add fs-extra and consola as deps
1 parent 6032164 commit d74ad68

File tree

15 files changed

+72
-86
lines changed

15 files changed

+72
-86
lines changed

packages/config/test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { resolve } from 'node:path'
2-
import { existsSync } from 'node:fs'
2+
import { existsSync } from 'fs-extra'
33
import { deleteAsync } from 'del'
44
import { fixturesRoot } from './utils'
55
import { getConfig, initConfig } from '@/index'

packages/tailwindcss-patch/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
"@babel/types": "^7.24.9",
5656
"@tailwindcss-mangle/config": "workspace:^",
5757
"cac": "^6.7.14",
58+
"consola": "^3.2.3",
59+
"fs-extra": "^11.2.0",
5860
"jiti": "^1.21.6",
5961
"lilconfig": "^3.1.2",
6062
"postcss": "^8.4.38",

packages/tailwindcss-patch/src/cli.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import process from 'node:process'
22
import cac from 'cac'
33
import { configName, getConfig, initConfig } from './config'
44
import { TailwindcssPatcher } from './core'
5+
import logger from './logger'
56
import { getPatchOptions } from '@/defaults'
67

78
function init() {
@@ -20,15 +21,15 @@ cli.command('install', 'patch install').action(() => {
2021

2122
cli.command('init').action(async () => {
2223
await init()
23-
console.log(`✨ ${configName}.config.ts initialized!`)
24+
logger.success(`✨ ${configName}.config.ts initialized!`)
2425
})
2526

2627
cli.command('extract').action(async () => {
2728
const { config } = await getConfig()
2829
if (config) {
2930
const twPatcher = new TailwindcssPatcher()
3031
const p = await twPatcher.extract(config.patch)
31-
console.log(`✨ tailwindcss-patch extract success! file path: ${p}`)
32+
logger.success(`✨ tailwindcss-patch extract success! file path: ${p}`)
3233
}
3334
})
3435

packages/tailwindcss-patch/src/core/cache.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import fs from 'node:fs'
21
import path from 'node:path'
32
import process from 'node:process'
3+
import fs from 'fs-extra'
44
import { pkgName } from '@/constants'
55
import type { CacheOptions, InternalCacheOptions } from '@/types'
6-
import { log } from '@/logger'
6+
import logger from '@/logger'
77

88
export function getCacheOptions(options?: CacheOptions | boolean) {
99
let cache: InternalCacheOptions
@@ -67,7 +67,7 @@ export class CacheManager {
6767
return filename
6868
}
6969
catch {
70-
log('write cache file fail!')
70+
logger.error('write cache file fail!')
7171
}
7272
}
7373

@@ -80,12 +80,12 @@ export class CacheManager {
8080
}
8181
}
8282
catch {
83-
log(`parse cache content fail! path:${filename}`)
83+
logger.error(`parse cache content fail! path:${filename}`)
8484
try {
8585
fs.unlinkSync(filename)
8686
}
8787
catch {
88-
log(`delete cache file fail! path:${filename}`)
88+
logger.error(`delete cache file fail! path:${filename}`)
8989
}
9090
}
9191
}

packages/tailwindcss-patch/src/core/patches/exportContext/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import path from 'node:path'
2-
import fs from 'node:fs'
2+
import fs from 'fs-extra'
33
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from './postcss'
44
import { inspectPostcssPlugin as inspectPostcssPluginCompat, inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-compat'
55
import type { InternalPatchOptions } from '@/types'
66
import { ensureFileContent } from '@/utils'
7+
import logger from '@/logger'
78

89
export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatchOptions) {
910
const processTailwindFeaturesFilePath = path.resolve(twDir, 'lib/processTailwindFeatures.js')
@@ -16,7 +17,7 @@ export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatc
1617
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
1718
encoding: 'utf8',
1819
})
19-
console.log('patch tailwindcss processTailwindFeatures for return content successfully!')
20+
logger.success('patch tailwindcss processTailwindFeatures for return content successfully!')
2021
}
2122
result.processTailwindFeatures = code
2223
}
@@ -30,7 +31,7 @@ export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatc
3031
fs.writeFileSync(pluginFilePath, code, {
3132
encoding: 'utf8',
3233
})
33-
console.log('patch tailwindcss for expose runtime content successfully!')
34+
logger.success('patch tailwindcss for expose runtime context successfully!')
3435
}
3536
result.plugin = code
3637
}
@@ -50,7 +51,7 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
5051
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
5152
encoding: 'utf8',
5253
})
53-
console.log('patch tailwindcss processTailwindFeatures for return content successfully!')
54+
logger.success('patch tailwindcss processTailwindFeatures for return content successfully!')
5455
}
5556
result.processTailwindFeatures = code
5657
}
@@ -63,7 +64,7 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
6364
fs.writeFileSync(indexFilePath, code, {
6465
encoding: 'utf8',
6566
})
66-
console.log('patch tailwindcss for expose runtime content successfully!')
67+
logger.success('patch tailwindcss for expose runtime content successfully!')
6768
}
6869
result.plugin = code
6970
}

packages/tailwindcss-patch/src/core/patches/supportCustomUnits/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import fs from 'node:fs'
21
import path from 'node:path'
2+
import fs from 'fs-extra'
33
import * as t from '@babel/types'
44
import type { ArrayExpression, StringLiteral } from '@babel/types'
55
import { defu } from 'defu'
66
import type { ILengthUnitsPatchOptions } from './types'
77
import { generate, parse, traverse } from '@/babel'
8+
import logger from '@/logger'
89

910
function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
1011
const { variableName, units } = options
@@ -77,7 +78,7 @@ export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: Pa
7778
fs.writeFileSync(destPath ?? dataTypesFilePath, newCode, {
7879
encoding: 'utf8',
7980
})
80-
console.log('patch tailwindcss for custom length unit successfully!')
81+
logger.success('patch tailwindcss for custom length unit successfully!')
8182
}
8283
}
8384
return code

packages/tailwindcss-patch/src/core/tw-patcher.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import path from 'node:path'
2-
import fs from 'node:fs'
2+
import fs from 'fs-extra'
33
import { CacheManager, getCacheOptions } from './cache'
44
import { internalPatch } from './runtime-patcher'
55
import { processTailwindcss } from './postcss'
66
import type { UserConfig } from '@/config'
77
import { getPatchOptions } from '@/defaults'
8-
import { ensureDir, getPackageInfoSync, isObject } from '@/utils'
8+
import { getPackageInfoSync, isObject } from '@/utils'
99
import type { CacheStrategy, InternalCacheOptions, InternalPatchOptions, PackageInfo, TailwindcssClassCache, TailwindcssPatcherOptions, TailwindcssRuntimeContext } from '@/types'
1010

1111
export class TailwindcssPatcher {
@@ -128,7 +128,7 @@ export class TailwindcssPatcher {
128128
removeUniversalSelector,
129129
})
130130
if (filename) {
131-
await ensureDir(path.dirname(filename))
131+
await fs.ensureDir(path.dirname(filename))
132132
const classList = [...set]
133133
fs.writeFileSync(filename, JSON.stringify(classList, null, loose ? 2 : undefined), 'utf8')
134134
return filename
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { pkgName } from './constants'
1+
import { createConsola } from 'consola'
22

3-
export function log(message?: any, ...optionalParams: any[]) {
4-
return console.log(`[${pkgName}]:${message}`, ...optionalParams)
5-
}
3+
const logger = createConsola()
4+
5+
export default logger

packages/tailwindcss-patch/src/utils.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import fss from 'node:fs'
2-
import fs from 'node:fs/promises'
31
import path from 'node:path'
2+
import fs from 'fs-extra'
43
import type { SyncOpts } from 'resolve'
54
import pkg from 'resolve'
65
import type { PackageJson } from 'pkg-types'
@@ -13,8 +12,8 @@ export function ensureFileContent(filepaths: string | string[]) {
1312
}
1413
let content
1514
for (const filepath of filepaths) {
16-
if (fss.existsSync(filepath)) {
17-
content = fss.readFileSync(filepath, {
15+
if (fs.existsSync(filepath)) {
16+
content = fs.readFileSync(filepath, {
1817
encoding: 'utf8',
1918
})
2019
break
@@ -27,17 +26,6 @@ export function requireResolve(id: string, opts?: SyncOpts) {
2726
return sync(id, opts)
2827
}
2928

30-
export async function ensureDir(p: string) {
31-
try {
32-
await fs.access(p)
33-
}
34-
catch {
35-
await fs.mkdir(p, {
36-
recursive: true,
37-
})
38-
}
39-
}
40-
4129
function searchPackageJSON(dir: string) {
4230
let packageJsonPath
4331
while (true) {
@@ -50,7 +38,7 @@ function searchPackageJSON(dir: string) {
5038
}
5139
dir = newDir
5240
packageJsonPath = path.join(dir, 'package.json')
53-
if (fss.existsSync(packageJsonPath)) {
41+
if (fs.existsSync(packageJsonPath)) {
5442
break
5543
}
5644
}
@@ -77,7 +65,7 @@ export function getPackageInfoSync(name: string, options: SyncOpts = {}) {
7765
return
7866
}
7967

80-
const packageJson: PackageJson = JSON.parse(fss.readFileSync(packageJsonPath, 'utf8'))
68+
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
8169

8270
return {
8371
name,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path'
2-
import fs from 'node:fs'
2+
import fs from 'fs-extra'
33
import { getCss } from './utils'
44
import { pkgName } from '@/constants'
55
import { CacheManager, TailwindcssPatcher } from '@/core'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { resolve } from 'node:path'
2-
import { existsSync } from 'node:fs'
2+
import { existsSync } from 'fs-extra'
33
import { deleteAsync } from 'del'
44
import { fixturesRoot } from './utils'
55
import { getConfig, getDefaultMangleUserConfig, getDefaultUserConfig, initConfig } from '@/config'

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path'
2-
import fs from 'node:fs'
2+
import fs from 'fs-extra'
33
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/patches/exportContext/postcss'
44

55
const tailwindcssCasePath = path.resolve(__dirname, 'fixtures')

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { existsSync, statSync } from 'node:fs'
22
import path from 'node:path'
33
import { deleteAsync } from 'del'
4+
import fs from 'fs-extra'
45
import { fixturesRoot } from './utils'
5-
import { ensureDir } from '@/utils'
66

77
describe('utils', () => {
88
it('ensureDir', async () => {
@@ -11,7 +11,7 @@ describe('utils', () => {
1111
await deleteAsync(dir)
1212
}
1313
expect(existsSync(dir)).toBe(false)
14-
await ensureDir(dir)
14+
await fs.ensureDir(dir)
1515
const stat = statSync(dir)
1616
expect(stat.isDirectory()).toBe(true)
1717
expect(existsSync(dir)).toBe(true)

packages/tailwindcss-patch/test/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fs from 'node:fs'
21
import path from 'node:path'
2+
import fs from 'fs-extra'
33
import postcss from 'postcss'
44
import tailwindcss from 'tailwindcss'
55

0 commit comments

Comments
 (0)