1
1
'use strict' ;
2
2
3
- const shelljs = require ( 'shelljs' ) ;
4
3
const path = require ( 'path' ) ;
5
- const { fs, _, chalk } = require ( '@micro-app/shared-utils' ) ;
4
+ const { fs, _, chalk, execa , Env } = require ( '@micro-app/shared-utils' ) ;
6
5
const CONSTANTS = require ( '../../constants' ) ;
7
- const { execJS , getCurrBranch, getGitBranch, getGitUser } = require ( './utils' ) ;
6
+ const { execGit , getCurrBranch, getGitBranch, getGitUser } = require ( './utils' ) ;
8
7
9
8
function createCNAMEFile ( { deployConfig, deployDir } ) {
10
9
const cname = deployConfig . cname ;
@@ -18,23 +17,22 @@ async function setup(api, { deployDir, gitUser }) {
18
17
await fs . ensureDir ( deployDir ) ;
19
18
await fs . emptyDir ( deployDir ) ;
20
19
21
- await execJS ( 'git init', { cwd : deployDir } ) ;
20
+ await execGit ( [ ' init' ] , { cwd : deployDir } ) ;
22
21
23
22
// git config
24
23
if ( gitUser . name && typeof gitUser . name === 'string' ) {
25
- await execJS ( `git config user.name ${ gitUser . name } ` , { cwd : deployDir } ) ;
24
+ await execGit ( [ ' config' , ' user.name' , gitUser . name ] , { cwd : deployDir } ) ;
26
25
}
27
26
if ( gitUser . email && typeof gitUser . email === 'string' ) {
28
- await execJS ( `git config user.email ${ gitUser . email } ` , { cwd : deployDir } ) ;
27
+ await execGit ( [ ' config' , ' user.email' , gitUser . email ] , { cwd : deployDir } ) ;
29
28
}
30
29
}
31
30
32
31
async function clone ( api , { deployDir, gitURL, gitBranch } ) {
33
- const execStr = `git clone "${ gitURL } " -b ${ gitBranch } "${ deployDir } "` ;
34
- return await execJS ( execStr ) ;
32
+ return await execGit ( [ 'clone' , gitURL , '-b' , gitBranch , deployDir ] , { cwd : deployDir } ) ;
35
33
}
36
34
37
- async function gitPush ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name } ) {
35
+ function gitPush ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage, name } ) {
38
36
const currBranch = getCurrBranch ( ) ;
39
37
// commit + push
40
38
const { message } = api . applyPluginHooks ( 'modifyCommandDeployMessage' , {
@@ -50,36 +48,34 @@ async function gitPush(api, { args, deployConfig, deployDir, gitURL, gitBranch,
50
48
throw new Error ( 'modifyCommandDeployMessage() must be retrun { message } !!!' ) ;
51
49
}
52
50
51
+ let chain = Promise . resolve ( ) ;
52
+
53
53
if ( process . env . MICRO_APP_TEST ) {
54
54
api . logger . debug ( 'MICRO_APP_TEST --> Exit!!!' ) ;
55
- return ;
55
+ return chain ;
56
56
}
57
57
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
- }
58
+ chain = chain . then ( ( ) => execGit ( [ 'add' , '-A' ] , { cwd : deployDir } ) ) ;
59
+ chain = chain . then ( ( ) => execGit ( [ 'commit' , '-a' , '-m' , message ] , { cwd : deployDir } ) ) ;
60
+ chain = chain . then ( ( ) => execGit ( [ 'push' , '-u' , gitURL , `HEAD:${ gitBranch } ` , '--force' ] , { cwd : deployDir } ) ) ;
65
61
66
- return true ;
62
+ return chain ;
67
63
}
68
64
69
65
function getCommitHash ( api , { isHooks, gitBranch } ) {
70
66
let commitHash = '' ;
71
67
if ( isHooks ) {
72
- commitHash = ( ( shelljs . exec ( 'git rev-parse --verify HEAD' , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
68
+ commitHash = ( ( execa . commandSync ( 'git rev-parse --verify HEAD' , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
73
69
} else {
74
- commitHash = ( ( shelljs . exec ( `git rev-parse origin/${ gitBranch } ` , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
70
+ commitHash = ( ( execa . commandSync ( `git rev-parse origin/${ gitBranch } ` , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
75
71
}
76
72
return commitHash ;
77
73
}
78
74
79
75
function getGitMessage ( api , { deployConfig, commitHash } ) {
80
76
let gitMessage = deployConfig . message && ` | ${ deployConfig . message } ` || '' ;
81
77
if ( ! gitMessage ) {
82
- const msg = ( ( shelljs . exec ( `git log --pretty=format:“%s” ${ commitHash } -1` , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
78
+ const msg = ( ( execa . commandSync ( `git log --pretty=format:“%s” ${ commitHash } -1` , { silent : true } ) || { } ) . stdout || '' ) . trim ( ) ;
83
79
if ( msg ) {
84
80
gitMessage = ` | ${ msg } ` ;
85
81
}
@@ -139,6 +135,10 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
139
135
const logger = api . logger ;
140
136
const root = api . root ;
141
137
138
+ if ( ! Env . hasGit ( ) ) {
139
+ logger . throw ( 'Sorry, this script requires git' ) ;
140
+ }
141
+
142
142
const isHooks = args . hooks ;
143
143
144
144
return Promise . all ( deployConfigs . map ( async ( deployConfig , index ) => {
@@ -178,8 +178,8 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
178
178
}
179
179
180
180
if ( ! bSuccessful ) {
181
- logger . error ( `Fail [${ index } ]! Check your config, please!` ) ;
182
- process . exitCode = 1 ;
181
+ logger . error ( `Fail${ index && ` [${ index } ]` } ! Check your config, please!` ) ;
182
+ process . exit ( 1 ) ;
183
183
}
184
184
185
185
return params ;
0 commit comments