1- import simpleGit , { SimpleGit } from 'simple-git/promise'
2- import * as isoGit from 'isomorphic-git'
31import fs from 'fs'
42import moment from 'moment'
3+ // @ts -ignore
4+ import * as git from 'isomorphic-git/dist/for-node/isomorphic-git/index'
55import Model from './model'
66
77export 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}
0 commit comments