forked from
npmx.dev/npmx.dev
[READ-ONLY]
a fast, modern browser for the npm registry
1import type { Dirent } from 'node:fs'
2import { glob, readFile } from 'node:fs/promises'
3import { fileURLToPath } from 'node:url'
4import { resolve } from 'node:path'
5import { createGenerator } from 'unocss'
6import { presetRtl } from '../uno-preset-rtl.ts'
7import { presetA11y } from '../uno-preset-a11y.ts'
8import { COLORS } from './utils.ts'
9import { presetWind4 } from 'unocss'
10
11const argvFiles = process.argv.slice(2)
12const APP_DIRECTORY = fileURLToPath(new URL('../app', import.meta.url))
13
14async function checkFile(path: Dirent): Promise<string | undefined> {
15 if (path.isDirectory() || !path.name.endsWith('.vue')) {
16 return undefined
17 }
18
19 const filename = resolve(APP_DIRECTORY, path.parentPath, path.name)
20 const file = await readFile(filename, 'utf-8')
21 let idx = -1
22 let line: string
23 const warnings = new Map<number, string[]>()
24 const uno = await createGenerator({
25 presets: [
26 presetWind4(),
27 presetRtl((warning, rule) => {
28 let entry = warnings.get(idx)
29 if (!entry) {
30 entry = []
31 warnings.set(idx, entry)
32 }
33 const ruleIdx = line.indexOf(rule)
34 entry.push(
35 `${COLORS.red} ❌ [RTL] ${filename}:${idx}${ruleIdx > -1 ? `:${ruleIdx + 1}` : ''} - ${warning}${COLORS.reset}`,
36 )
37 }),
38 presetA11y((warning, rule) => {
39 let entry = warnings.get(idx)
40 if (!entry) {
41 entry = []
42 warnings.set(idx, entry)
43 }
44 const ruleIdx = line.indexOf(rule)
45 entry.push(
46 `${COLORS.red} ❌ [A11y] ${filename}:${idx}${ruleIdx > -1 ? `:${ruleIdx + 1}` : ''} - ${warning}${COLORS.reset}`,
47 )
48 }),
49 ],
50 })
51 const lines = file.split('\n')
52 for (let i = 0; i < lines.length; i++) {
53 idx = i + 1
54 line = lines[i]
55 await uno.generate(line)
56 }
57
58 return warnings.size > 0 ? Array.from(warnings.values()).flat().join('\n') : undefined
59}
60
61async function check(): Promise<void> {
62 const dir = glob(argvFiles.length > 0 ? argvFiles : '**/*.vue', {
63 withFileTypes: true,
64 cwd: APP_DIRECTORY,
65 })
66 let hasErrors = false
67 for await (const file of dir) {
68 const result = await checkFile(file)
69 if (result) {
70 hasErrors = true
71 // oxlint-disable-next-line no-console -- warn logging
72 console.error(result)
73 }
74 }
75
76 if (hasErrors) {
77 process.exit(1)
78 } else {
79 // oxlint-disable-next-line no-console -- success logging
80 console.log(`${COLORS.green}✅ CSS check passed!${COLORS.reset}`)
81 }
82}
83
84check()