Skip to content

Commit 2078edd

Browse files
committed
feat: replace simple-git to isomorphic-git
1 parent befd9d2 commit 2078edd

3 files changed

Lines changed: 114 additions & 85 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
"normalize-path": "^3.0.0",
5656
"prismjs": "^1.16.0",
5757
"shortid": "^2.2.14",
58-
"simple-git": "^1.107.0",
5958
"slug": "^0.9.3",
6059
"ssh2-sftp-client": "^4.2.4",
6160
"striptags": "^3.1.1",

src/server/deploy.ts

Lines changed: 111 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import simpleGit, { SimpleGit } from 'simple-git/promise'
2-
import * as isoGit from 'isomorphic-git'
31
import fs from 'fs'
42
import moment from 'moment'
3+
// @ts-ignore
4+
import * as git from 'isomorphic-git/dist/for-node/isomorphic-git/index'
55
import Model from './model'
66

77
export default class Deploy extends Model {
88
outputDir: string = `${this.appDir}/output`
99

10-
git: SimpleGit
11-
1210
remoteUrl = ''
1311

1412
platformAddress = ''
@@ -22,7 +20,6 @@ export default class Deploy extends Model {
2220
coding: 'git.coding.net',
2321
} as any)[setting.platform || 'github']
2422
this.remoteUrl = `https://${setting.username}:${setting.token}@${this.platformAddress}/${setting.username}/${setting.repository}.git`
25-
this.git = simpleGit(this.outputDir)
2623
}
2724

