11'use strict' ;
22
3- const shelljs = require ( 'shelljs' ) ;
43const path = require ( 'path' ) ;
5- const { fs, _, chalk } = require ( '@micro-app/shared-utils' ) ;
4+ const { fs, _, chalk, execa , Env } = require ( '@micro-app/shared-utils' ) ;
65const CONSTANTS = require ( '../../constants' ) ;
7- const { execJS , getCurrBranch, getGitBranch, getGitUser } = require ( './utils' ) ;
6+ const { execGit , getCurrBranch, getGitBranch, getGitUser } = require ( './utils' ) ;
87
98function createCNAMEFile ( { deployConfig, deployDir } ) {
109 const cname = deployConfig . cname ;
@@ -18,23 +17,22 @@ async function setup(api, { deployDir, gitUser }) {
1817 await fs . ensureDir ( deployDir ) ;
1918 await fs . emptyDir ( deployDir ) ;
2019
21- await execJS ( 'git init', { cwd : deployDir } ) ;
20+ await execGit ( [ ' init' ] , { cwd : deployDir } ) ;
2221
2322 // git config
2423 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 } ) ;
2625 }
2726 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 } ) ;
2928 }
3029}
3130
3231async 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 } ) ;
3533}
3634
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 } ) {
3836 const currBranch = getCurrBranch ( ) ;
3937 // commit + push
4038 const { message } = api . applyPluginHooks ( 'modifyCommandDeployMessage' , {
@@ -50,36 +48,34 @@ async function gitPush(api, { args, deployConfig, deployDir, gitURL, gitBranch,
5048 throw new Error ( 'modifyCommandDeployMessage() must be retrun { message } !!!' ) ;
5149 }
5250
51+ let chain = Promise . resolve ( ) ;
52+
5353 if ( process . env . MICRO_APP_TEST ) {
5454 api . logger . debug ( 'MICRO_APP_TEST --> Exit!!!' ) ;
55- return ;
55+ return chain ;
5656 }
5757
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 } ) ) ;
6561
66- return true ;
62+ return chain ;
6763}
6864
6965function getCommitHash ( api , { isHooks, gitBranch } ) {
7066 let commitHash = '' ;
7167 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 ( ) ;
7369 } 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 ( ) ;
7571 }
7672 return commitHash ;
7773}
7874
7975function getGitMessage ( api , { deployConfig, commitHash } ) {
8076 let gitMessage = deployConfig . message && ` | ${ deployConfig . message } ` || '' ;
8177 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 ( ) ;
8379 if ( msg ) {
8480 gitMessage = ` | ${ msg } ` ;
8581 }
@@ -139,6 +135,10 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
139135 const logger = api . logger ;
140136 const root = api . root ;
141137
138+ if ( ! Env . hasGit ( ) ) {
139+ logger . throw ( 'Sorry, this script requires git' ) ;
140+ }
141+
142142 const isHooks = args . hooks ;
143143
144144 return Promise . all ( deployConfigs . map ( async ( deployConfig , index ) => {
@@ -178,8 +178,8 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
178178 }
179179
180180 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 ) ;
183183 }
184184
185185 return params ;
0 commit comments