改用 node:url 的 pathToFileURL(process.argv[1]).href 比對,正確處理含空白/ 特殊字元路徑與跨平台差異,取代脆弱的手動 file:// 字串拼接。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
// 進入點:載入設定、建立 Gitea 客戶端,依序清理舊成品與孤立 tag。
|
|
|
|
import { pathToFileURL } from 'node:url'
|
|
import { loadConfig } from './config.js'
|
|
import { GiteaClient } from './gitea-client.js'
|
|
import { cleanupReleases } from './releases.js'
|
|
import { cleanupOrphanTags } from './tags.js'
|
|
import { separator, failError } from './logger.js'
|
|
|
|
/**
|
|
* Action 主流程:讀取並驗證環境設定、建立帶認證的 Gitea 客戶端,
|
|
* 先清理超出保留數量的舊 release,再清理未被任何 release 指定的孤立 tag,最後輸出結尾分隔線。
|
|
*
|
|
* 任一步驟拋出的例外不在此攔截,改由模組層級的 `main().catch(...)` 統一處理並以非零狀態結束。
|
|
*
|
|
* @returns {Promise<void>} 流程完成時 resolve;設定驗證或讀取 API 失敗時 reject。
|
|
*/
|
|
export async function main() {
|
|
const config = loadConfig()
|
|
const client = new GiteaClient({ token: config.token })
|
|
|
|
await cleanupReleases(client, config)
|
|
await cleanupOrphanTags(client, config)
|
|
|
|
separator()
|
|
}
|
|
|
|
// 僅在直接以 `node index.js` 執行時啟動主流程;被測試 import 時不自動執行,方便撰寫整合測試。
|
|
// 以 pathToFileURL 正確處理含空白/特殊字元的路徑與跨平台差異,避免手動拼接 file:// 的脆弱性。
|
|
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
main().catch((error) => {
|
|
failError(error)
|
|
process.exit(1)
|
|
})
|
|
}
|