forked from getgridea/gridea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ts
More file actions
227 lines (207 loc) · 5.71 KB
/
Copy pathdeploy.ts
File metadata and controls
227 lines (207 loc) · 5.71 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import fs from 'fs'
import moment from 'moment'
// @ts-ignore
import * as git from 'isomorphic-git/dist/for-node/isomorphic-git'
import Model from './model'
export default class Deploy extends Model {
outputDir: string = `${this.appDir}/output`
remoteUrl = ''
platformAddress = ''
constructor(appInstance: any) {
super(appInstance)
const { setting } = this.db
this.platformAddress = ({
github: 'github.com',
coding: 'e.coding.net',
} as any)[setting.platform || 'github']
const preUrl = ({
github: `${setting.username}:${setting.token}`,
coding: `${setting.tokenUsername}:${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.config({
fs,
dir: this.outputDir,
path: 'user.name',
value: setting.username,
})
await git.config({
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({
core: 'default',
url: this.remoteUrl,
})
console.log('info', info)
result.message = info
} catch (e) {
console.log('Test Remote Error: ', e.message)
result.success = false
result.message = e.message
}
return result
}
async publish() {
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 firstPush() {
const { setting } = this.db
const localBranchs = {}
console.log('first push')
try {
await git.init({ fs, dir: this.outputDir })
await git.config({
fs,
dir: this.outputDir,
path: 'user.name',
value: setting.username,
})
await git.config({
fs,
dir: this.outputDir,
path: 'user.email',
value: setting.email,
})
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 git.addRemote({
fs, dir: this.outputDir, remote: 'origin', url: this.remoteUrl, force: true,
})
await this.checkCurrentBranch()
const pushRes = await git.push({
fs,
dir: this.outputDir,
remote: 'origin',
ref: setting.branch,
force: true,
})
return {
success: true,
data: pushRes,
message: '',
localBranchs,
}
} catch (e) {
console.error(e)
return {
success: false,
data: localBranchs,
message: e.message,
localBranchs,
}
}
}
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,
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.fastCheckout({ fs, dir: this.outputDir, ref: setting.branch })
}
}
}