136 lines
3.8 KiB
JavaScript
136 lines
3.8 KiB
JavaScript
#!/usr/bin/env node
|
|||
|
|
import fs from 'node:fs/promises'
|
||
|
|
import path from 'node:path'
|
||
|
|
import process from 'node:process'
|
||
|
|
import { TextDecoder } from 'node:util'
|
||
|
|
|
||
|
|
const repoRoot = process.cwd()
|
||
|
|
const defaultTargets = [
|
||
|
|
'docx',
|
||
|
|
'scripts/sql',
|
||
|
|
'YumiNail-Core/src/main/resources/files/YumiNail.sql'
|
||
|
|
]
|
||
|
|
const includedExtensions = new Set(['.md', '.sql', '.txt'])
|
||
|
|
const excludedSegments = new Set(['.git', 'node_modules', 'target', 'dist'])
|
||
|
|
const utf8Decoder = new TextDecoder('utf-8', { fatal: true })
|
||
|
|
const latinMojibakePattern = /(?:Ã.|Â.|â[€\u0080-\u009F]?)/u
|
||
|
|
const gbkMojibakeTokens = [
|
||
|
|
'鏂', '鏄', '鐨', '璇', '鍙', '鎴', '閫', '绛', '锛', '銆',
|
||
|
|
'乂', '浠', '鍗', '灏', '涓', '闂', '妯', '瀹', '搴', '鐢',
|
||
|
|
'鍓', '鍚', '骞', '绾', '濡', '浜', '娴', '槑', '绋', '湪'
|
||
|
|
]
|
||
|
|
|
||
|
|
async function collectFiles(targets) {
|
||
|
|
const files = []
|
||
|
|
for (const target of targets) {
|
||
|
|
const absoluteTarget = path.resolve(repoRoot, target)
|
||
|
|
const stat = await fs.stat(absoluteTarget).catch(() => null)
|
||
|
|
if (!stat) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if (stat.isDirectory()) {
|
||
|
|
await walkDirectory(absoluteTarget, files)
|
||
|
|
} else if (shouldScanFile(absoluteTarget)) {
|
||
|
|
files.push(absoluteTarget)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return files
|
||
|
|
}
|
||
|
|
|
||
|
|
async function walkDirectory(directory, files) {
|
||
|
|
const entries = await fs.readdir(directory, { withFileTypes: true })
|
||
|
|
for (const entry of entries) {
|
||
|
|
const absolutePath = path.join(directory, entry.name)
|
||
|
|
const relativePath = path.relative(repoRoot, absolutePath).replaceAll('\\', '/')
|
||
|
|
if (isExcluded(relativePath)) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if (entry.isDirectory()) {
|
||
|
|
await walkDirectory(absolutePath, files)
|
||
|
|
} else if (entry.isFile() && shouldScanFile(absolutePath)) {
|
||
|
|
files.push(absolutePath)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function isExcluded(relativePath) {
|
||
|
|
return relativePath.split('/').some(segment => excludedSegments.has(segment))
|
||
|
|
}
|
||
|
|
|
||
|
|
function shouldScanFile(filePath) {
|
||
|
|
return includedExtensions.has(path.extname(filePath).toLowerCase())
|
||
|
|
}
|
||
|
|
|
||
|
|
async function checkFile(filePath) {
|
||
|
|
const bytes = await fs.readFile(filePath)
|
||
|
|
let text
|
||
|
|
try {
|
||
|
|
text = utf8Decoder.decode(bytes)
|
||
|
|
} catch (error) {
|
||
|
|
return [{
|
||
|
|
filePath,
|
||
|
|
line: 1,
|
||
|
|
reason: `not valid UTF-8: ${error.message}`
|
||
|
|
}]
|
||
|
|
}
|
||
|
|
|
||
|
|
const issues = []
|
||
|
|
const lines = text.split(/\r?\n/)
|
||
|
|
lines.forEach((lineText, index) => {
|
||
|
|
const reason = detectIssue(lineText)
|
||
|
|
if (reason) {
|
||
|
|
issues.push({
|
||
|
|
filePath,
|
||
|
|
line: index + 1,
|
||
|
|
reason
|
||
|
|
})
|
||
|
|
}
|
||
|
|
})
|
||
|
|
return issues
|
||
|
|
}
|
||
|
|
|
||
|
|
function detectIssue(lineText) {
|
||
|
|
if (lineText.includes('\uFFFD')) {
|
||
|
|
return 'contains replacement character'
|
||
|
|
}
|
||
|
|
if (latinMojibakePattern.test(lineText)) {
|
||
|
|
return 'contains Latin-1 mojibake fragment'
|
||
|
|
}
|
||
|
|
const gbkTokenHits = gbkMojibakeTokens.reduce(
|
||
|
|
(count, token) => count + countOccurrences(lineText, token),
|
||
|
|
0
|
||
|
|
)
|
||
|
|
if (gbkTokenHits >= 2) {
|
||
|
|
return 'contains likely GBK/UTF-8 mojibake fragments'
|
||
|
|
}
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
|
||
|
|
function countOccurrences(text, token) {
|
||
|
|
let count = 0
|
||
|
|
let index = text.indexOf(token)
|
||
|
|
while (index !== -1) {
|
||
|
|
count += 1
|
||
|
|
index = text.indexOf(token, index + token.length)
|
||
|
|
}
|
||
|
|
return count
|
||
|
|
}
|
||
|
|
|
||
|
|
const targets = process.argv.slice(2)
|
||
|
|
const files = await collectFiles(targets.length > 0 ? targets : defaultTargets)
|
||
|
|
const issues = []
|
||
|
|
for (const filePath of files) {
|
||
|
|
issues.push(...await checkFile(filePath))
|
||
|
|
}
|
||
|
|
|
||
|
|
if (issues.length > 0) {
|
||
|
|
for (const issue of issues) {
|
||
|
|
const relativePath = path.relative(repoRoot, issue.filePath).replaceAll('\\', '/')
|
||
|
|
console.error(`${relativePath}:${issue.line}: ${issue.reason}`)
|
||
|
|
}
|
||
|
|
console.error(`Encoding check failed: ${issues.length} issue(s) in ${files.length} file(s).`)
|
||
|
|
process.exit(1)
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(`Encoding check passed: ${files.length} file(s) are valid UTF-8 without common mojibake.`)
|