UNPKG

integration-test-mcp

Version:

MCP服务器用于集成测试

231 lines (193 loc) 6.41 kB
#!/usr/bin/env node import { execSync } from "child_process"; import { readFileSync, writeFileSync } from "fs"; import { join, dirname } from "path"; import { fileURLToPath } from "url"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const projectRoot = join(__dirname, ".."); // 颜色输出 const colors = { reset: "\x1b[0m", red: "\x1b[31m", green: "\x1b[32m", yellow: "\x1b[33m", blue: "\x1b[34m", magenta: "\x1b[35m", cyan: "\x1b[36m", }; function log(message, color = "reset") { console.log(`${colors[color]}${message}${colors.reset}`); } function execCommand(command, options = {}) { try { log(`执行命令: ${command}`, "cyan"); const result = execSync(command, { cwd: projectRoot, stdio: "inherit", encoding: "utf8", ...options, }); return result; } catch (error) { log(`命令执行失败: ${command}`, "red"); log(`错误信息: ${error.message}`, "red"); throw error; } } function updateVersion(versionType = "patch") { log(`开始更新版本 (${versionType})...`, "blue"); const packageJsonPath = join(projectRoot, "package.json"); const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")); const currentVersion = packageJson.version; log(`当前版本: ${currentVersion}`, "yellow"); // 使用npm version命令更新版本 const newVersion = execCommand( `npm version ${versionType} --no-git-tag-version`, { stdio: "pipe" } ).trim(); log(`新版本: ${newVersion}`, "green"); return newVersion; } function buildProject() { log("开始构建项目...", "blue"); execCommand("npm run build"); log("项目构建完成", "green"); } function publishToNpm(tag = "latest") { log(`开始发布到npm (tag: ${tag})...`, "blue"); // 检查是否已登录npm try { execCommand("npm whoami", { stdio: "pipe" }); } catch (error) { log("请先登录npm: npm login", "red"); throw new Error("未登录npm"); } // 发布包 const publishCommand = tag === "latest" ? "npm publish" : `npm publish --tag ${tag}`; execCommand(publishCommand); log("发布成功!", "green"); } function createGitTag(version) { log(`创建Git标签: ${version}`, "blue"); try { // 检查是否有未提交的更改 const status = execCommand("git status --porcelain", { stdio: "pipe" }); if (status.trim()) { log("检测到未提交的更改,正在提交...", "yellow"); execCommand("git add ."); execCommand(`git commit -m "chore: 发布版本 ${version}"`); } // 创建标签 execCommand(`git tag -a ${version} -m "版本 ${version}"`); // 推送到远程仓库 const pushToRemote = process.argv.includes("--push"); if (pushToRemote) { log("推送到远程仓库...", "blue"); execCommand("git push"); execCommand(`git push origin ${version}`); log("已推送到远程仓库", "green"); } } catch (error) { log("Git操作失败,但npm发布已完成", "yellow"); log( `请手动执行: git tag ${version} && git push origin ${version}`, "yellow" ); } } function showHelp() { console.log(` ${colors.cyan}发布脚本使用说明:${colors.reset} ${colors.yellow}用法:${colors.reset} node scripts/publish.js [版本类型] [选项] ${colors.yellow}版本类型:${colors.reset} patch 补丁版本 (默认) - 1.0.0 -> 1.0.1 minor 次要版本 - 1.0.0 -> 1.1.0 major 主要版本 - 1.0.0 -> 2.0.0 prerelease 预发布版本 - 1.0.0 -> 1.0.1-0 ${colors.yellow}选项:${colors.reset} --tag <tag> 指定npm发布标签 (默认: latest) --push 推送Git标签到远程仓库 --dry-run 模拟运行,不实际发布 --help 显示帮助信息 ${colors.yellow}示例:${colors.reset} node scripts/publish.js # 发布patch版本 node scripts/publish.js minor # 发布minor版本 node scripts/publish.js patch --push # 发布并推送到远程仓库 node scripts/publish.js prerelease --tag beta # 发布预发布版本 node scripts/publish.js --dry-run # 模拟运行 `); } async function main() { try { // 解析命令行参数 const args = process.argv.slice(2); if (args.includes("--help") || args.includes("-h")) { showHelp(); return; } const isDryRun = args.includes("--dry-run"); const versionType = args.find((arg) => !arg.startsWith("--")) || "patch"; const tagIndex = args.indexOf("--tag"); const tag = tagIndex !== -1 && args[tagIndex + 1] ? args[tagIndex + 1] : "latest"; if (isDryRun) { log("=== 模拟运行模式 ===", "magenta"); } log("=== 开始发布流程 ===", "magenta"); log(`版本类型: ${versionType}`, "cyan"); log(`发布标签: ${tag}`, "cyan"); // 1. 更新版本号 let newVersion; if (!isDryRun) { newVersion = updateVersion(versionType); } else { log("模拟: 更新版本号", "yellow"); newVersion = "x.x.x"; } // 2. 构建项目 if (!isDryRun) { buildProject(); } else { log("模拟: 构建项目", "yellow"); } // 3. 发布到npm if (!isDryRun) { publishToNpm(tag); } else { log("模拟: 发布到npm", "yellow"); } // 4. 创建Git标签 if (!isDryRun) { createGitTag(newVersion); } else { log("模拟: 创建Git标签", "yellow"); } log("=== 发布完成 ===", "green"); if (!isDryRun) { log(`新版本 ${newVersion} 已成功发布!`, "green"); log(`查看: https://www.npmjs.com/package/integration-test-mcp`, "cyan"); } } catch (error) { log("=== 发布失败 ===", "red"); log(`错误: ${error.message}`, "red"); // 回滚版本号(如果已经更新了的话) try { const packageJsonPath = join(projectRoot, "package.json"); const gitStatus = execCommand("git status --porcelain package.json", { stdio: "pipe", }); if (gitStatus.includes("package.json")) { log("正在回滚版本号...", "yellow"); execCommand("git checkout -- package.json"); log("版本号已回滚", "green"); } } catch (rollbackError) { log("版本号回滚失败,请手动检查", "red"); } process.exit(1); } } main();