refactor(release-cleanup): 收斂錯誤輸出並抽出驗證規則常數

- 新增 logger.failError 集中處理 Error/非 Error 的 stderr 輸出與堆疊,
  index.js 改用之,移除重複的 reportFatal
- validate.js 將允許協定與 repository 格式抽為模組常數,便於維護擴充

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-06-26 11:04:29 +08:00
co-authored by Claude Opus 4.8
parent 0f4354bd01
commit f6d2ebeab2
3 changed files with 26 additions and 20 deletions
+2 -18
View File
@@ -4,7 +4,7 @@ import { loadConfig } from './config.js'
import { GiteaClient } from './gitea-client.js'
import { cleanupReleases } from './releases.js'
import { cleanupOrphanTags } from './tags.js'
import { separator, fail } from './logger.js'
import { separator, failError } from './logger.js'
/**
* Action 主流程:讀取並驗證環境設定、建立帶認證的 Gitea 客戶端,
@@ -24,26 +24,10 @@ export async function main() {
separator()
}
/**
* 將錯誤輸出至 stderr,並盡量保留可供除錯的上下文(錯誤類型與堆疊),
* 以便區分設定驗證錯誤與網路/API 請求錯誤。
* @param {unknown} error 捕捉到的錯誤
*/
function reportFatal(error) {
if (error instanceof Error) {
fail(`${error.name}: ${error.message}`)
if (error.stack) {
process.stderr.write(`${error.stack}\n`)
}
} else {
fail(String(error))
}
}
// 僅在直接以 `node index.js` 執行時啟動主流程;被測試 import 時不自動執行,方便撰寫整合測試。
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch((error) => {
reportFatal(error)
failError(error)
process.exit(1)
})
}