// 引入所需模块:文件系统(Promise版)、子进程exec(Promise化)、路径处理 const fs = require('fs/promises'); const exec = require('util').promisify(require('child_process').exec); const path = require('path'); // 引入GitHub Actions核心模块(用于设置输出)和语义化版本处理工具 const core = require('@actions/core'); const semver = require('semver'); // 异步自执行函数:处理版本号更新逻辑 (async () => { // 1. 定位并读取core模块的package.json文件 const corePackageJsonPath = path.join(__dirname, '../../ghost/core/package.json'); const corePackageJson = require(corePackageJsonPath); // 2. 获取当前版本号并打印 const current_version = corePackageJson.version; console.log(`Current version: ${current_version}`); // 3. 获取命令行传入的第一个参数(用于判断版本类型) const firstArg = process.argv[2]; console.log('firstArg', firstArg); // 4. 获取当前Git提交的短哈希值(用于构建版本号) const buildString = await exec('git rev-parse --short HEAD') .then(({stdout}) => stdout.trim()); // 去除输出中的换行符 let newVersion; // 声明新版本号变量 // 5. 根据命令行参数生成不同类型的新版本号 if (firstArg === 'canary' || firstArg === 'six') { // 对于canary或six类型:升级minor版本并添加预发布标签(包含Git哈希) const bumpedVersion = semver.inc(current_version, 'minor'); // 升级次要版本(如1.2.3 → 1.3.0) newVersion = `${bumpedVersion}-pre-g${buildString}`; // 格式:1.3.0-pre-gabc123 } else { // 其他情况:使用git describe生成版本号(基于最近的标签) // git describe --long会输出类似"v1.2.3-4-gabc123",这里去除前缀v const gitVersion = await exec('git describe --long HEAD') .then(({stdout}) => stdout.trim().replace(/^v/, '')); newVersion = gitVersion; } // 6. 统一添加+moya后缀(可能用于标识自定义构建) newVersion += '+moya'; console.log('newVersion', newVersion); // 7. 更新core模块的package.json版本号并写入文件 corePackageJson.version = newVersion; await fs.writeFile(corePackageJsonPath, JSON.stringify(corePackageJson, null, 2)); // 保留2空格缩进 // 8. 同步更新admin模块的package.json版本号 const adminPackageJsonPath = path.join(__dirname, '../../ghost/admin/package.json'); const adminPackageJson = require(adminPackageJsonPath); adminPackageJson.version = newVersion; await fs.writeFile(adminPackageJsonPath, JSON.stringify(adminPackageJson, null, 2)); console.log('Version bumped to', newVersion); // 9. 设置GitHub Actions输出变量(供后续步骤使用) core.setOutput('BUILD_VERSION', newVersion); // 输出构建版本号 core.setOutput('GIT_COMMIT_HASH', buildString) // 输出Git提交哈希 })();