long-git-cli
Version:
A CLI tool for Git tag management.
149 lines • 6.15 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.tagCommand = tagCommand;
const simple_git_1 = require("simple-git");
const git_remote_1 = require("../utils/git-remote");
const git_1 = require("../utils/git");
const tag_1 = require("../utils/tag");
const constants_1 = require("../constants");
const prompt = __importStar(require("../utils/prompt"));
const message_1 = require("../utils/message");
// Git 操作实例
const git = (0, simple_git_1.simpleGit)();
/**
* 主要的 tag 命令处理函数
*/
async function tagCommand(options) {
(0, message_1.printInfo)('开始打 tag...');
try {
// 0. 检查当前分支是否受保护
const branch = await (0, git_1.getCurrentBranch)();
if (constants_1.PROTECTED_BRANCHES.includes(branch)) {
(0, message_1.printError)(`⚠️ 当前分支为 "${branch}",禁止在主分支(${constants_1.PROTECTED_BRANCHES.join('、')})上打 tag!`);
(0, message_1.printWarning)('请切换到 feature、dev 或其他非主分支后再执行打 tag 操作。');
return;
}
// 1. 检查是否在 Git 仓库中
await (0, git_1.checkGitRepository)();
// 2. 检测远程仓库类型
const remoteInfo = await (0, git_remote_1.detectRemoteType)();
if (remoteInfo) {
(0, message_1.printInfo)(`检测到远程仓库: ${remoteInfo.type} (${remoteInfo.url})`);
}
// 3. 拉取最新的远程信息
await (0, git_1.fetchLatestTags)(remoteInfo?.type);
// 4. 获取最新的 tag
const latestTag = await (0, git_1.getLatestTag)();
if (!latestTag) {
(0, message_1.printWarning)('⚠️ 没有找到现有的 tag,将创建初始 tag: test-v00.00.0001');
await createInitialTag(remoteInfo);
return;
}
// 5. 解析当前 tag
const currentTagInfo = (0, tag_1.parseTag)(latestTag);
(0, message_1.printSuccess)(`🏷️ 当前最新 tag: ${latestTag}`);
// 6. 生成新版本号
const newTagInfo = (0, tag_1.incrementVersion)(currentTagInfo);
const newTag = (0, tag_1.formatTag)(newTagInfo, { prefix: 'test-v', pattern: '00.00.0000' });
(0, message_1.printInfo)(`新 tag: ${newTag}`);
// 7. 询问用户是否确认
if (!(options && options.pass)) {
const confirmed = await prompt.confirm(`是否确认创建并推送新 tag: ${newTag}?`);
if (!confirmed) {
(0, message_1.printWarning)('🚫 已取消 tag 创建与推送。');
return;
}
}
// 8. 创建并推送新 tag
await createAndPushTag(newTag, remoteInfo);
(0, message_1.printSuccess)('✅ Tag 创建并推送成功!');
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Tag 操作失败: ${errorMessage}`);
}
}
/**
* 创建初始 tag(当没有现有 tag 时调用)
*/
async function createInitialTag(remoteInfo) {
const initialTag = 'test-v00.00.0001';
(0, message_1.printInfo)(`创建初始 tag: ${initialTag}`);
try {
await git.addTag(initialTag);
(0, message_1.printSuccess)('初始 tag 创建成功');
await pushTagWithFallback(remoteInfo);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`创建初始 tag 失败: ${errorMessage}`);
}
}
/**
* 创建并推送 tag
*/
async function createAndPushTag(tag, remoteInfo) {
(0, message_1.printInfo)(`创建 tag: ${tag}`);
try {
await git.addTag(tag);
(0, message_1.printSuccess)(`Tag ${tag} 创建成功`);
await pushTagWithFallback(remoteInfo);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`创建 tag 失败: ${errorMessage}`);
}
}
/**
* 推送 tag 到远程仓库,如果失败则给出友好提示
*/
async function pushTagWithFallback(remoteInfo) {
try {
await (0, git_1.pushTags)();
(0, message_1.printSuccess)('✅ Tag 推送成功');
if (remoteInfo) {
(0, git_remote_1.showRemoteSpecificTips)(remoteInfo.type);
}
}
catch (pushError) {
(0, message_1.printWarning)('⚠️ Tag 推送失败,但本地 tag 已创建成功');
(0, message_1.printInfo)('💡 提示:如果需要推送到远程仓库,请先配置远程仓库');
if (remoteInfo) {
(0, message_1.printInfo)(`当前远程仓库: ${remoteInfo.type} (${remoteInfo.url})`);
}
}
}
//# sourceMappingURL=tag.js.map