forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-css-props.ts
More file actions
executable file
·55 lines (47 loc) · 1.45 KB
/
check-css-props.ts
File metadata and controls
executable file
·55 lines (47 loc) · 1.45 KB
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
// Check that all design tokens (CSS Custom Properties) are used
import { globSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { extname, join } from 'node:path'
import { styleText } from 'node:util'
import postcss, { type Parser } from 'postcss'
import html from 'postcss-html'
import { getPropsError, propsChecker } from '../postcss/props-checker.ts'
function printError(message: string): void {
process.stderr.write(styleText('red', message) + '\n')
}
function printWarning(message: string): void {
process.stderr.write(styleText('yellow', message) + '\n')
}
const cssChecker = postcss([propsChecker])
let files = [
...globSync(join(import.meta.dirname, '..', 'dist', '**', '*.css')),
join(import.meta.dirname, '..', 'dist', 'index.html')
]
await Promise.all(
files.map(async file => {
let content = await readFile(file)
try {
let parser: Parser | undefined
if (extname(file) === '.html') parser = html.parse
await cssChecker.process(content, { from: file, parser })
} catch (e) {
if (!(e instanceof Error)) {
printError(String(e))
} else if (e instanceof Error && e.name === 'CssSyntaxError') {
printError(e.message)
} else {
printError(e.stack ?? e.message)
}
process.exit(1)
}
})
)
let error = getPropsError(true)
if (error) {
printError(error)
process.exit(1)
}
let warning = getPropsError(false)
if (warning) {
printWarning(warning)
}