feat: enhance JSON format validation with backup and reset mechanism on error

This commit is contained in:
2026-05-13 01:06:11 +00:00
parent 49a02ebb6b
commit 6c6680fd3e
3 changed files with 20 additions and 12 deletions
+10 -2
View File
@@ -110,8 +110,16 @@ async function main() {
JSON.parse(fs.readFileSync(fullPath, 'utf8'));
console.log(`${relPath} JSON 格式正確`);
} catch (e) {
console.error(`${relPath} JSON 格式錯誤: ${e.message}`);
process.exit(1);
console.error(`${relPath} JSON 格式錯誤: ${e.message},嘗試修正...`);
try {
const backupPath = fullPath + '.bak';
fs.copyFileSync(fullPath, backupPath);
fs.writeFileSync(fullPath, '[]\n', 'utf8');
console.log(`${relPath} 已重置為空陣列(原檔備份至 ${relPath}.bak`);
} catch (repairErr) {
console.error(`${relPath} 修正失敗: ${repairErr.message}`);
process.exit(1);
}
}
}