template-syncer
Version:
智能模板同步工具 - 让你的项目与模板仓库保持同步,支持智能合并、差异对比和交互式更新
98 lines (97 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Git = void 0;
const child_process_1 = require("child_process");
const platform_1 = require("./platform");
/**
* Git 操作工具
*/
class Git {
constructor(verbose = false) {
this.verbose = verbose;
}
/**
* 执行 Git 命令
*/
exec(cmd, cwd) {
try {
return (0, child_process_1.execSync)(cmd, {
cwd,
encoding: 'utf8',
stdio: this.verbose ? 'inherit' : 'pipe'
});
}
catch (error) {
if (this.verbose && error instanceof Error) {
console.error(`Git 命令失败: ${error.message}`);
}
throw error;
}
}
/**
* 测试仓库连接
*/
testConnection(repo) {
try {
(0, child_process_1.execSync)(`git ls-remote --heads "${repo}"`, { stdio: 'ignore' });
return true;
}
catch {
return false;
}
}
/**
* 克隆仓库
*/
clone(repo, dest) {
platform_1.platform.removeDir(dest);
this.exec(`git clone "${repo}" "${dest}"`);
}
/**
* 获取所有分支
*/
getBranches(cwd) {
try {
const output = (0, child_process_1.execSync)('git branch -r', { cwd, encoding: 'utf8' });
return output
.split('\n')
.map(line => line.trim())
.filter(line => line && !line.includes('HEAD'))
.map(line => line.replace('origin/', ''))
.filter(Boolean);
}
catch {
return [];
}
}
/**
* 切换分支
*/
checkout(branch, cwd) {
this.exec(`git checkout ${branch}`, cwd);
}
/**
* 创建备份 (stash)
* 只备份已跟踪的文件,不影响未跟踪的配置文件
*/
backup(message = 'Template sync backup') {
try {
// 只 stash 已跟踪文件的修改,不包括未跟踪文件
(0, child_process_1.execSync)(`git stash push -m "${message}"`, {
stdio: this.verbose ? 'inherit' : 'ignore'
});
return true;
}
catch {
return false;
}
}
/**
* 删除 .git 目录
*/
removeGitDir(dir) {
const gitDir = require('path').join(dir, '.git');
platform_1.platform.removeDir(gitDir);
}
}
exports.Git = Git;