@@ -181,7 +181,6 @@ class ProjectBuilder {
181181 }
182182
183183 const projectBuildContexts = await this . _createRequiredBuildContexts ( requestedProjects ) ;
184- const cleanupSigHooks = this . _registerCleanupSigHooks ( ) ;
185184 let fsTarget ;
186185 if ( destPath ) {
187186 fsTarget = resourceFactory . createAdapter ( {
@@ -191,7 +190,6 @@ class ProjectBuilder {
191190 }
192191
193192 const queue = [ ] ;
194- const alreadyBuilt = [ ] ;
195193
196194 // Create build queue based on graph depth-first search to ensure correct build order
197195 await this . _graph . traverseDepthFirst ( async ( { project} ) => {
@@ -202,15 +200,38 @@ class ProjectBuilder {
202200 // => This project needs to be built or, in case it has already
203201 // been built, it's build result needs to be written out (if requested)
204202 queue . push ( projectBuildContext ) ;
205- if ( ! await projectBuildContext . requiresBuild ( ) ) {
206- alreadyBuilt . push ( projectName ) ;
207- }
208203 }
209204 } ) ;
210205
206+ if ( destPath && cleanDest ) {
207+ this . #log. info ( `Cleaning target directory...` ) ;
208+ await rmrf ( destPath ) ;
209+ }
210+
211+ await this . #build( queue , projectBuildContexts , requestedProjects , fsTarget ) ;
212+
213+ if ( watch ) {
214+ const relevantProjects = queue . map ( ( projectBuildContext ) => {
215+ return projectBuildContext . getProject ( ) ;
216+ } ) ;
217+ return this . _buildContext . initWatchHandler ( relevantProjects , async ( ) => {
218+ await this . #updateBuild( projectBuildContexts , requestedProjects , fsTarget ) ;
219+ } ) ;
220+ }
221+ }
222+
223+ async #build( queue , projectBuildContexts , requestedProjects , fsTarget ) {
211224 this . #log. setProjects ( queue . map ( ( projectBuildContext ) => {
212225 return projectBuildContext . getProject ( ) . getName ( ) ;
213226 } ) ) ;
227+
228+ const alreadyBuilt = [ ] ;
229+ for ( const projectBuildContext of queue ) {
230+ if ( ! await projectBuildContext . requiresBuild ( ) ) {
231+ const projectName = projectBuildContext . getProject ( ) . getName ( ) ;
232+ alreadyBuilt . push ( projectName ) ;
233+ }
234+ }
214235 if ( queue . length > 1 ) { // Do not log if only the root project is being built
215236 this . #log. info ( `Processing ${ queue . length } projects` ) ;
216237 if ( alreadyBuilt . length ) {
@@ -240,13 +261,9 @@ class ProjectBuilder {
240261 . join ( "\n " ) } `) ;
241262 }
242263 }
243-
244- if ( destPath && cleanDest ) {
245- this . #log. info ( `Cleaning target directory...` ) ;
246- await rmrf ( destPath ) ;
247- }
248- const startTime = process . hrtime ( ) ;
264+ const cleanupSigHooks = this . _registerCleanupSigHooks ( ) ;
249265 try {
266+ const startTime = process . hrtime ( ) ;
250267 const pWrites = [ ] ;
251268 for ( const projectBuildContext of queue ) {
252269 const project = projectBuildContext . getProject ( ) ;
@@ -285,21 +302,26 @@ class ProjectBuilder {
285302 await Promise . all ( pWrites ) ;
286303 this . #log. info ( `Build succeeded in ${ this . _getElapsedTime ( startTime ) } ` ) ;
287304 } catch ( err ) {
288- this . #log. error ( `Build failed in ${ this . _getElapsedTime ( startTime ) } ` ) ;
305+ this . #log. error ( `Build failed` ) ;
289306 throw err ;
290307 } finally {
291308 this . _deregisterCleanupSigHooks ( cleanupSigHooks ) ;
292309 await this . _executeCleanupTasks ( ) ;
293310 }
311+ }
294312
295- if ( watch ) {
296- const relevantProjects = queue . map ( ( projectBuildContext ) => {
297- return projectBuildContext . getProject ( ) ;
298- } ) ;
299- const watchHandler = this . _buildContext . initWatchHandler ( relevantProjects , async ( ) => {
300- await this . #update( projectBuildContexts , requestedProjects , fsTarget ) ;
301- } ) ;
302- return watchHandler ;
313+ async #updateBuild( projectBuildContexts , requestedProjects , fsTarget ) {
314+ const cleanupSigHooks = this . _registerCleanupSigHooks ( ) ;
315+ try {
316+ const startTime = process . hrtime ( ) ;
317+ await this . #update( projectBuildContexts , requestedProjects , fsTarget ) ;
318+ this . #log. info ( `Update succeeded in ${ this . _getElapsedTime ( startTime ) } ` ) ;
319+ } catch ( err ) {
320+ this . #log. error ( `Update failed` ) ;
321+ this . #log. error ( err ) ;
322+ } finally {
323+ this . _deregisterCleanupSigHooks ( cleanupSigHooks ) ;
324+ await this . _executeCleanupTasks ( ) ;
303325 }
304326 }
305327
0 commit comments