Files
calculate-version/app/index.js
T

70 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const logger = require('./logger');
const { loadConfig } = require('./config');
const { fetchReleases } = require('./releases');
const { calculateVersion } = require('./version');
const { writeOutput } = require('./output');
/**
* Action 進入點:協調設定載入、release 取得、版本號計算與輸出寫出。
*
* 失敗時(loadConfig 或 fetchReleases 拋錯)直接向外拋出例外,由呼叫端決定如何結束,
* 以利單元測試覆蓋失敗路徑。相依模組可透過 deps 注入,預設使用各兄弟模組的實作。
*
* @param {Object} [deps={}] - 可注入的相依,供測試替換。
* @param {Function} [deps.loadConfig] - 載入設定的函式。
* @param {Function} [deps.fetchReleases] - 取得 release 的函式。
* @param {Function} [deps.calculateVersion] - 計算版本號的函式。
* @param {Function} [deps.writeOutput] - 寫出 output 的函式。
* @param {{section:Function, info:Function, error:Function}} [deps.log] - log 記錄器。
* @returns {Promise<string>} 計算出的版本號。
* @throws {Error} 當任一注入相依(loadConfigfetchReleasescalculateVersionwriteOutput)拋出例外時,
* 原樣向外傳播,由呼叫端決定如何結束。
* @remarks 由模組底部的 require.main 守衛在被直接執行時呼叫;失敗時頂層以模組層級 logger.error 輸出
* 並 exit(1)。單元測試可透過 deps 注入替身以覆蓋成功與各失敗路徑。
*/
async function main(deps = {}) {
const {
loadConfig: loadConfigFn = loadConfig,
fetchReleases: fetchReleasesFn = fetchReleases,
calculateVersion: calculateVersionFn = calculateVersion,
writeOutput: writeOutputFn = writeOutput,
log = logger,
} = deps;
log.section('參數檢查');
const config = loadConfigFn();
log.info(`GITEA_SERVER_URL=${config.serverUrl}`);
log.info(`GITEA_REPOSITORY=${config.repository}`);
log.info(config.token ? 'GITEA_TOKEN=***' : 'GITEA_TOKEN=未提供');
log.info(`IS_BETA=${config.isBeta}`);
log.section('取得版本資料');
const releaseUrl = `${config.serverUrl}/api/v1/repos/${config.repository}/releases`;
log.info(`RELEASE_URL=${releaseUrl}`);
const releases = await fetchReleasesFn(releaseUrl, { token: config.token, logger: log });
const { latest, version } = calculateVersionFn(releases, config.isBeta);
log.info(`LATEST_VERSION=${latest}`);
log.section('計算版本號');
log.info(`NEW_VERSION=${version}`);
writeOutputFn('version', version);
return version;
}
// 僅在被直接執行時啟動(被 require/測試載入時不自動執行),失敗則在頂層回報並以狀態碼 1 結束
if (require.main === module) {
main().catch((error) => {
logger.error(error.message);
process.exit(1);
});
}
module.exports = { main };