@@ -9,17 +9,18 @@ import { pri, tempPath, declarationPath } from '../../../node';
99import { buildComponent } from '../../command-build/plugin/build' ;
1010import { commandBundle } from '../../command-bundle/plugin/command-bundle' ;
1111import { isWorkingTreeClean , getCurrentBranchName } from '../../../utils/git-operate' ;
12- import { logFatal , logInfo , spinner } from '../../../utils/log' ;
12+ import { logInfo , spinner } from '../../../utils/log' ;
1313import { getMonoAndNpmDepsOnce , DepMap } from '../../../utils/packages' ;
1414import { PackageInfo } from '../../../utils/define' ;
1515import {
1616 buildDeclaration ,
1717 generateVersion ,
1818 addTagAndPush ,
1919 moveSourceFilesToTempFolderAndPublish ,
20- cleanWorkingTree ,
2120 upgradePackageVersionAndPush ,
2221 generateTag ,
22+ prePareParamsBeforePublish ,
23+ checkEnvBeforePublish ,
2324} from './utils.js' ;
2425
2526export const publish = async ( options : PublishOption ) => {
@@ -58,8 +59,8 @@ export const publish = async (options: PublishOption) => {
5859
5960 await buildDeclaration ( ) ;
6061
61- if ( installAllPrompt . publishAllPackages && currentSelectedSourceType === 'root' ) {
62- // parallel publish root
62+ if ( installAllPrompt . publishAllPackages ) {
63+ // async publish
6364 await publishPackageAndItsMonoPackage (
6465 currentSelectedSourceType ,
6566 options ,
@@ -68,11 +69,8 @@ export const publish = async (options: PublishOption) => {
6869 isDevelopBranch ,
6970 currentBranchName ,
7071 ) ;
71- } else if ( installAllPrompt . publishAllPackages && currentSelectedSourceType !== 'root' ) {
72- // Serial publish non root situation TODO: change to parallel
73- for ( const eachPackage of depMonoPackages ) {
74- await publishByPackageName ( eachPackage . name , options , depMap , isDevelopBranch , currentBranchName ) ;
75- }
72+ } else {
73+ await publishByPackageName ( currentSelectedSourceType , options , depMap , isDevelopBranch , currentBranchName ) ;
7674 }
7775 } else {
7876 await buildDeclaration ( ) ;
@@ -103,42 +101,16 @@ async function publishByPackageName(
103101) {
104102 logInfo ( `Start publish ${ sourceType } .` ) ;
105103
106- const targetPackageJson =
107- sourceType === 'root'
108- ? pri . projectPackageJson || { }
109- : pri . packages . find ( eachPackage => eachPackage . name === sourceType ) . packageJson || { } ;
110-
111- const targetConfig =
112- sourceType === 'root'
113- ? pri . projectConfig
114- : pri . packages . find ( eachPackage => eachPackage . name === sourceType ) . config ;
115-
116- const targetRoot =
117- sourceType === 'root'
118- ? pri . projectRootPath
119- : pri . packages . find ( eachPackage => eachPackage . name === sourceType ) . rootPath ;
120-
121- await fs . remove ( path . join ( pri . projectRootPath , pri . sourceConfig . distDir ) ) ;
104+ const { targetPackageJson, targetConfig, targetRoot, targetPackageInfo } = prePareParamsBeforePublish ( sourceType ) ;
122105
123106 // Change source config here
124107 pri . sourceConfig = targetConfig ;
125108 pri . sourceRoot = targetRoot ;
126109 pri . selectedSourceType = sourceType ;
127110
128- if ( ! targetPackageJson . name ) {
129- logFatal ( `No name found in ${ sourceType } package.json` ) ;
130- }
131-
132- if ( ! targetPackageJson . version ) {
133- logFatal ( `No version found in ${ sourceType } package.json` ) ;
134- }
135-
136- // clean workingTree before publish
137- if ( ! ( await isWorkingTreeClean ( ) ) ) {
138- await cleanWorkingTree ( ) ;
139- }
111+ await fs . remove ( path . join ( pri . projectRootPath , pri . sourceConfig . distDir ) ) ;
140112
141- logInfo ( 'Check if npm package exist' ) ;
113+ await checkEnvBeforePublish ( targetPackageJson , sourceType ) ;
142114
143115 targetPackageJson . version = await generateVersion (
144116 options ,
@@ -162,7 +134,7 @@ async function publishByPackageName(
162134 } ) ;
163135 }
164136
165- await buildComponent ( ) ;
137+ await buildComponent ( targetPackageInfo ) ;
166138
167139 if ( options . bundle ) {
168140 await commandBundle ( { skipLint : true } ) ;
@@ -188,102 +160,79 @@ async function publishPackageAndItsMonoPackage(
188160) {
189161 logInfo ( `Start publish ${ sourceType } .` ) ;
190162
191- const currentPackageJson =
192- sourceType === 'root'
193- ? pri . projectPackageJson || { }
194- : pri . packages . find ( eachPackage => eachPackage . name === sourceType ) . packageJson || { } ;
195-
196- const currentConfig =
197- sourceType === 'root'
198- ? pri . projectConfig
199- : pri . packages . find ( eachPackage => eachPackage . name === sourceType ) . config ;
200-
201- const currentRoot =
202- sourceType === 'root'
203- ? pri . projectRootPath
204- : pri . packages . find ( eachPackage => eachPackage . name === sourceType ) . rootPath ;
205-
206- await fs . remove ( path . join ( pri . projectRootPath , pri . sourceConfig . distDir ) ) ;
163+ const { targetPackageJson, targetConfig, targetRoot, targetPackageInfo } = prePareParamsBeforePublish ( sourceType ) ;
207164
208165 // Change source config here
209- pri . sourceConfig = currentConfig ;
210- pri . sourceRoot = currentRoot ;
166+ pri . sourceConfig = targetConfig ;
167+ pri . sourceRoot = targetRoot ;
211168 pri . selectedSourceType = sourceType ;
212169
213- if ( ! currentPackageJson . name ) {
214- logFatal ( `No name found in ${ sourceType } package.json` ) ;
215- }
170+ await fs . remove ( path . join ( pri . projectRootPath , pri . sourceConfig . distDir ) ) ;
216171
217- if ( ! currentPackageJson . version ) {
218- logFatal ( `No version found in ${ sourceType } package.json` ) ;
219- }
172+ await checkEnvBeforePublish ( targetPackageJson , sourceType ) ;
220173
221174 const monoPackageVersion : {
222175 [ key in string ] : string ;
223176 } = { } ;
224177
225- if ( sourceType === 'root' ) {
226- // Generate all package version and upgrade
227- await depMonoPackages . forEach ( async item => {
228- const version = await generateVersion ( options , isDevelopBranch , item . packageJson , item . config , currentBranchName ) ;
178+ // Generate all package version and upgrade
229179
230- monoPackageVersion [ item . name as string ] = version ;
180+ await depMonoPackages . forEach ( async item => {
181+ const version = await generateVersion ( options , isDevelopBranch , item . packageJson , item . config , currentBranchName ) ;
231182
232- item . packageJson . version = version ;
183+ monoPackageVersion [ item . name as string ] = version ;
233184
234- const rootPath = depMonoPackages . find ( eachPackage => eachPackage . name === item . name ) . rootPath ;
185+ item . packageJson . version = version ;
235186
236- await fs . outputFile ( path . join ( rootPath , 'package.json' ) , `${ JSON . stringify ( item . packageJson , null , 2 ) } \n` ) ;
237- } ) ;
187+ const rootPath = item . rootPath ;
238188
239- // Update depMonoPackages version
240- if ( depMap ) {
241- depMap . forEach ( value => {
242- value . depMonoPackages . forEach ( eachPackage => {
243- eachPackage . packageJson . version = monoPackageVersion [ eachPackage . packageJson . name ] ;
244- } ) ;
245- } ) ;
246- }
247- } else {
248- // TODO: add non root situation, the packages dep relation
249- }
189+ await fs . outputFile ( path . join ( rootPath , 'package.json' ) , `${ JSON . stringify ( item . packageJson , null , 2 ) } \n` ) ;
190+ } ) ;
191+
192+ // Update depMonoPackages version
193+
194+ depMonoPackages . forEach ( item => {
195+ depMap . get ( item . name ) . depMonoPackages . forEach ( eachPackage => {
196+ eachPackage . packageJson . version = monoPackageVersion [ eachPackage . packageJson . name ] ;
197+ } ) ;
198+ } ) ;
250199
251200 // current package version
252- const projectVersion = await generateVersion (
201+ const currentVersion = await generateVersion (
253202 options ,
254203 isDevelopBranch ,
255- currentPackageJson ,
256- currentConfig ,
204+ targetPackageJson ,
205+ targetConfig ,
257206 currentBranchName ,
258207 ) ;
259208
260- currentPackageJson . version = projectVersion ;
209+ targetPackageJson . version = currentVersion ;
261210
262- await fs . outputFile ( path . join ( currentRoot , 'package.json' ) , `${ JSON . stringify ( currentPackageJson , null , 2 ) } \n` ) ;
211+ await fs . outputFile ( path . join ( targetRoot , 'package.json' ) , `${ JSON . stringify ( targetPackageJson , null , 2 ) } \n` ) ;
263212
264213 if ( ! ( await isWorkingTreeClean ( ) ) ) {
265- const commitMessage = `upgrade ${ sourceType } version to ${ currentPackageJson . version } \n\n${ depMonoPackages
214+ const commitMessage = `upgrade ${ sourceType } version to ${ targetPackageJson . version } \n\n${ depMonoPackages
266215 . map ( i => `upgrade ${ i . name } version to ${ i . packageJson . version } \n\n` )
267216 . join ( '' ) } `;
268217 await exec ( `git add -A; git commit -m "${ commitMessage } " -n` , {
269218 cwd : pri . projectRootPath ,
270219 } ) ;
271220 }
272221
273- await buildComponent ( ) ;
274-
275- if ( options . bundle ) {
276- await commandBundle ( { skipLint : true } ) ;
277- }
278-
279- /** parallel publish queue & tag with push */
222+ // async publish queue & add tag and push
280223 const publishQueue = depMonoPackages . map ( item => {
281224 return new Promise ( async resolve => {
225+ await buildComponent ( item ) ;
226+
227+ if ( options . bundle ) {
228+ await commandBundle ( { skipLint : true } ) ;
229+ }
230+
282231 await moveSourceFilesToTempFolderAndPublish (
283232 item . name ,
284233 options ,
285234 item . config ,
286- pri . packages . find ( eachPackage => eachPackage . name === item . name ) . rootPath ,
235+ item . rootPath ,
287236 depMap ,
288237 isDevelopBranch ,
289238 ) ;
@@ -296,16 +245,22 @@ async function publishPackageAndItsMonoPackage(
296245 // push current package into publishQueue
297246 publishQueue . push (
298247 new Promise ( async resolve => {
248+ await buildComponent ( targetPackageInfo ) ;
249+
250+ if ( options . bundle ) {
251+ await commandBundle ( { skipLint : true } ) ;
252+ }
253+
299254 await moveSourceFilesToTempFolderAndPublish (
300255 sourceType ,
301256 options ,
302- currentConfig ,
303- currentRoot ,
257+ targetConfig ,
258+ targetRoot ,
304259 depMap ,
305260 isDevelopBranch ,
306261 ) ;
307262
308- await addTagAndPush ( generateTag ( sourceType , currentPackageJson ) , currentPackageJson ) ;
263+ await addTagAndPush ( generateTag ( sourceType , targetPackageJson ) , targetPackageJson ) ;
309264
310265 resolve ( ) ;
311266 } ) ,
0 commit comments