forked from remy/mit-license
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
80 lines (68 loc) · 2.17 KB
/
test.js
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
import {promises as fs} from 'node:fs'
import process from 'node:process'
import {writeJsonFile} from 'write-json-file'
import {parse as parseCSS} from 'css'
import hasFlag from 'has-flag'
import getExtension from 'file-ext'
import path from 'path-extra'
import is from '@sindresorhus/is'
import isDomainId from './utils/is-domain-id.js'
async function report(content, fix) {
console.error(content)
if (fix && hasFlag('--fix')) {
await fix()
}
process.exitCode = 1
}
const users = await fs.readdir('users')
for await (const user of users) {
if (getExtension(user) !== 'json') {
await report(`${user} is not a json file`, async () => {
await fs.unlink(user)
})
}
if (!isDomainId(path.base(user))) {
await report(`${user} is not a valid domain id.`)
}
try {
const data = await fs.readFile(path.join('users', user), 'utf8')
try {
const parsedData = JSON.parse(data)
if (!parsedData.locked && !parsedData.copyright) {
report(`Copyright not specified in ${user}`)
}
if (parsedData.version) {
await report(`Version tag found in ${user}`, async () => {
delete parsedData.version
await writeJsonFile(path.join('users', user), parsedData, {indent: 2})
})
}
if (is.string(parsedData.gravatar)) {
await report(`Gravatar boolean encoded as string found in ${user}`, async () => {
parsedData.gravatar = parsedData.gravatar === 'true'
const stringified = `${JSON.stringify(parsedData, 0, 2)}\n`
await fs.writeFile(path.join('users', user), stringified)
})
}
} catch ({message}) {
report(`Invalid JSON in ${user} (${message})`)
}
} catch ({message}) {
report(`Unable to read ${user} (${message})`)
}
}
const themes = await fs.readdir('themes')
for await (const theme of themes) {
if (getExtension(theme) === 'css') {
try {
const cssData = await fs.readFile(path.join('themes', theme), 'utf8')
try {
parseCSS(cssData)
} catch ({message}) {
report(`Invalid CSS in ${theme} (${message})`)
}
} catch ({message}) {
report(`Unable to read ${theme} (${message})`)
}
}
}