forked from vv314/actions-mtz-coupons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-upstream.js
76 lines (58 loc) · 1.72 KB
/
sync-upstream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const { spawn, exec } = require('child_process')
function gitExec(opt) {
const pull = spawn('git', opt.split(' '))
return new Promise((resolve, reject) => {
let res = ''
pull.stdout.on('data', buffer => {
res += buffer.toString()
})
pull.stderr.on('data', buffer => {
res += buffer.toString()
})
pull.on('close', code => {
code == 0 ? resolve(res) : reject(res)
})
})
}
async function getRemoteUrls() {
const urls = await gitExec('remote -v')
return urls.split('\n')
}
function addUpstream(upstreamUrl) {
return gitExec(`remote add upstream ${upstreamUrl}`)
}
function setUpstream(upstreamUrl) {
return gitExec(`remote set-url upstream ${upstreamUrl}`)
}
function pullUpstream() {
return gitExec('pull upstream main:main')
}
function pushOrigin() {
return gitExec('push origin main:main')
}
async function main() {
const upstreamUrl = '[email protected]:vv314/actions-mtz-coupons.git'
console.log('———— [1/4] 获取上游仓库信息 ————')
const urls = await getRemoteUrls()
const exist = urls.some(url => url.startsWith('upstream'))
if (!exist) {
console.log('———— [2/4] 添加上游仓库 ————')
await addUpstream(upstreamUrl)
} else {
console.log('———— [2/4] 设置上游仓库 ————')
await setUpstream(upstreamUrl)
}
try {
console.log('———— [3/4] 拉取上游仓库 ————')
const pullRes = await pullUpstream()
console.log(pullRes)
console.log('———— [4/4] 推送更新 ————')
const pushRes = await pushOrigin()
console.log(pushRes)
console.log('同步成功')
} catch (e) {
console.log(e)
console.log('同步失败')
}
}
main()