@@ -4,47 +4,7 @@ const shelljs = require('shelljs');
4
4
const path = require ( 'path' ) ;
5
5
const { fs, _, chalk } = require ( '@micro-app/shared-utils' ) ;
6
6
const CONSTANTS = require ( '../../constants' ) ;
7
-
8
- function execJS ( execStr , options = { } ) {
9
- return new Promise ( ( resolve , reject ) => {
10
- shelljs . exec ( execStr , Object . assign ( { silent : true , async : true , stdio : 'inherit' } , options ) , function ( code , stdout , stderr ) {
11
- if ( code && stderr ) {
12
- reject ( new Error ( stderr ) ) ;
13
- } else {
14
- resolve ( stdout ) ;
15
- }
16
- } ) ;
17
- } ) ;
18
- }
19
-
20
- function getGitBranch ( deployConfig ) {
21
- let gitBranch = deployConfig . branch || 'master' ;
22
- const currBranch = ( ( shelljs . exec ( 'git rev-parse --abbrev-ref HEAD' , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
23
- // 继承当前分支
24
- if ( currBranch && deployConfig . extends === true ) {
25
- gitBranch = currBranch ;
26
- } else if ( deployConfig . branch ) {
27
- gitBranch = deployConfig . branch ;
28
- } else {
29
- gitBranch = 'master' ;
30
- }
31
- return gitBranch ;
32
- }
33
-
34
- function getGitUser ( deployConfig ) {
35
- let userName = deployConfig . userName ;
36
- if ( _ . isEmpty ( userName ) ) {
37
- userName = ( ( shelljs . exec ( 'git config user.name' , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
38
- }
39
- let userEmail = deployConfig . userEmail ;
40
- if ( _ . isEmpty ( userEmail ) ) {
41
- userEmail = ( ( shelljs . exec ( 'git config user.email' , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
42
- }
43
- return {
44
- name : userName || '' ,
45
- email : userEmail || '' ,
46
- } ;
47
- }
7
+ const { execJS, getCurrBranch, getGitBranch, getGitUser } = require ( './utils' ) ;
48
8
49
9
function createCNAMEFile ( { deployConfig, deployDir } ) {
50
10
const cname = deployConfig . cname ;
@@ -54,23 +14,32 @@ function createCNAMEFile({ deployConfig, deployDir }) {
54
14
}
55
15
}
56
16
57
- async function clone ( api , { deployDir, gitURL, gitBranch } ) {
58
- const execStr = `git clone "${ gitURL } " -b ${ gitBranch } "${ deployDir } "` ;
59
- return await execJS ( execStr ) ;
60
- }
17
+ async function setup ( api , { deployDir, gitUser } ) {
18
+ await fs . ensureDir ( deployDir ) ;
19
+ await fs . emptyDir ( deployDir ) ;
20
+
21
+ await execJS ( 'git init' , { cwd : deployDir } ) ;
61
22
62
- async function push ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name } ) {
63
23
// git config
64
24
if ( gitUser . name && typeof gitUser . name === 'string' ) {
65
25
await execJS ( `git config user.name ${ gitUser . name } ` , { cwd : deployDir } ) ;
66
26
}
67
27
if ( gitUser . email && typeof gitUser . email === 'string' ) {
68
28
await execJS ( `git config user.email ${ gitUser . email } ` , { cwd : deployDir } ) ;
69
29
}
30
+ }
31
+
32
+ async function clone ( api , { deployDir, gitURL, gitBranch } ) {
33
+ const execStr = `git clone "${ gitURL } " -b ${ gitBranch } "${ deployDir } "` ;
34
+ return await execJS ( execStr ) ;
35
+ }
36
+
37
+ async function gitPush ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name } ) {
38
+ const currBranch = getCurrBranch ( ) ;
70
39
// commit + push
71
40
const { message } = api . applyPluginHooks ( 'modifyCommandDeployMessage' , {
72
41
args, config : deployConfig ,
73
- message : `:rocket: auto deploy from ${ name } [${ gitBranch } ] - ${ commitHash . substr ( 0 , 8 ) } ${ gitMessage } ` ,
42
+ message : `:rocket: auto deploy from ${ name } [${ currBranch || gitBranch } ] - ${ commitHash . substr ( 0 , 8 ) } ${ gitMessage } ` ,
74
43
branch : gitBranch ,
75
44
gitMessage,
76
45
commitHash,
@@ -86,17 +55,36 @@ async function push(api, { args, deployConfig, deployDir, gitURL, gitBranch, com
86
55
return ;
87
56
}
88
57
89
- await execJS ( 'git add -A' , { cwd : deployDir } ) ;
90
- await execJS ( `git commit -a -m "${ message } "` , { cwd : deployDir } ) ;
91
- await execJS ( `git push -u "${ gitURL } " HEAD:${ gitBranch } --force` , { cwd : deployDir } ) ;
58
+ try {
59
+ await execJS ( 'git add -A' , { cwd : deployDir } ) ;
60
+ await execJS ( `git commit -a -m "${ message } "` , { cwd : deployDir } ) ;
61
+ await execJS ( `git push -u "${ gitURL } " HEAD:${ gitBranch } --force` , { cwd : deployDir } ) ;
62
+ } catch ( error ) {
63
+ throw error ;
64
+ }
65
+
66
+ return true ;
92
67
}
93
68
94
- async function setup ( deployDir ) {
95
- await fs . ensureDir ( deployDir ) ;
96
- await fs . emptyDir ( deployDir ) ;
69
+ function getCommitHash ( api , { isHooks, gitBranch } ) {
70
+ let commitHash = '' ;
71
+ if ( isHooks ) {
72
+ commitHash = ( ( shelljs . exec ( 'git rev-parse --verify HEAD' , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
73
+ } else {
74
+ commitHash = ( ( shelljs . exec ( `git rev-parse origin/${ gitBranch } ` , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
75
+ }
76
+ return commitHash ;
77
+ }
97
78
98
- const execStr = 'git init' ;
99
- return await execJS ( execStr , { cwd : deployDir } ) ;
79
+ function getGitMessage ( api , { deployConfig, commitHash } ) {
80
+ let gitMessage = deployConfig . message && ` | ${ deployConfig . message } ` || '' ;
81
+ if ( ! gitMessage ) {
82
+ const msg = ( ( shelljs . exec ( `git log --pretty=format:“%s” ${ commitHash } -1` , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
83
+ if ( msg ) {
84
+ gitMessage = ` | ${ msg } ` ;
85
+ }
86
+ }
87
+ return gitMessage ;
100
88
}
101
89
102
90
async function runDeploy ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage } ) {
@@ -113,7 +101,8 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
113
101
const spinner = logger . spinner ( 'Auto Deploy...' ) ;
114
102
spinner . start ( ) ;
115
103
try {
116
- await setup ( deployDir ) ;
104
+ await setup ( api , { deployDir, gitUser } ) ;
105
+
117
106
const hasDist = deployConfig . dist ;
118
107
let bModify = false ;
119
108
if ( ! hasDist ) { // 需要 clone, 且自动修改 package.json
@@ -134,7 +123,7 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
134
123
135
124
if ( bModify ) {
136
125
spinner . text = 'Push files...' ;
137
- await push ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name : MICRO_APP_CONFIG_NAME } ) ;
126
+ await gitPush ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name : MICRO_APP_CONFIG_NAME } ) ;
138
127
spinner . succeed ( chalk . green ( 'Success!' ) ) ;
139
128
} else {
140
129
spinner . succeed ( chalk . yellow ( 'NOT MODIFIED!' ) ) ;
@@ -166,24 +155,13 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
166
155
}
167
156
const gitUser = getGitUser ( deployConfig ) ;
168
157
169
- let commitHash = '' ;
170
- if ( isHooks ) {
171
- commitHash = ( ( shelljs . exec ( 'git rev-parse --verify HEAD' , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
172
- } else {
173
- commitHash = ( ( shelljs . exec ( `git rev-parse origin/${ gitBranch } ` , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
174
- }
158
+ const commitHash = getCommitHash ( api , { isHooks, gitBranch } ) ;
175
159
if ( _ . isEmpty ( commitHash ) ) {
176
160
logger . warn ( 'Not Found commit Hash!' ) ;
177
161
return ;
178
162
}
179
163
180
- let gitMessage = deployConfig . message && ` | ${ deployConfig . message } ` || '' ;
181
- if ( ! gitMessage ) {
182
- const msg = ( ( shelljs . exec ( `git log --pretty=format:“%s” ${ commitHash } -1` , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
183
- if ( msg ) {
184
- gitMessage = ` | ${ msg } ` ;
185
- }
186
- }
164
+ const gitMessage = getGitMessage ( api , { deployConfig, commitHash } ) ;
187
165
188
166
const gitRoot = path . resolve ( root , CONSTANTS . GIT_NAME ) ;
189
167
if ( ! fs . existsSync ( gitRoot ) ) {
@@ -192,19 +170,18 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
192
170
const deployDir = path . resolve ( gitRoot , CONSTANTS . GIT_SCOPE_NAME ) ;
193
171
194
172
const params = { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage } ;
195
-
196
- if ( fs . statSync ( gitRoot ) . isDirectory ( ) ) {
197
- const bSuccessful = await runDeploy ( api , params ) ;
198
- if ( ! bSuccessful ) {
199
- logger . error ( `Fail [${ index } ]! Check your config, please!` ) ;
200
- }
201
- }
173
+ const bSuccessful = await runDeploy ( api , params ) ;
202
174
203
175
// 清空
204
176
if ( fs . existsSync ( deployDir ) ) {
205
177
fs . removeSync ( deployDir ) ;
206
178
}
207
179
180
+ if ( ! bSuccessful ) {
181
+ logger . error ( `Fail [${ index } ]! Check your config, please!` ) ;
182
+ process . exitCode = 1 ;
183
+ }
184
+
208
185
return params ;
209
186
} ) ) ;
210
187
} ;
0 commit comments