@@ -4,47 +4,7 @@ const shelljs = require('shelljs');
44const path = require ( 'path' ) ;
55const { fs, _, chalk } = require ( '@micro-app/shared-utils' ) ;
66const 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' ) ;
488
499function createCNAMEFile ( { deployConfig, deployDir } ) {
5010 const cname = deployConfig . cname ;
@@ -54,23 +14,32 @@ function createCNAMEFile({ deployConfig, deployDir }) {
5414 }
5515}
5616
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 } ) ;
6122
62- async function push ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name } ) {
6323 // git config
6424 if ( gitUser . name && typeof gitUser . name === 'string' ) {
6525 await execJS ( `git config user.name ${ gitUser . name } ` , { cwd : deployDir } ) ;
6626 }
6727 if ( gitUser . email && typeof gitUser . email === 'string' ) {
6828 await execJS ( `git config user.email ${ gitUser . email } ` , { cwd : deployDir } ) ;
6929 }
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 ( ) ;
7039 // commit + push
7140 const { message } = api . applyPluginHooks ( 'modifyCommandDeployMessage' , {
7241 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 } ` ,
7443 branch : gitBranch ,
7544 gitMessage,
7645 commitHash,
@@ -86,17 +55,36 @@ async function push(api, { args, deployConfig, deployDir, gitURL, gitBranch, com
8655 return ;
8756 }
8857
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 ;
9267}
9368
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+ }
9778
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 ;
10088}
10189
10290async function runDeploy ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage } ) {
@@ -113,7 +101,8 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
113101 const spinner = logger . spinner ( 'Auto Deploy...' ) ;
114102 spinner . start ( ) ;
115103 try {
116- await setup ( deployDir ) ;
104+ await setup ( api , { deployDir, gitUser } ) ;
105+
117106 const hasDist = deployConfig . dist ;
118107 let bModify = false ;
119108 if ( ! hasDist ) { // 需要 clone, 且自动修改 package.json
@@ -134,7 +123,7 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
134123
135124 if ( bModify ) {
136125 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 } ) ;
138127 spinner . succeed ( chalk . green ( 'Success!' ) ) ;
139128 } else {
140129 spinner . succeed ( chalk . yellow ( 'NOT MODIFIED!' ) ) ;
@@ -166,24 +155,13 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
166155 }
167156 const gitUser = getGitUser ( deployConfig ) ;
168157
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 } ) ;
175159 if ( _ . isEmpty ( commitHash ) ) {
176160 logger . warn ( 'Not Found commit Hash!' ) ;
177161 return ;
178162 }
179163
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 } ) ;
187165
188166 const gitRoot = path . resolve ( root , CONSTANTS . GIT_NAME ) ;
189167 if ( ! fs . existsSync ( gitRoot ) ) {
@@ -192,19 +170,18 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
192170 const deployDir = path . resolve ( gitRoot , CONSTANTS . GIT_SCOPE_NAME ) ;
193171
194172 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 ) ;
202174
203175 // 清空
204176 if ( fs . existsSync ( deployDir ) ) {
205177 fs . removeSync ( deployDir ) ;
206178 }
207179
180+ if ( ! bSuccessful ) {
181+ logger . error ( `Fail [${ index } ]! Check your config, please!` ) ;
182+ process . exitCode = 1 ;
183+ }
184+
208185 return params ;
209186 } ) ) ;
210187} ;
0 commit comments