exec异步问题

This commit is contained in:
OBKoro1 2020-01-02 14:19:05 +08:00
parent b9c7cc14c2
commit aff262a28c
4 changed files with 41 additions and 37 deletions

View File

@ -6,3 +6,5 @@
<!-- TODO: 前面先选中的日期会覆盖后面的日期 --> <!-- TODO: 前面先选中的日期会覆盖后面的日期 -->
<!-- TODO: html的链接 --> <!-- TODO: html的链接 -->
<!-- TODO: 可以挂在后台慢慢commit -->

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 : 2020-01-01 18:06:25 * LastEditTime : 2020-01-02 14:18:36
* FilePath : /autoCommit/src/models/commitHandle.ts * FilePath : /autoCommit/src/models/commitHandle.ts
* Description : commit * Description : commit
* https://github.com/OBKoro1 * https://github.com/OBKoro1
@ -11,7 +11,7 @@
import { webviewMsg } from '../util/dataStatement'; import { webviewMsg } from '../util/dataStatement';
import * as moment from 'moment'; import * as moment from 'moment';
import * as fs from 'fs'; import * as fs from 'fs';
import { execSync } from 'child_process'; import { execSync, exec } from 'child_process';
import { RandomNumber } from '../util/util'; import { RandomNumber } from '../util/util';
import { getPanelWebview, outputLog, isProduction } from '../util/vscodeUtil'; import { getPanelWebview, outputLog, isProduction } from '../util/vscodeUtil';
import WebView from './WebView'; import WebView from './WebView';
@ -83,7 +83,7 @@ class CommitHandle {
this.commitFn(); this.commitFn();
} }
async commitFn() { async commitFn() {
outputLog('将要commit的日期:', JSON.stringify(this.timeArr)); await outputLog('将要commit的日期:', JSON.stringify(this.timeArr));
let totalNum = 0; // 总commit次数 let totalNum = 0; // 总commit次数
// 遍历日期 // 遍历日期
for (let item of this.timeArr.values()) { for (let item of this.timeArr.values()) {
@ -107,22 +107,26 @@ class CommitHandle {
commitContent, commitContent,
'utf-8' 'utf-8'
); );
const isDebug = true; // 手动更改调试模拟是否提交git let commitMsg: string = '';
if (!isProduction() && !isDebug) { const isDebug = false; // 手动更改调试模拟是否提交git
// TODO: 测试提交 以及接受log if (!isProduction() || !isDebug) {
const res = this.myExecSync( try {
`cd ${this.paramsObj.itemSrc} && git add . && git commit -m 'autoCommit' --date='${time}' && git pull && git push origin master` // 异步执行命令 让出线程 打印日志 等
); commitMsg = await new Promise((resolve, reject) => {
console.log('res 结束', res); let cmd = `cd ${this.paramsObj.itemSrc} && git add . && git commit -m 'autoCommit' --date='${time}' && git pull && git push origin master`;
// 当前最新版本commit 信息 exec(cmd, (error, stdout, stderr) => {
const cmd = `git log -1 \ if (error) {
--date=iso --pretty=format:'{"commit": "%h","author": "%aN <%aE>","date": "%ad","message": "%s"},' \ outputLog(`执行命令出错:${cmd}`);
$@ | \ outputLog(`错误信息:${error}`, stderr);
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \ reject(error);
perl -pe 's/},]/}]/'`; return;
// [{"commit": "a6b5f3d","author": "OBKoro1 <1677593011@qq.com>","date": "2019-12-26 21:05:57 +0800","message": "init"}] }
const log = this.myExecSync(cmd); resolve(stdout);
console.log('log 开始', log); });
});
} catch (err) {
continue; // 错误 退出本次循环
}
} else { } else {
// 模拟git提交 // 模拟git提交
const test = await new Promise((resolve, reject) => { const test = await new Promise((resolve, reject) => {
@ -132,8 +136,9 @@ class CommitHandle {
}, 1000); }, 1000);
}); });
} }
await outputLog(`${totalNum + 1}commit内容`, commitContent);
await outputLog(`${totalNum + 1}commit信息`, commitMsg);
totalNum++; totalNum++;
outputLog('commit内容', commitContent);
} }
} }
this.commitEnd(totalNum); this.commitEnd(totalNum);
@ -152,19 +157,12 @@ class CommitHandle {
public closeCommit() { public closeCommit() {
this.userCancel = true; this.userCancel = true;
} }
// 当天的随机时间
// formatTime(time: string) {
// const hour = `${RandomNumber(0, 1)}${RandomNumber(0, 9)}`;
// const minute = `${RandomNumber(0, 5)}${RandomNumber(0, 9)}`;
// return `${time} ${hour}:${minute}`;
// }
// TODO: 某天的随机时间 代码块
// 获取当天的随机时间 // 获取当天的随机时间
formatTime(time: string) { formatTime(time: string) {
const hour1 = RandomNumber(0, 2); const hour1 = RandomNumber(0, 2);
let hour2 = RandomNumber(0, 9); let hour2 = RandomNumber(0, 9);
if (hour1 === 2) { if (hour1 === 2) {
// 小时第一个数字为2 则小时第二个数字最多为4 // 小时第一个数字为2 则小时第二个数字最多为3
hour2 = RandomNumber(0, 3); hour2 = RandomNumber(0, 3);
} }
const minute = `${RandomNumber(0, 5)}${RandomNumber(0, 9)}`; const minute = `${RandomNumber(0, 5)}${RandomNumber(0, 9)}`;
@ -198,14 +196,12 @@ class CommitHandle {
} }
return timeArr; return timeArr;
} }
// 同步执行命令
myExecSync(cmd: string) { myExecSync(cmd: string) {
// 除了该方法直到子进程完全关闭后才返回 执行完毕 返回 // 除了该方法直到子进程完全关闭后才返回 执行完毕 返回
try { try {
const res = execSync(cmd, { const res = execSync(cmd, {
encoding: 'utf8', encoding: 'utf8',
timeout: 0,
maxBuffer: 200 * 1024,
killSignal: 'SIGTERM',
cwd: undefined, cwd: undefined,
env: undefined env: undefined
}); });

View File

@ -2,7 +2,7 @@
* Author : OBKoro1 * Author : OBKoro1
* Date : 2019-12-25 17:13:30 * Date : 2019-12-25 17:13:30
* LastEditors : OBKoro1 * LastEditors : OBKoro1
* LastEditTime : 2019-12-31 16:36:12 * LastEditTime : 2020-01-02 13:44:30
* FilePath : /autoCommit/src/util/vscodeUtil.ts * FilePath : /autoCommit/src/util/vscodeUtil.ts
* Description : vscode * Description : vscode
* https://github.com/OBKoro1 * https://github.com/OBKoro1

View File

@ -70,7 +70,7 @@
</el-tooltip> </el-tooltip>
</label> </label>
<el-input-number v-model="ele.commitNumber" :step="1" :min="0" :max="100" ></el-input-number> <el-input-number v-model="ele.commitNumber" :step="1" :min="0" :max="100" ></el-input-number>
<el-button @click.prevent="removeTime(index)" :disabled="index === 0">删除</el-button> <el-button @click.prevent="removeTime(index)" :disabled="form.timeArr.length === 1">删除</el-button>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<el-button type="primary" @click="submitForm('form')" :disabled="runNow === 1 && runNow !== 0">开始commit</el-button> <el-button type="primary" @click="submitForm('form')" :disabled="runNow === 1 && runNow !== 0">开始commit</el-button>
@ -109,9 +109,11 @@
<el-link href="https://element.eleme.io" type="primary" target="_blank">更新日志</el-link> <el-link href="https://element.eleme.io" type="primary" target="_blank">更新日志</el-link>
</el-form-item> </el-form-item>
<div class="tip">觉得这个插件还不错的话, 请给我点个<el-link href="https://github.com/OBKoro1/autoCommit" type="primary" target="_blank">Star⭐</el-link>吧~</div> <div class="tip">觉得这个插件还不错的话, 请给我点个<el-link href="https://github.com/OBKoro1/autoCommit" type="primary" target="_blank">Star⭐</el-link>吧~</div>
<div v-if="!isProduction">
<div>{{ showText.logArr }}</div>
</div> </div>
</el-form> </el-form>
</el-input> </div>
</el-main> </el-main>
</el-main> </el-main>
</el-container> </el-container>
@ -147,6 +149,8 @@
paramsStr: '', // commit参数 paramsStr: '', // commit参数
logArr: [], // 保存的日志 logArr: [], // 保存的日志
logStr: '', // logStr: '', //
command: [],
data: []
}, },
runNow: 0, // 0 初始化 1运行中 2运行结束 3取消运行 runNow: 0, // 0 初始化 1运行中 2运行结束 3取消运行
form: { form: {
@ -241,6 +245,8 @@
initListener() { initListener() {
window.onmessage = (e) =>{ window.onmessage = (e) =>{
const { command, data } = event.data; const { command, data } = event.data;
this.showText.command.unshift(command)
this.showText.data.unshift(data)
if (command === 'choose item success') { if (command === 'choose item success') {
// 选择文件夹成功 // 选择文件夹成功
this.form.itemSrc = data this.form.itemSrc = data
@ -260,7 +266,7 @@
// 调试模式 查看通信 // 调试模式 查看通信
this.isProduction = data this.isProduction = data
} }
if(!this.isProduction){ if(!this.isProduction && false){
this.logDebug(command, data) this.logDebug(command, data)
} }
} }