mirror of
https://git.zhongyuan.online/ytke/tap.git
synced 2026-11-24 22:15:18 -08:00
15 lines
1.1 KiB
JavaScript
15 lines
1.1 KiB
JavaScript
import { createHash } from 'node:crypto';
|
|||
|
|
import { readFile, readdir } from 'node:fs/promises';
|
||
|
|
import { resolve } from 'node:path';
|
||
|
|
const directory=resolve('content/word');
|
||
|
|
const manifest=JSON.parse(await readFile(resolve(directory,'manifest.json'),'utf8'));
|
||
|
|
const sources=(await readdir(resolve('content/docs'))).filter(name=>name.endsWith('.md')).sort();
|
||
|
|
if(JSON.stringify(manifest.map(item=>item.source).sort())!==JSON.stringify(sources))throw new Error('模板源文件列表已变化,请重新生成 Word 模板。');
|
||
|
|
for(const item of manifest){
|
||
|
|
const source=await readFile(resolve('content/docs',item.source));
|
||
|
|
const output=await readFile(resolve(directory,item.file));
|
||
|
|
const digest=bytes=>createHash('sha256').update(bytes).digest('hex');
|
||
|
|
if(digest(source)!==item.sourceSha256||digest(output)!==item.fileSha256)throw new Error('Word 模板与正文不同步,请重新生成:'+item.source);
|
||
|
|
if(output[0]!==0x50||output[1]!==0x4b)throw new Error('Word 模板文件格式无效:'+item.file);
|
||
|
|
}
|
||
|
|
console.log(`Verified ${manifest.length} Word templates against their source documents.`);
|