-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathdeploy.ts
181 lines (162 loc) · 4.64 KB
/
deploy.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import fs from 'fs'
import moment from 'moment'
// @ts-ignore
import Model from './model'
import GitProxy from './plugins/deploys/gitproxy'
const git = require('isomorphic-git')
export default class Deploy extends Model {
outputDir: string = this.buildDir
remoteUrl = ''
platformAddress = ''
http = new GitProxy(this)
constructor(appInstance: any) {
super(appInstance)
const { setting } = this.db
this.platformAddress = ({
github: 'github.com',
coding: 'e.coding.net',
gitee: 'gitee.com',
} as any)[setting.platform || 'github']
const preUrl = ({
github: `${setting.username}:${setting.token}`,
coding: `${setting.tokenUsername}:${setting.token}`,
gitee: `${setting.username}:${setting.token}`,
} as any)[setting.platform || 'github']
this.remoteUrl = `https://${preUrl}@${this.platformAddress}/${setting.username}/${setting.repository}.git`
}
/**
* Check whether the remote connection is normal
*/
async remoteDetect() {
const result = {
success: true,
message: [''],
}
try {
const { setting } = this.db
let isRepo = false
try {
await git.currentBranch({ fs, dir: this.outputDir })
isRepo = true
} catch (e) {
console.log('Not a repo', e.message)
}
if (!setting.username || !setting.repository || !setting.token) {
return {
success: false,
message: 'Username、repository、token is required',
}
}
if (!isRepo) {
await git.init({ fs, dir: this.outputDir })
await git.setConfig({
fs,
dir: this.outputDir,
path: 'user.name',
value: setting.username,
})
await git.setConfig({
fs,
dir: this.outputDir,
path: 'user.email',
value: setting.email,
})
}
await git.addRemote({
fs, dir: this.outputDir, remote: 'origin', url: this.remoteUrl, force: true,
})
const info = await git.getRemoteInfo({
http: this.http,
url: this.remoteUrl,
})
console.log('info', info)
result.message = info.capabilities
} catch (e) {
console.log('Test Remote Error: ', e)
result.success = false
result.message = e.message
}
return result
}
async publish() {
await this.remoteDetect()
this.db.themeConfig.domain = this.db.setting.domain
let result = {
success: true,
message: '',
localBranchs: {},
}
let isRepo = false
try {
await git.currentBranch({ fs, dir: this.outputDir })
isRepo = true
} catch (e) {
console.log('Not a repo', e.message)
}
if (isRepo) {
result = await this.commonPush()
} else {
// result = await this.firstPush()
}
return result
}
async commonPush() {
console.log('common push')
const { setting } = this.db
const localBranchs = {}
try {
const statusSummary = await git.status({ fs, dir: this.outputDir, filepath: '.' })
console.log('statusSummary', statusSummary)
await git.addRemote({
fs, dir: this.outputDir, remote: 'origin', url: this.remoteUrl, force: true,
})
if (statusSummary !== 'unmodified') {
await git.add({ fs, dir: this.outputDir, filepath: '.' })
await git.commit({
fs,
dir: this.outputDir,
message: `update from gridea: ${moment().format('YYYY-MM-DD HH:mm:ss')}`,
})
}
await this.checkCurrentBranch()
const pushRes = await git.push({
fs,
http: this.http,
dir: this.outputDir,
remote: 'origin',
ref: setting.branch,
force: true,
})
console.log('pushRes', pushRes)
return {
success: true,
data: pushRes,
message: '',
localBranchs,
}
} catch (e) {
console.log(e)
return {
success: false,
message: e.message,
data: localBranchs,
localBranchs,
}
}
}
/**
* Check whether the branch needs to be switched,
* FIXME: if branch is change, then the fist push is not work. so need to push again.
*/
async checkCurrentBranch() {
const { setting } = this.db
const currentBranch = await git.currentBranch({ fs, dir: this.outputDir, fullname: false })
const localBranches = await git.listBranches({ fs, dir: this.outputDir })
if (currentBranch !== setting.branch) {
if (!localBranches.includes(setting.branch)) {
await git.branch({ fs, dir: this.outputDir, ref: setting.branch })
}
await git.checkout({ fs, dir: this.outputDir, ref: setting.branch })
}
}
}