AutoCommit/src/index.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-12-20 11:12:22 +08:00
/*
* Author : OBKoro1
* Date : 2019-12-19 20:23:57
* LastEditors : OBKoro1
2019-12-20 18:54:51 +08:00
* LastEditTime : 2019-12-20 18:54:45
2019-12-20 11:12:22 +08:00
* FilePath : /autoCommit/index.js
* Description : 自动commit
* https://github.com/OBKoro1
*/
const { execSync } = require('child_process');
const moment = require('moment');
const fs = require('fs');
class autoCommit {
constructor() {
this.init();
}
2019-12-20 18:54:51 +08:00
getData() {
let time1 = '2019-01-02';
let time2 = '2019-01-10';
this.getAll(time1, time2);
}
formatTime(time) {
return `${time} 08:00`;
}
getAll(begin, end) {
let timeArr = []; // TODO: 时间段
const beginSplit = begin.split('-');
const endSplit = end.split('-');
const beginDate = new Date();
beginDate.setUTCFullYear(beginSplit[0], beginSplit[1] - 1, beginSplit[2]);
const endDate = new Date();
endDate.setUTCFullYear(endSplit[0], endSplit[1] - 1, endSplit[2]);
const beginNumber = beginDate.getTime();
const endNumber = endDate.getTime();
for (var k = timeNumber; k <= endNumber; ) {
console.log(new beginNumber(parseInt(k)).format());
k = k + 24 * 60 * 60 * 1000;
}
}
2019-12-20 11:12:22 +08:00
init() {
2019-12-20 18:54:51 +08:00
this.getData();
2019-12-20 11:12:22 +08:00
const time = moment().format('DD/MM/YYYY HH:MM:ss');
2019-12-20 18:54:51 +08:00
let time3 = moment('2019-01-02 08:00').format();
console.log('time', time2, time3);
2019-12-20 11:12:22 +08:00
fs.writeFileSync('./test.md', time, 'utf-8');
2019-12-20 18:54:51 +08:00
// this.commit();
}
getTime() {
return '';
2019-12-20 11:12:22 +08:00
}
commit() {
2019-12-20 15:47:09 +08:00
// git commit --amend --date="2019-01-02T00:00:00+0800" -am 'autoCommit'
2019-12-20 11:38:45 +08:00
this.myExecSync(
2019-12-20 15:47:09 +08:00
`git add . && git commit -m 'autoCommit' --date='2019-01-02T00:00:00+0800' && git pull && git push origin master`
2019-12-20 11:38:45 +08:00
);
2019-12-20 11:12:22 +08:00
}
myExecSync(cmd) {
// 除了该方法直到子进程完全关闭后才返回 执行完毕 返回
try {
const res = execSync(cmd, {
encoding: 'utf8',
timeout: 0,
maxBuffer: 200 * 1024,
killSignal: 'SIGTERM',
cwd: undefined,
env: undefined
});
return res;
} catch (err) {
console.log(`执行命令出错:${cmd}`);
}
}
}
new autoCommit();