Files
font2pic/scripts/check-font-assets.js
2026-07-26 10:44:05 +08:00

58 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* 校验字体清单中的每个字体文件是否存在且非空。
*
* 用法node scripts/check-font-assets.js --manifest <清单路径> --root <项目根目录>
*/
const fs = require('fs')
const path = require('path')
function readArgument(name) {
const index = process.argv.indexOf(name)
return index >= 0 ? process.argv[index + 1] : ''
}
const manifestPath = readArgument('--manifest')
const projectRoot = readArgument('--root') || '.'
if (!manifestPath) {
console.error('用法错误:缺少 --manifest 参数')
process.exit(1)
}
let manifest
try {
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))
} catch (error) {
console.error(`无法读取字体清单 ${manifestPath}${error.message}`)
process.exit(1)
}
const items = Array.isArray(manifest) ? manifest : (manifest.items || manifest.fonts || [])
if (!Array.isArray(items) || items.length === 0) {
console.error(`字体清单为空:${manifestPath}`)
process.exit(1)
}
const missing = []
for (const item of items) {
const relativePath = String(item.path || '').replace(/^\/+/, '')
const fontPath = path.resolve(projectRoot, relativePath)
if (!relativePath || !fs.existsSync(fontPath) || fs.statSync(fontPath).size === 0) {
missing.push(`${item.id || '未知ID'} ${item.name || '未知字体'} (${relativePath || '未配置路径'})`)
}
}
if (missing.length > 0) {
console.error(`字体资源校验失败:缺少或为空 ${missing.length}/${items.length}`)
missing.slice(0, 20).forEach((item) => console.error(`- ${item}`))
if (missing.length > 20) {
console.error(`- 其余 ${missing.length - 20} 个未显示`)
}
process.exit(1)
}
console.log(`字体资源校验通过:${items.length} 个字体文件均存在且非空`)