forked from unocss/unocss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.test.ts
184 lines (163 loc) · 5.05 KB
/
cli.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { resolve } from 'path'
import fs from 'fs-extra'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { startCli } from '../packages/cli/src/cli-start'
export const tempDir = resolve('.temp')
export const cli = resolve(__dirname, '../packages/cli/src/cli.ts')
beforeAll(async () => {
await fs.remove(tempDir)
})
afterAll(async () => {
await fs.remove(tempDir)
})
describe('cli', () => {
it('builds uno.css', async () => {
const { output } = await runCli({
'views/index.html': '<div class="p-4 max-w-screen-md"></div>',
})
expect(output).toMatchSnapshot()
})
it('supports unocss.config.js', async () => {
const { output } = await runCli({
'views/index.html': '<div class="box"></div>',
'unocss.config.js': `
import { defineConfig } from 'unocss'
export default defineConfig({
shortcuts: [{ box: 'max-w-7xl mx-auto bg-gray-100 rounded-md shadow-sm p-4' }]
})
`.trim(),
})
expect(output).toMatchSnapshot()
})
it('supports variantGroup transformer', async () => {
const { output, transform } = await runCli({
'views/index.html': '<div class="p-4 border-(solid red)"></div>',
'unocss.config.js': `
import { defineConfig, transformerVariantGroup } from 'unocss'
export default defineConfig({
transformers: [transformerVariantGroup()]
})
`.trim(),
}, 'views/index.html')
expect(output).toMatchSnapshot()
expect(transform).toMatchSnapshot()
})
it('supports directives transformer', async () => {
const { output, transform } = await runCli({
'views/index.css': '.btn-center{@apply text-center my-0 font-medium;}',
'unocss.config.js': `
import { defineConfig, transformerDirectives } from 'unocss'
export default defineConfig({
transformers: [transformerDirectives()]
})
`.trim(),
}, 'views/index.css')
expect(output).toMatchSnapshot()
expect(transform).toMatchSnapshot()
})
it('uno.css exclude initialized class after changing file', async () => {
const fileName = 'views/index.html'
const initializedContent = '<div class="bg-blue"></div>'
const changedContent = '<div class="bg-red"></div>'
const testDir = getTestDir()
const absolutePathOfFile = resolve(testDir, fileName)
await fs.outputFile(absolutePathOfFile, initializedContent)
await runAsyncChildProcess(testDir, './views/**/*', '-w')
const outputPath = resolve(testDir, 'uno.css')
for (let i = 50; i >= 0; i--) {
await sleep(50)
if (fs.existsSync(outputPath))
break
}
await fs.writeFile(absolutePathOfFile, changedContent)
// polling until update
for (let i = 100; i >= 0; i--) {
await sleep(100)
const output = await readFile(testDir)
if (i === 0 || output.includes('.bg-red')) {
expect(output).toContain('.bg-red')
break
}
}
})
it('supports unocss.config.js cli options', async () => {
const testDir = getTestDir()
const outFiles = ['./uno1.css', './test/uno2.css']
const files = [
{
path: 'views/index1.html',
content: '<div class="bg-blue"></div>',
},
{
path: 'views/index2.html',
content: '<div class="bg-red"></div>',
},
{
path: 'unocss.config.js',
content: `
import { defineConfig } from 'unocss'
export default defineConfig({
cli: {
entry: [
{
patterns: ['views/index1.html'],
outFile: '${outFiles[0]}',
},
{
patterns: ['views/index2.html'],
outFile: '${outFiles[1]}',
},
],
}
})
`.trim(),
},
]
await Promise.all(files.map(({ path, content }) => fs.outputFile(resolve(testDir, path), content)))
await runAsyncChildProcess(testDir, '', '')
await sleep(500)
const [output1, output2] = await Promise.all(outFiles.map(async file => readFile(testDir, resolve(testDir, file))))
expect(output1).toContain('.bg-blue')
expect(output2).toContain('.bg-red')
})
})
// ----- Utils -----
function sleep(time = 300) {
return new Promise<void>((resolve) => {
setTimeout(() => {
resolve()
}, time)
})
}
function getTestDir() {
return resolve(tempDir, Math.round(Math.random() * 100000).toString())
}
function initOutputFiles(testDir: string, files: Record<string, string>) {
return Promise.all(
Object.entries(files).map(([path, content]) =>
fs.outputFile(resolve(testDir, path), content, 'utf8'),
),
)
}
function runAsyncChildProcess(cwd: string, ...args: string[]) {
return startCli(cwd, ['', '', ...args, '--no-preflights'])
}
function readFile(testDir: string, targetFile?: string) {
return fs.readFile(resolve(testDir, targetFile ?? 'uno.css'), 'utf8')
}
async function runCli(files: Record<string, string>, transformFile?: string) {
const testDir = getTestDir()
await initOutputFiles(testDir, files)
await runAsyncChildProcess(testDir, 'views/**/*')
const output = await readFile(testDir)
if (transformFile) {
const transform = await readFile(testDir, transformFile)
return {
output,
transform,
}
}
return {
output,
}
}