UNPKG

infly-libs

Version:

工具组件库

148 lines (132 loc) 5.31 kB
const { execSync } = require("child_process"); /** * 获取当前 Git 分支名称 * @returns */ function getCurrentBranch() { return execSync("git rev-parse --abbrev-ref HEAD").toString().trim(); } /** * 获取 Git 提交信息 * @returns {Object} 包含提交信息的对象,如果获取失败则返回空对象 */ function getLatestCommitInfo() { // 定义 Git 命令 const commands = { commitMsg: "git log -1 --no-merges --pretty=%B", branch: "git rev-parse --abbrev-ref HEAD", fullHash: "git rev-parse HEAD", author: "git log -1 --no-merges --pretty=%an", email: "git log -1 --no-merges --pretty=%ae", date: "git log -1 --no-merges --pretty=%cd --date=iso" }; // 结果对象 const commitInfo = {}; try { // 检查当前目录是否是 Git 仓库 execSync("git rev-parse --is-inside-work-tree", { stdio: "ignore" }); // 遍历命令并执行 for (const [key, command] of Object.entries(commands)) { try { commitInfo[key] = execSync(`${command}`, { encoding: "utf-8" }).trim(); } catch (cmdError) { console.warn(global.logColor.warning, `⚠️️ 获取 Git 信息失败(${key}):${cmdError.message}`); commitInfo[key] = ""; // 失败时设置为空字符串 } } } catch (error) { console.error(global.logColor.error, `❌ 获取 Git 信息失败:当前目录不是 Git 仓库或 Git 未安装。错误信息:${error.message}`); return {}; // 返回空对象 } return commitInfo; } /** * 获取指定文件的最后一次提交信息 * @param {String} filePath - 文件路径 * @returns {Object|null} 包含提交信息的对象,如果获取失败则返回 null */ function getLastCommitByFile(filePath) { try { // 执行 Git 命令获取最后一次提交信息‌:ml-citation{ref="3,5" data="citationList"} const cmd = `git log -1 --format="%H|%an|%ae|%at|%s" -- ${filePath}`; const output = execSync(cmd, { encoding: "utf8" }).trim(); if (!output) return null; // 解析提交信息‌:ml-citation{ref="1,5" data="citationList"} const [hash, author, email, timestamp, message] = output.split("|"); return { commitMsg: message.trim(), hash, author, email, date: new Date(parseInt(timestamp) * 1000).toISOString() }; } catch { return null; } } /** * 检查 Git 状态 * @param {String} excludeDir - 排除的目录 * @param {String} extraFilter - 额外过滤条件 */ function checkGitStatus(excludeDir, extraFilter, extraParams) { const { deleteOldFile = [] } = extraParams || {}; try { console.log(global.logColor.success, `🚀 Git自动提交开始执行, 正在检查非构建文件`); // 获取所有未跟踪和已修改的文件列表 const files = execSync("git status --porcelain -z", { encoding: "utf8" }) .toString() .split("\u0000") .filter((line) => line.trim()); // .map((line) => line.substring(3).trim()); // 提取文件路径 const excludeFiles = []; // 非指定构建相关文件 const includeFiles = []; // 指定的构建相关文件 files.forEach((file) => { const existsDelFile = deleteOldFile.find((item) => file.includes(item)); if (file.includes(`${excludeDir}/`) || file.includes("version-config.json") || file.includes(extraFilter) || existsDelFile) { includeFiles.push(file); } else { excludeFiles.push(file); } }); if (excludeFiles.length > 0) { throw new Error(`存在非构建文件未提交,请手动确定提交信息,本次自动提交终止\n${excludeFiles.join("\n")}`); } } catch (err) { console.error(global.logColor.error, `❌ 自动化构建Git状态检查存在问题:${err.message}`); process.exit(1); } } function runGitCommand(command, options = {}) { try { const result = execSync(command, { ...options, encoding: "utf8" }); if (result) { return result.trim(); } } catch (error) { console.error(global.logColor.error, `❌ 执行 Git 命令失败:${error.message}`); process.exit(1); } } function autoGitProcess(gitInfo, extraParams = {}) { const { commitMsg, branch } = gitInfo || {}; const { gitAutoCommitText = commitMsg } = extraParams || {}; const addCommand = `git add -A`; const commitCommand = `git commit -m "${gitAutoCommitText}"`; const pushCommand = `git push origin ${branch}`; runGitCommand(addCommand); runGitCommand(commitCommand); runGitCommand(pushCommand); console.log(global.logColor.success, "✅ Git 自动提交和推送成功!"); } module.exports = { getCurrentBranch, getLatestCommitInfo, getLastCommitByFile, checkGitStatus, autoGitProcess, runGitCommand };