UNPKG

gchcg-cli

Version:
1 lines 1.44 kB
const Command=require("./command"),log=require("./log"),SimpleGit=require("simple-git"),path=require("path"),fs=require("fs"),fse=require("fs-extra"),{spinnerStart:spinnerStart,sleep:sleep}=require("./utils");class PushCommand extends Command{async init(){this.branch=this._cmd.branch}async exec(){const e=(new Date).getTime();try{let e;if(this.prepare(),this.git=SimpleGit(this.projectInfo.dir),log.info("代码冲突检查"),e=await this.git.status(),e.conflicted.length>0)throw new Error("当前代码存在冲突,请手动处理合并后执行:git add . && cg push!");log.success("代码冲突检查通过。"),log.info("开始拉取代码...");let t=spinnerStart(`正在拉取远程分支 ${this.branch||e.current} 代码...`);await sleep();try{await this.git.pull("origin",this.branch||e.current),t.stop(!0)}catch(e){throw e}finally{t.stop(!0),log.success("拉取成功")}}catch(e){log.error(e.message)}log.verbose("本次执行指令耗时:"+((new Date).getTime()-e)/1e3+"秒")}prepare(){const e=process.cwd(),t=path.resolve(e,".git");if(!fs.existsSync(t))throw new Error("未找到 .git 文件夹,初始化 git 后重试");const s=path.resolve(e,"package.json");if(!fs.existsSync(s))throw new Error("package.json不存在!");const r=fse.readJsonSync(s),{name:i,version:n}=r;this.projectInfo={name:i,version:n,dir:e}}}function init(e){return new PushCommand(e)}module.exports=init,module.exports.PushCommand=PushCommand;