Skip to content

Commit

Permalink
feat(tailwindcss-patch): add fs-extra and consola as deps
Browse files Browse the repository at this point in the history
  • Loading branch information
sonofmagic committed Jul 18, 2024
1 parent 6032164 commit d74ad68
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 86 deletions.
2 changes: 1 addition & 1 deletion packages/config/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'node:path'
import { existsSync } from 'node:fs'
import { existsSync } from 'fs-extra'
import { deleteAsync } from 'del'
import { fixturesRoot } from './utils'
import { getConfig, initConfig } from '@/index'
Expand Down
2 changes: 2 additions & 0 deletions packages/tailwindcss-patch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
"@babel/types": "^7.24.9",
"@tailwindcss-mangle/config": "workspace:^",
"cac": "^6.7.14",
"consola": "^3.2.3",
"fs-extra": "^11.2.0",
"jiti": "^1.21.6",
"lilconfig": "^3.1.2",
"postcss": "^8.4.38",
Expand Down
5 changes: 3 additions & 2 deletions packages/tailwindcss-patch/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import process from 'node:process'
import cac from 'cac'
import { configName, getConfig, initConfig } from './config'
import { TailwindcssPatcher } from './core'
import logger from './logger'
import { getPatchOptions } from '@/defaults'

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

cli.command('init').action(async () => {
await init()
console.log(`✨ ${configName}.config.ts initialized!`)
logger.success(`✨ ${configName}.config.ts initialized!`)
})

cli.command('extract').action(async () => {
const { config } = await getConfig()
if (config) {
const twPatcher = new TailwindcssPatcher()
const p = await twPatcher.extract(config.patch)
console.log(`✨ tailwindcss-patch extract success! file path: ${p}`)
logger.success(`✨ tailwindcss-patch extract success! file path: ${p}`)
}
})

Expand Down
10 changes: 5 additions & 5 deletions packages/tailwindcss-patch/src/core/cache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import fs from 'fs-extra'
import { pkgName } from '@/constants'
import type { CacheOptions, InternalCacheOptions } from '@/types'
import { log } from '@/logger'
import logger from '@/logger'

export function getCacheOptions(options?: CacheOptions | boolean) {
let cache: InternalCacheOptions
Expand Down Expand Up @@ -67,7 +67,7 @@ export class CacheManager {
return filename
}
catch {
log('write cache file fail!')
logger.error('write cache file fail!')
}
}

Expand All @@ -80,12 +80,12 @@ export class CacheManager {
}
}
catch {
log(`parse cache content fail! path:${filename}`)
logger.error(`parse cache content fail! path:${filename}`)
try {
fs.unlinkSync(filename)
}
catch {
log(`delete cache file fail! path:${filename}`)
logger.error(`delete cache file fail! path:${filename}`)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import path from 'node:path'
import fs from 'node:fs'
import fs from 'fs-extra'
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from './postcss'
import { inspectPostcssPlugin as inspectPostcssPluginCompat, inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-compat'
import type { InternalPatchOptions } from '@/types'
import { ensureFileContent } from '@/utils'
import logger from '@/logger'

export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatchOptions) {
const processTailwindFeaturesFilePath = path.resolve(twDir, 'lib/processTailwindFeatures.js')
Expand All @@ -16,7 +17,7 @@ export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatc
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss processTailwindFeatures for return content successfully!')
logger.success('patch tailwindcss processTailwindFeatures for return content successfully!')
}
result.processTailwindFeatures = code
}
Expand All @@ -30,7 +31,7 @@ export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatc
fs.writeFileSync(pluginFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss for expose runtime content successfully!')
logger.success('patch tailwindcss for expose runtime context successfully!')
}
result.plugin = code
}
Expand All @@ -50,7 +51,7 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
fs.writeFileSync(processTailwindFeaturesFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss processTailwindFeatures for return content successfully!')
logger.success('patch tailwindcss processTailwindFeatures for return content successfully!')
}
result.processTailwindFeatures = code
}
Expand All @@ -63,7 +64,7 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
fs.writeFileSync(indexFilePath, code, {
encoding: 'utf8',
})
console.log('patch tailwindcss for expose runtime content successfully!')
logger.success('patch tailwindcss for expose runtime content successfully!')
}
result.plugin = code
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import fs from 'node:fs'
import path from 'node:path'
import fs from 'fs-extra'
import * as t from '@babel/types'
import type { ArrayExpression, StringLiteral } from '@babel/types'
import { defu } from 'defu'
import type { ILengthUnitsPatchOptions } from './types'
import { generate, parse, traverse } from '@/babel'
import logger from '@/logger'

