UNPKG

gchcg-cli

Version:
1 lines 5.47 kB
const Command=require("./command"),log=require("./log"),inquirer=require("inquirer").default,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.type=this._cmd.type,this.message=this._cmd.message,this.skip=this._cmd.skip}async exec(){const e=(new Date).getTime();try{let e;if(this.prepare(),this.git=SimpleGit(this.projectInfo.dir),e=await this.git.status(),log.info("代码冲突检查"),e.conflicted.length>0){e.conflicted.forEach((e=>{log.error("冲突文件:",e)}));const{confirmCommit:t}=await inquirer.prompt([{type:"confirm",name:"confirmCommit",default:!1,message:"是否已解决冲突,已解决请输入Y或y继续提交"}]);if(!t)throw new Error("请手动处理冲突文件后提交:cg push");log.info("已确认解决冲突文件,开始处理冲突文件...");let s=0,a=0,i=e.conflicted.length;for(let t=0;t<e.conflicted.length;t++){const i=e.conflicted[t];try{await this.git.add(i),log.success("【冲突文件】add 成功:",i),s++}catch(e){log.warn("【冲突文件】add 失败:",i),log.warn("【冲突文件】add 失败错误信息:",e&&"string"==typeof e.message?e.message.trim():e),a++}}log.success(`冲突文件处理完成!总数:${i},成功:${s},失败:${a}`)}log.success("代码冲突检查通过");if(e.not_added.length+e.created.length+e.deleted.length+e.modified.length+e.renamed.length>0){if(this.skip)log.info("已跳过 git add");else{log.info("开始处理未添加文件...");for(let t=0;t<e.not_added.length;t++)try{await this.git.add(e.not_added[t]),log.success("【未添加文件】add 成功:",e.not_added[t])}catch(s){log.warn("【未添加文件】add 失败:",e.not_added[t]),log.warn("【未添加文件】add 失败错误信息:",s&&"string"==typeof s.message?s.message.trim():s)}for(let t=0;t<e.created.length;t++)try{await this.git.add(e.created[t]),log.success("【新增文件】add 成功:",e.created[t])}catch(s){log.warn("【新增文件】add 失败:",e.created[t]),log.warn("【新增文件】add 失败错误信息:",s&&"string"==typeof s.message?s.message.trim():s)}for(let t=0;t<e.created.length;t++)try{await this.git.add(e.deleted[t]),log.success("【已删除文件】add 成功:",e.deleted[t])}catch(s){log.warn("【已删除文件】add 失败:",e.deleted[t]),log.warn("【已删除文件】add 失败错误信息:",s&&"string"==typeof s.message?s.message.trim():s)}for(let t=0;t<e.modified.length;t++)try{await this.git.add(e.modified[t]),log.success("【已修改文件】add 成功:",e.modified[t])}catch(s){log.warn("【已修改文件】add 失败:",e.modified[t]),log.warn("已修改文件报错信息:",s&&"string"==typeof s.message?s.message.trim():s)}for(let t=0;t<e.renamed.length;t++)try{await this.git.add(e.renamed[t]),log.success("【已重命名文件】add 成功:",e.renamed[t])}catch(s){log.warn("【已重命名文件】add 失败:",e.renamed[t]),log.warn("【已重命名文件】add 失败错误信息:",s&&"string"==typeof s.message?s.message.trim():s)}log.success("git add 完成")}const t=require("child_process").execSync("git config user.name").toString().trim();if(!t)throw new Error('git用户名不存在,请先执行git config --global user.name "Your Name"');let s=["feat","fix","docs","style","refactor","perf","test","build","ci","chore","revert"].includes(this.type)?this.type:"",a=this.message;const i={type:"list",name:"type",message:"请选择commit类型",default:s||"fix",choices:[{name:"新功能(feat)",value:"feat"},{name:"修复bug(fix)",value:"fix"},{name:"文档变更(docs)",value:"docs"},{name:"无关代码的格式(style)",value:"style"},{name:"重构(refactor)",value:"refactor"},{name:"优化性能(perf)",value:"perf"},{name:"增加或更新测试(test)",value:"test"},{name:"构建系统或外部依赖更改(build)",value:"build"},{name:"CI配置或脚本变动(ci)",value:"ci"},{name:"不影响代码的其余变动(chore)",value:"chore"},{name:"回滚(revert)",value:"revert"}]},r={type:"text",name:"message",message:"请输入commit信息:"},o=[];if(s||o.push(i),a||o.push(r),a&&1===o.length)s=(await inquirer.prompt(o)).type;else for(;!a;){const e=await inquirer.prompt(o);s=e.type,s&&o.shift(),a=e.message}const n=`${s}(${t}):${a}`;log.info("commit信息:",n);let d=spinnerStart("正在 commit...");await sleep();try{await this.git.commit(n),d.stop(!0),log.success("本次commit提交成功")}catch(e){throw d.stop(!0),log.error("本次commit提交失败"),e}}else log.warn("本次没有需要commit的文件");if(e=await this.git.status(),e.behind)log.warn("远程分支领先本地分支:"+e.behind+"次。请拉取代码:cg pull");else if(e.ahead){let t=spinnerStart(`正在 push 到 ${e.current} 分支...`);await sleep();try{await this.git.push("origin",e.current),t.stop(!0),log.success("本次push推送成功")}catch(e){throw t.stop(!0),log.error("本次push推送失败"),e}}else log.warn("本地分支已经是最新分支,无需 push")}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 后重试");this.projectInfo={dir:e}}}function init(e){return new PushCommand(e)}module.exports=init,module.exports.PushCommand=PushCommand;