22
33const shelljs = require ( 'shelljs' ) ;
44const path = require ( 'path' ) ;
5- const { fs, _, chalk, tryRequire } = require ( '@micro-app/shared-utils' ) ;
5+ const { fs, _, chalk } = require ( '@micro-app/shared-utils' ) ;
66const CONSTANTS = require ( '../../constants' ) ;
77
88function execJS ( execStr , options = { } ) {
@@ -54,40 +54,6 @@ function createCNAMEFile({ deployConfig, deployDir }) {
5454 }
5555}
5656
57- function modifyFile ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage } ) {
58- const logger = api . logger ;
59- const microAppConfig = api . self ;
60-
61- const pkgPath = path . resolve ( deployDir , 'package.json' ) ;
62- const pkg = tryRequire ( pkgPath ) || { } ;
63- const { dependencies = { } , devDependencies = { } } = pkg ;
64- const deps = Object . assign ( { } , dependencies , devDependencies ) ;
65-
66- const MICRO_APP_CONFIG_NAME = microAppConfig . packageName ;
67- if ( deps [ MICRO_APP_CONFIG_NAME ] ) {
68- const gitp = deps [ MICRO_APP_CONFIG_NAME ] ;
69- // update
70- const ngitp = gitp . replace ( / # [ - _ \d \w ] + $ / igm, `#${ commitHash } ` ) ;
71-
72- if ( gitp === ngitp ) { // not change
73- return false ;
74- }
75- if ( ngitp ) {
76- if ( dependencies [ MICRO_APP_CONFIG_NAME ] ) {
77- dependencies [ MICRO_APP_CONFIG_NAME ] = ngitp ;
78- }
79- if ( devDependencies [ MICRO_APP_CONFIG_NAME ] ) {
80- devDependencies [ MICRO_APP_CONFIG_NAME ] = ngitp ;
81- }
82- fs . writeFileSync ( pkgPath , JSON . stringify ( pkg , null , 4 ) , 'utf8' ) ;
83-
84- return true ;
85- }
86- }
87-
88- return false ;
89- }
90-
9157async function clone ( api , { deployDir, gitURL, gitBranch } ) {
9258 const execStr = `git clone "${ gitURL } " -b ${ gitBranch } "${ deployDir } "` ;
9359 return await execJS ( execStr ) ;
@@ -115,7 +81,7 @@ async function push(api, { args, deployConfig, deployDir, gitURL, gitBranch, com
11581 throw new Error ( 'modifyCommandDeployMessage() must be retrun { message } !!!' ) ;
11682 }
11783
118- if ( process . env . NODE_ENV === ' MICRO_APP_TEST' ) {
84+ if ( process . env . MICRO_APP_TEST ) {
11985 api . logger . debug ( 'MICRO_APP_TEST --> Exit!!!' ) ;
12086 return ;
12187 }
@@ -126,9 +92,7 @@ async function push(api, { args, deployConfig, deployDir, gitURL, gitBranch, com
12692}
12793
12894async function setup ( deployDir ) {
129- if ( ! fs . existsSync ( deployDir ) ) {
130- fs . mkdirpSync ( deployDir ) ;
131- }
95+ await fs . ensureDir ( deployDir ) ;
13296 await fs . emptyDir ( deployDir ) ;
13397
13498 const execStr = 'git init' ;
@@ -152,10 +116,11 @@ async function runDeploy(api, { args, deployConfig, deployDir, gitURL, gitBranch
152116 await setup ( deployDir ) ;
153117 const hasDist = deployConfig . dist ;
154118 let bModify = false ;
155- if ( ! hasDist ) { // 需要 clone
119+ if ( ! hasDist ) { // 需要 clone, 且自动修改 package.json
156120 spinner . text = 'Cloning...' ;
157121 await clone ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser } ) ;
158122 spinner . text = 'Modify files...' ;
123+ const modifyFile = require ( './modifyFile' ) ;
159124 bModify = modifyFile ( api , { args, deployConfig, deployDir, gitURL, gitBranch, commitHash, gitUser, gitMessage } ) ;
160125 } else { // copy dist to deployDir
161126 const opts = { } ;
@@ -233,10 +198,11 @@ module.exports = async function deployCommit(api, args, deployConfigs) {
233198 if ( ! bSuccessful ) {
234199 logger . error ( `Fail [${ index } ]! Check your config, please!` ) ;
235200 }
236- // 清空
237- if ( fs . existsSync ( deployDir ) ) {
238- fs . removeSync ( deployDir ) ;
239- }
201+ }
202+
203+ // 清空
204+ if ( fs . existsSync ( deployDir ) ) {
205+ fs . removeSync ( deployDir ) ;
240206 }
241207
242208 return params ;
0 commit comments