2825
/**
@@ -34,34 +31,41 @@ export default class Deploy extends Model {
3431
message: '',
3532
}
3633
try {
37-
const remotes = await isoGit.listRemotes({ fs, dir: this.outputDir })
38-
console.log('remotes', remotes)
39-
// const { setting } = this.db
40-
// let isRepo = false
41-
// try {
42-
// isRepo = await this.git.checkIsRepo()
43-
// } catch (e) {
44-
// console.log('Not a repo', e.message)
45-
// }
46-
47-
// if (!setting.username || !setting.repository || !setting.token) {
48-
// return {
49-
// success: false,
50-
// message: 'Username、repository、token is required',
51-
// }
52-
// }
53-
// if (!isRepo) {
54-
// await this.git.init()
55-
// await this.git.addConfig('user.name', setting.username)
56-
// await this.git.addConfig('user.email', setting.email)
57-
// }
58-
59-
// try {
60-
// await this.git.raw(['remote', 'set-url', 'origin', this.remoteUrl])
61-
// } catch (e) {
62-
// await this.git.addRemote('origin', this.remoteUrl)
63-
// }
64-
// const data = await this.git.listRemote([])
34+
const { setting } = this.db
35+
let isRepo = false
36+
try {
37+
await git.currentBranch({ fs, dir: this.outputDir })
38+
isRepo = true
39+
} catch (e) {
40+
console.log('Not a repo', e.message)
41+
}
42+
43+
if (!setting.username || !setting.repository || !setting.token) {
44+
return {
45+
success: false,
46+
message: 'Username、repository、token is required',
47+
}
48+
}
49+
if (!isRepo) {
50+
await git.init({ fs, dir: this.outputDir })
51+
await git.config({
52+
fs,
53+
dir: this.outputDir,
54+
path: 'user.name',
55+
value: setting.username,
56+
})
57+
await git.config({
58+
fs,
59+
dir: this.outputDir,
60+
path: 'user.email',
61+
value: setting.email,
62+
})
63+
}
64+
65+
await git.addRemote({
66+
fs, dir: this.outputDir, remote: 'origin', url: this.remoteUrl, force: true,
67+
})
68+
await git.listRemotes({ fs, dir: this.outputDir })
6569
} catch (e) {
6670
console.log('Test Remote Error: ', e.message)
6771
result.success = false
@@ -77,8 +81,13 @@ export default class Deploy extends Model {
7781
message: '',
7882
localBranchs: {},
7983
}
80-
const isRepo = await this.git.checkIsRepo()
81-
console.log(isRepo)
84+
let isRepo = false
85+
try {
86+
await git.currentBranch({ fs, dir: this.outputDir })
87+
isRepo = true
88+
} catch (e) {
89+
console.log('Not a repo', e.message)
90+
}
8291
if (isRepo) {
8392
result = await this.commonPush()
8493
} else {
@@ -89,21 +98,44 @@ export default class Deploy extends Model {
8998

9099
async firstPush() {
91100
const { setting } = this.db
92-
let localBranchs = {}
101+
const localBranchs = {}
93102
console.log('first push')
94103

95104
try {
96-
await this.git.init()
97-
await this.git.addConfig('user.name', setting.username)
98-
await this.git.addConfig('user.email', setting.email)
99-
await this.git.add('./*')
100-
await this.git.commit('first commit')
101-
await this.git.addRemote('origin', this.remoteUrl)
102-
localBranchs = await this.checkCurrentBranch()
103-
await this.git.push('origin', setting.branch, { '--force': true })
105+
await git.init({ fs, dir: this.outputDir })
106+
await git.config({
107+
fs,
108+
dir: this.outputDir,
109+
path: 'user.name',
110+
value: setting.username,
111+
})
112+
await git.config({
113+
fs,
114+
dir: this.outputDir,
115+
path: 'user.email',
116+
value: setting.email,
117+
})
118+
await git.add({ fs, dir: this.outputDir, filepath: './*' })
119+
await git.commit({
120+
fs,
121+
dir: this.outputDir,
122+
message: `update from gridea: ${moment().format('YYYY-MM-DD HH:mm:ss')}`,
123+
})
124+
await git.addRemote({
125+
fs, dir: this.outputDir, remote: 'origin', url: this.remoteUrl, force: true,
126+
})
127+
// await git.fastCheckout({ fs, dir: this.outputDir, ref: setting.branch })
128+
await this.checkCurrentBranch()
129+
const pushRes = await git.push({
130+
fs,
131+
dir: this.outputDir,
132+
remote: 'origin',
133+
ref: setting.branch,
134+
force: true,
135+
})
104136
return {
105137
success: true,
106-
data: localBranchs,
138+
data: pushRes,
107139
message: '',
108140
localBranchs,
109141
}
@@ -121,24 +153,36 @@ export default class Deploy extends Model {
121153
async commonPush() {
122154
console.log('common push')
123155
const { setting } = this.db
124-
let localBranchs = {}
156+
const localBranchs = {}
125157
try {
126-
const statusSummary = await this.git.status()
127-
console.log(statusSummary)
128-
await this.git.raw(['remote', 'set-url', 'origin', this.remoteUrl])
129-
130-
if (statusSummary.modified.length > 0 || statusSummary.not_added.length > 0) {
131-
await this.git.add('./*')
132-
await this.git.commit(`update from gridea: ${moment().format('YYYY-MM-DD HH:mm:ss')}`)
133-
localBranchs = await this.checkCurrentBranch()
134-
await this.git.push('origin', this.db.setting.branch, { '--force': true })
135-
} else {
136-
await this.checkCurrentBranch()
137-
await this.git.push('origin', this.db.setting.branch, { '--force': true })
158+
const statusSummary = await git.status({ fs, dir: this.outputDir, filepath: '.' })
159+
console.log('statusSummary', statusSummary)
160+
await git.addRemote({
161+
fs, dir: this.outputDir, remote: 'origin', url: this.remoteUrl, force: true,
162+
})
163+
164+
if (statusSummary !== 'unmodified') {
165+
await git.add({ fs, dir: this.outputDir, filepath: '.' })
166+
await git.commit({
167+
fs,
168+
dir: this.outputDir,
169+
message: `update from gridea: ${moment().format('YYYY-MM-DD HH:mm:ss')}`,
170+
})
138171
}
172+
173+
await this.checkCurrentBranch()
174+
175+
const pushRes = await git.push({
176+
fs,
177+
dir: this.outputDir,
178+
remote: 'origin',
179+
ref: setting.branch,
180+
force: true,
181+
})
182+
console.log('pushRes', pushRes)
139183
return {
140184
success: true,
141-
data: localBranchs,
185+
data: pushRes,
142186
message: '',
143187
localBranchs,
144188
}
@@ -154,34 +198,20 @@ export default class Deploy extends Model {
154198
}
155199

156200
/**
157-
* Check whether the branch needs to be switched
201+
* Check whether the branch needs to be switched,
202+
* FIXME: if branch is change, then the fist push is not work. so need to push again.
158203
*/
159204
async checkCurrentBranch() {
160205
const { setting } = this.db
161-
const currentBranch = (await this.git.revparse(['--abbrev-ref', 'HEAD']) || 'master').replace(/\n/g, '')
162-
let hasNewBranch = true
163-
console.log(currentBranch)
164-
165-
const list = await this.git.branch([])
166-
list.all.forEach((item: string) => {
167-
if (item === setting.branch) {
168-
hasNewBranch = false
169-
}
170-
})
206+
const currentBranch = await git.currentBranch({ fs, dir: this.outputDir, fullname: false })
207+
const localBranches = await git.listBranches({ fs, dir: this.outputDir })
171208

172209
if (currentBranch !== setting.branch) {
173-
if (hasNewBranch) {
174-
await this.git.checkout(['-b', setting.branch])
175-
} else {
176-
try {
177-
await this.git.deleteLocalBranch(setting.branch)
178-
} catch (e) {
179-
console.log(e)
180-
} finally {
181-
await this.git.checkout(['-b', setting.branch])
182-
}
210+
if (!localBranches.includes(setting.branch)) {
211+
await git.branch({ fs, dir: this.outputDir, ref: setting.branch })
183212
}
213+
214+
await git.fastCheckout({ fs, dir: this.outputDir, ref: setting.branch })
184215
}
185-
return {}
186216
}
187217
}

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10835,9 +10835,9 @@ vue-class-component@^6.0.0, vue-class-component@^6.2.0:
1083510835
resolved "http://registry.npm.taobao.org/vue-class-component/download/vue-class-component-6.3.2.tgz#e6037e84d1df2af3bde4f455e50ca1b9eec02be6"
1083610836

1083710837
vue-cli-plugin-electron-builder@^2.0.0:
10838-
version "2.0.0-beta.1"
10839-
resolved "https://registry.yarnpkg.com/vue-cli-plugin-electron-builder/-/vue-cli-plugin-electron-builder-2.0.0-beta.1.tgz#ea7748877391ae617f0583306fd56773978a94db"
10840-
integrity sha512-AhLPZbiVXEWUfWms7ikmxOWgM7MdELihfU/W5x6wz6xmuoVji3/ALlYcO/Qq4x5yed5nNF7Q0GMeF/CUH8rvyw==
10838+
version "2.0.0-beta.2"
10839+
resolved "https://registry.yarnpkg.com/vue-cli-plugin-electron-builder/-/vue-cli-plugin-electron-builder-2.0.0-beta.2.tgz#b06839a9b86056079cb77fa51a6688e948082afa"
10840+
integrity sha512-hirLBvY+OpA8+uU35GnDYsGdbYAd6rO7gnjwr7vJ5HcsyPooZK4cYMxHjHRJI+g63ypBHkpt7ST605Gpk0likA==
1084110841
dependencies:
1084210842
chokidar "^3.0.2"
1084310843
electron-builder "^22.2.0"

0 commit comments

Comments
 (0)