feat: commit命令测试与兼容、运行命令函数提取、eslint规则禁用

This commit is contained in:
OBKoro1 2021-06-27 16:22:25 +08:00
parent 7b83ba1b6c
commit 181c8ae5ca

View File

@ -2,7 +2,7 @@
* Author : OBKoro1 * Author : OBKoro1
* Date : 2019-12-30 16:59:30 * Date : 2019-12-30 16:59:30
* LastEditors : OBKoro1 * LastEditors : OBKoro1
* LastEditTime : 2021-01-08 19:50:07 * LastEditTime : 2021-06-27 16:18:59
* File : \autoCommit\src\models\commitHandle.ts * File : \autoCommit\src\models\commitHandle.ts
* Description : commit * Description : commit
* https://github.com/OBKoro1 * https://github.com/OBKoro1
@ -139,7 +139,7 @@ class CommitHandle {
if (scopeDay < 1 || noCommitDay < 1) return; // 必须大于1 if (scopeDay < 1 || noCommitDay < 1) return; // 必须大于1
if (scopeDay > this.timeArr.length) return; // 日期不够 if (scopeDay > this.timeArr.length) return; // 日期不够
// 删除 // 删除
for (let i = 0; i < noCommitDay; i++) { for (let i = 0; i < noCommitDay; i += 1) {
const ranDomNum = Math.floor(Math.random() * this.timeArr.length); // 随机数 const ranDomNum = Math.floor(Math.random() * this.timeArr.length); // 随机数
this.timeArr.splice(ranDomNum, 1); this.timeArr.splice(ranDomNum, 1);
} }
@ -159,45 +159,38 @@ class CommitHandle {
if (this.cancelCommit()) break; if (this.cancelCommit()) break;
// 每个日期commit次数 // 每个日期commit次数
const dayCommitNumber = item.commitNumber; const dayCommitNumber = item.commitNumber;
/* eslint no-plusplus: ["error", { "allowForLoopAfterthoughts": true }] */ for (let i = 0; i < dayCommitNumber; i += 1) {
for (let i = 0; i < dayCommitNumber; i++) {
if (this.cancelCommit()) break; if (this.cancelCommit()) break;
let time = getTodayRandomNumber(item.value); // 2019-01-02 08:00 let time = getTodayRandomNumber(item.value); // 2019-01-02 08:00
time = moment(time).format(); // 2019-01-02T00:00:00+0800 time = moment(time).format(); // 2019-01-02T00:00:00+0800
const commitContent = this.commitFileContent(time, totalNum); const commitContent = this.commitFileContent(time, totalNum);
let commitMsg: string = ''; let commitMsg;
const isDebug = false; // 手动更改调试模拟是否提交git const isDebug = false; // 手动更改调试模拟是否提交git
if (!isProduction() || !isDebug) { if (!isProduction() || !isDebug) {
try { try {
// 异步执行命令 让出线程 打印日志 等 // 显示绿点成功 但是在仓库的时间不对 github可能修改了规则 后来的commit只会在上面
// eslint-disable-next-line no-await-in-loop // 先add
commitMsg = await new Promise((resolve, reject) => { const commitCmd = 'git add .';
// 先提交commit 再修改commit日期和时间 // 先add 和提交commit
const cmd = `git add . && git commit -m '${this.paramsObj.commitMsg}' && set GIT_COMMITTER_DATE='${time}' && set GIT_AUTHOR_DATE='${time}' && git commit --amend --no-edit --date '${time}'`; // const commitCmd = `git add . && git commit -m '${this.paramsObj.commitMsg}'`;
exec( commitMsg = await this.execCmd(commitCmd);
cmd, // 修改commit日期和时间
{ const cmd = `git commit --date='${time}' -am '${this.paramsObj.commitMsg}'`;
cwd: this.paramsObj.itemSrc,
env: process.env, // set在window下可能有兼容问题
}, // const cmd = `git commit --amend --date='${time}' -am '${this.paramsObj.commitMsg}' && set GIT_COMMITTER_DATE='${time}' && set GIT_AUTHOR_DATE='${time}' && git commit --amend --no-edit --date '${time}'`;
(error, stdout, stderr) => { // const cmd = `set GIT_COMMITTER_DATE='${time}' && set GIT_AUTHOR_DATE='${time}' && git commit --amend --no-edit --date '${time}'`;
if (error) { // 修改commit的id 的提交time
outputLog(`执行命令出错:${cmd}`); // const commitId = await this.execCmd('git rev-parse HEAD');
outputLog(`错误信息:${error}`, stderr); // const cmd = `git commit --amend --date='${time}' -C ${commitId}`;
reject(error);
return; commitMsg = await this.execCmd(cmd);
}
resolve(stdout);
},
);
});
} catch (err) { } catch (err) {
outputLog(`commit出错:${err}`); outputLog(`commit出错:${err}`);
continue; // 错误 退出本次循环 continue; // 错误 退出本次循环
} }
} else { } else {
// 模拟git提交 // 模拟git提交
// eslint-disable-next-line no-await-in-loop
await new Promise((resolve) => { await new Promise((resolve) => {
setTimeout(() => { setTimeout(() => {
outputLog('延迟一秒'); outputLog('延迟一秒');
@ -224,9 +217,7 @@ class CommitHandle {
thinkNumber = 2000; // 无感 考虑两秒 thinkNumber = 2000; // 无感 考虑两秒
} }
await new Promise((resolve) => { await new Promise((resolve) => {
setTimeout(() => { setTimeout(resolve, thinkNumber);
resolve();
}, thinkNumber);
}); });
if (this.cancelCommit()) { if (this.cancelCommit()) {
if (totalNum === 0) return; if (totalNum === 0) return;
@ -235,29 +226,8 @@ class CommitHandle {
} else { } else {
outputLog('提交中...'); outputLog('提交中...');
this.autoCommitView.postMessage('提交中...', '提交中'); this.autoCommitView.postMessage('提交中...', '提交中');
const res = await new Promise((resolve, reject) => { const cmd = 'git pull --rebase && git push';
const cmd = 'git pull --rebase && git push'; const res = await this.execCmd(cmd);
exec(
cmd,
{
encoding: 'utf8',
cwd: this.paramsObj.itemSrc,
env: undefined,
},
(error, stdout, stderr) => {
if (error) {
outputLog(`执行命令出错:${cmd}`);
outputLog(`错误信息:${error}`, stderr);
outputLog(
'git push失败很可能是你的网络有问题请换到一个网络状况比较良好的地方然后再项目下执行 git push操作。',
);
reject(error);
return;
}
resolve(stdout);
},
);
});
outputLog('提交信息:', res); outputLog('提交信息:', res);
this.commitEnd(totalNum); this.commitEnd(totalNum);
} }
@ -266,26 +236,9 @@ class CommitHandle {
async resetCommit(totalNum: number) { async resetCommit(totalNum: number) {
this.autoCommitView.postMessage('回滚', '回滚'); this.autoCommitView.postMessage('回滚', '回滚');
outputLog('回滚中...'); outputLog('回滚中...');
const p = await new Promise((resolve, reject) => { const cmd = `git reset --hard HEAD~${totalNum}`;
const cmd = `git reset --hard HEAD~${totalNum}`; const p = await this.execCmd(cmd);
exec( outputLog(`回滚${totalNum}次commit成功:`, p);
cmd,
{
encoding: 'utf8',
cwd: this.paramsObj.itemSrc,
},
(error, stdout, stderr) => {
if (error) {
outputLog(`执行命令出错:${cmd}`);
outputLog(`回滚失败:${error}`, stderr);
reject(error);
return;
}
outputLog(`回滚${totalNum}次commit成功:`, stdout);
resolve(stdout);
},
);
});
return p; return p;
} }
@ -325,6 +278,28 @@ class CommitHandle {
return commitContent; return commitContent;
} }
// 执行命令封装
execCmd(cmd: string) {
return new Promise((resolve, reject) => {
exec(
cmd,
{
cwd: this.paramsObj.itemSrc,
env: process.env,
},
(error, stdout, stderr) => {
if (error) {
outputLog(`执行命令出错:${cmd}`);
outputLog(`错误信息:${error}`, stderr);
reject(error);
return;
}
resolve(stdout);
},
);
});
}
// 获取当天的commit次数 // 获取当天的commit次数
getDayCommitNumber(commitNumber: number) { getDayCommitNumber(commitNumber: number) {
let dayCommitNumber = this.paramsObj.commitNumber; // 固定commit次数 let dayCommitNumber = this.paramsObj.commitNumber; // 固定commit次数