2019-12-20 11:12:22 +08:00
|
|
|
/*
|
|
|
|
* Author : OBKoro1
|
|
|
|
* Date : 2019-12-19 20:23:57
|
|
|
|
* LastEditors : OBKoro1
|
2019-12-26 17:47:37 +08:00
|
|
|
* LastEditTime : 2019-12-25 15:23:38
|
|
|
|
* FilePath : /autoCommit/src/index.js
|
2019-12-20 11:12:22 +08:00
|
|
|
* Description : 自动commit
|
|
|
|
* https://github.com/OBKoro1
|
|
|
|
*/
|
|
|
|
|
2019-12-26 17:47:37 +08:00
|
|
|
const { execSync } = require('child_process')
|
|
|
|
const moment = require('moment')
|
|
|
|
const fs = require('fs')
|
2019-12-20 11:12:22 +08:00
|
|
|
|
|
|
|
class autoCommit {
|
2019-12-26 17:47:37 +08:00
|
|
|
constructor () {
|
|
|
|
this.init()
|
2019-12-20 11:12:22 +08:00
|
|
|
}
|
2019-12-26 17:47:37 +08:00
|
|
|
getData () {
|
|
|
|
this.getAllDay(this.paramsObj.beginDay, this.paramsObj.endDay)
|
|
|
|
this.readyCommit()
|
2019-12-20 18:54:51 +08:00
|
|
|
}
|
2019-12-26 17:47:37 +08:00
|
|
|
// 格式化日期
|
|
|
|
formatTime (time) {
|
|
|
|
return `${time} 08:00`
|
2019-12-20 18:54:51 +08:00
|
|
|
}
|
2019-12-26 17:47:37 +08:00
|
|
|
// 获取两个日期之间的间隔: [ '2019-02-02', '2019-02-03' ... ]
|
|
|
|
getAllDay (begin, end) {
|
|
|
|
this.timeArr = []
|
|
|
|
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 (let k = beginNumber; k <= endNumber;) {
|
|
|
|
const day = new Date(parseInt(k))
|
|
|
|
const dayFormat = moment(day).format('YYYY-MM-DD')
|
|
|
|
this.timeArr.push(dayFormat)
|
|
|
|
k = k + 24 * 60 * 60 * 1000
|
2019-12-20 18:54:51 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-26 17:47:37 +08:00
|
|
|
init () {
|
|
|
|
// TODO: 获取参数
|
2019-12-22 02:01:38 +08:00
|
|
|
let paramsObj = {
|
2019-12-26 17:47:37 +08:00
|
|
|
beginDay: '2019-12-20',
|
|
|
|
itemSrc: '/Users/koro/work/web_my/testCommit' // 要commit的项目地址
|
|
|
|
}
|
2019-12-23 20:46:33 +08:00
|
|
|
let defaultOption = {
|
2019-12-26 17:47:37 +08:00
|
|
|
beginDay: moment().format('YYYY-MM-DD'), // 默认为今天
|
2019-12-23 20:46:33 +08:00
|
|
|
endDay: moment().format('YYYY-MM-DD'), // 默认为今天
|
2019-12-26 17:47:37 +08:00
|
|
|
fileSrc: `commit.md`,
|
|
|
|
commitNumber: 1 // 每天commit 次数
|
|
|
|
}
|
|
|
|
this.paramsObj = Object.assign(defaultOption, paramsObj)
|
|
|
|
this.getData()
|
2019-12-20 18:54:51 +08:00
|
|
|
}
|
2019-12-26 17:47:37 +08:00
|
|
|
// commit
|
|
|
|
readyCommit () {
|
|
|
|
console.log('日期数组:', this.timeArr)
|
|
|
|
console.log('每个日期提交次数:', this.paramsObj.commitNumber)
|
|
|
|
let totalNum = 0 // 总commit次数
|
2019-12-23 20:46:33 +08:00
|
|
|
// 遍历日期
|
|
|
|
this.timeArr.forEach(item => {
|
|
|
|
// 每个日期commit次数
|
2019-12-26 17:47:37 +08:00
|
|
|
for (let i = 0; i < this.paramsObj.commitNumber; i++) {
|
|
|
|
let time = this.formatTime(item) // 2019-01-02 08:00
|
|
|
|
time = moment(time).format() // 2019-01-02T00:00:00+0800
|
|
|
|
const commitContent = `${time}${i}`
|
2019-12-23 20:46:33 +08:00
|
|
|
fs.writeFileSync(
|
2019-12-26 17:47:37 +08:00
|
|
|
`${this.paramsObj.itemSrc}/${this.paramsObj.fileSrc}`,
|
|
|
|
commitContent,
|
2019-12-23 20:46:33 +08:00
|
|
|
'utf-8'
|
2019-12-26 17:47:37 +08:00
|
|
|
)
|
|
|
|
this.myExecSync(
|
|
|
|
`cd ${this.paramsObj.itemSrc} && git add . && git commit -m 'autoCommit' --date='${time}' && git pull && git push origin master`
|
|
|
|
)
|
|
|
|
let cmd = `git log -1 \
|
|
|
|
--date=iso --pretty=format:'{"commit": "%h","author": "%aN <%aE>","date": "%ad","message": "%s"},' \
|
|
|
|
$@ | \
|
|
|
|
perl -pe 'BEGIN{print "["}; END{print "]\n"}' | \
|
|
|
|
perl -pe 's/},]/}]/'`
|
|
|
|
this.myExecSync(cmd)
|
|
|
|
this.totalNum++
|
|
|
|
|
|
|
|
console.log('commit内容', commitContent)
|
|
|
|
console.log(`总commit次数${totalNum}`)
|
2019-12-23 20:46:33 +08:00
|
|
|
}
|
2019-12-26 17:47:37 +08:00
|
|
|
})
|
2019-12-20 11:12:22 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 17:47:37 +08:00
|
|
|
myExecSync (cmd) {
|
2019-12-20 11:12:22 +08:00
|
|
|
// 除了该方法直到子进程完全关闭后才返回 执行完毕 返回
|
|
|
|
try {
|
|
|
|
const res = execSync(cmd, {
|
|
|
|
encoding: 'utf8',
|
|
|
|
timeout: 0,
|
|
|
|
maxBuffer: 200 * 1024,
|
|
|
|
killSignal: 'SIGTERM',
|
|
|
|
cwd: undefined,
|
|
|
|
env: undefined
|
2019-12-26 17:47:37 +08:00
|
|
|
})
|
|
|
|
return res
|
2019-12-20 11:12:22 +08:00
|
|
|
} catch (err) {
|
2019-12-26 17:47:37 +08:00
|
|
|
console.log(`执行命令出错:${cmd}`)
|
2019-12-20 11:12:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 17:47:37 +08:00
|
|
|
new autoCommit()
|