58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
#!/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} 个字体文件均存在且非空`)
|