function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
const { variableName, units } = options
Expand Down Expand Up @@ -77,7 +78,7 @@ export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: Pa
fs.writeFileSync(destPath ?? dataTypesFilePath, newCode, {
encoding: 'utf8',
})
console.log('patch tailwindcss for custom length unit successfully!')
logger.success('patch tailwindcss for custom length unit successfully!')
}
}
return code
Expand Down
6 changes: 3 additions & 3 deletions packages/tailwindcss-patch/src/core/tw-patcher.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import path from 'node:path'
import fs from 'node:fs'
import fs from 'fs-extra'
import { CacheManager, getCacheOptions } from './cache'
import { internalPatch } from './runtime-patcher'
import { processTailwindcss } from './postcss'
import type { UserConfig } from '@/config'
import { getPatchOptions } from '@/defaults'
import { ensureDir, getPackageInfoSync, isObject } from '@/utils'
import { getPackageInfoSync, isObject } from '@/utils'
import type { CacheStrategy, InternalCacheOptions, InternalPatchOptions, PackageInfo, TailwindcssClassCache, TailwindcssPatcherOptions, TailwindcssRuntimeContext } from '@/types'

export class TailwindcssPatcher {
Expand Down Expand Up @@ -128,7 +128,7 @@ export class TailwindcssPatcher {
removeUniversalSelector,
})
if (filename) {
await ensureDir(path.dirname(filename))
await fs.ensureDir(path.dirname(filename))
const classList = [...set]
fs.writeFileSync(filename, JSON.stringify(classList, null, loose ? 2 : undefined), 'utf8')
return filename
Expand Down
8 changes: 4 additions & 4 deletions packages/tailwindcss-patch/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pkgName } from './constants'
import { createConsola } from 'consola'

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

export default logger
22 changes: 5 additions & 17 deletions packages/tailwindcss-patch/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fss from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
import fs from 'fs-extra'
import type { SyncOpts } from 'resolve'
import pkg from 'resolve'
import type { PackageJson } from 'pkg-types'
Expand All @@ -13,8 +12,8 @@ export function ensureFileContent(filepaths: string | string[]) {
}
let content
for (const filepath of filepaths) {
if (fss.existsSync(filepath)) {
content = fss.readFileSync(filepath, {
if (fs.existsSync(filepath)) {
content = fs.readFileSync(filepath, {
encoding: 'utf8',
})
break
Expand All @@ -27,17 +26,6 @@ export function requireResolve(id: string, opts?: SyncOpts) {
return sync(id, opts)
}

export async function ensureDir(p: string) {
try {
await fs.access(p)
}
catch {
await fs.mkdir(p, {
recursive: true,
})
}
}

function searchPackageJSON(dir: string) {
let packageJsonPath
while (true) {
Expand All @@ -50,7 +38,7 @@ function searchPackageJSON(dir: string) {
}
dir = newDir
packageJsonPath = path.join(dir, 'package.json')
if (fss.existsSync(packageJsonPath)) {
if (fs.existsSync(packageJsonPath)) {
break
}
}
Expand All @@ -77,7 +65,7 @@ export function getPackageInfoSync(name: string, options: SyncOpts = {}) {
return
}

const packageJson: PackageJson = JSON.parse(fss.readFileSync(packageJsonPath, 'utf8'))
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))

return {
name,
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-patch/test/cache.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path'
import fs from 'node:fs'
import fs from 'fs-extra'
import { getCss } from './utils'
import { pkgName } from '@/constants'
import { CacheManager, TailwindcssPatcher } from '@/core'
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-patch/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'node:path'
import { existsSync } from 'node:fs'
import { existsSync } from 'fs-extra'
import { deleteAsync } from 'del'
import { fixturesRoot } from './utils'
import { getConfig, getDefaultMangleUserConfig, getDefaultUserConfig, initConfig } from '@/config'
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-patch/test/inspector.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'node:path'
import fs from 'node:fs'
import fs from 'fs-extra'
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from '@/core/patches/exportContext/postcss'

const tailwindcssCasePath = path.resolve(__dirname, 'fixtures')
Expand Down
4 changes: 2 additions & 2 deletions packages/tailwindcss-patch/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { existsSync, statSync } from 'node:fs'
import path from 'node:path'
import { deleteAsync } from 'del'
import fs from 'fs-extra'
import { fixturesRoot } from './utils'
import { ensureDir } from '@/utils'

describe('utils', () => {
it('ensureDir', async () => {
Expand All @@ -11,7 +11,7 @@ describe('utils', () => {
await deleteAsync(dir)
}
expect(existsSync(dir)).toBe(false)
await ensureDir(dir)
await fs.ensureDir(dir)
const stat = statSync(dir)
expect(stat.isDirectory()).toBe(true)
expect(existsSync(dir)).toBe(true)
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss-patch/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs'
import path from 'node:path'
import fs from 'fs-extra'
import postcss from 'postcss'
import tailwindcss from 'tailwindcss'

Expand Down
Loading

0 comments on commit d74ad68

Please sign in to comment.