@@ -14,6 +14,7 @@ import { IBuildConfig } from './IBuildConfig';
1414import { CleanTask } from './tasks/CleanTask' ;
1515import { args } from './State' ;
1616export { IExecutable } from './IExecutable' ;
17+ import { log } from './logging' ;
1718import { initialize as initializeLogging , markTaskCreationTime , generateGulpError , setWatchMode } from './logging' ;
1819import { getFlagValue , setConfigDefaults } from './config' ;
1920import * as gulp from 'gulp' ;
@@ -188,57 +189,71 @@ export function watch(watchMatch: string | string[], task: IExecutable): IExecut
188189 let shouldRerunWatch : boolean = false ;
189190 let lastError : boolean = undefined ;
190191
192+ const successMessage : string = 'Build succeeded' ;
193+ const failureMessage : string = 'Build failed' ;
194+
191195 return {
192196 execute : ( buildConfig : IBuildConfig ) : Promise < void > => {
193-
194- setWatchMode ( ) ;
195- buildConfig . gulp . watch ( watchMatch , _runWatch ) ;
196-
197- function _runWatch ( ) : void {
198- if ( isWatchRunning ) {
199- shouldRerunWatch = true ;
200- } else {
201- isWatchRunning = true ;
202-
203- _executeTask ( task , buildConfig )
204- . then ( ( ) => {
205- if ( buildConfig . showToast && lastError ) {
206- lastError = undefined ;
207-
208- notifier . notify ( {
209- title : 'Build succeeded' ,
210- message : packageJSON . name ,
211- icon : buildConfig . buildSuccessIconPath
212- } ) ;
213- }
214- _finalizeWatch ( ) ;
215- } )
216- . catch ( ( error ) => {
217- if ( buildConfig . showToast ) {
197+ return new Promise < void > ( ( ) => {
198+
199+ function _runWatch ( ) : Promise < void > {
200+ if ( isWatchRunning ) {
201+ shouldRerunWatch = true ;
202+ } else {
203+ isWatchRunning = true ;
204+
205+ return _executeTask ( task , buildConfig )
206+ . then ( ( ) => {
207+ if ( lastError ) {
208+ lastError = undefined ;
209+
210+ if ( buildConfig . showToast ) {
211+ notifier . notify ( {
212+ title : successMessage ,
213+ message : packageJSON . name ,
214+ icon : buildConfig . buildSuccessIconPath
215+ } ) ;
216+ } else {
217+ log ( successMessage ) ;
218+ }
219+ }
220+ return _finalizeWatch ( ) ;
221+ } )
222+ . catch ( ( error ) => {
218223 if ( ! lastError || lastError !== error ) {
219224 lastError = error ;
220- notifier . notify ( {
221- title : 'Build failed' ,
222- message : error ,
223- icon : buildConfig . buildErrorIconPath
224- } ) ;
225+
226+ if ( buildConfig . showToast ) {
227+ notifier . notify ( {
228+ title : failureMessage ,
229+ message : error ,
230+ icon : buildConfig . buildErrorIconPath
231+ } ) ;
232+ } else {
233+ log ( failureMessage ) ;
234+ }
225235 }
226- }
227- _finalizeWatch ( ) ;
228- } ) ;
236+
237+ return _finalizeWatch ( ) ;
238+ } ) ;
239+ }
229240 }
230- }
231241
232- function _finalizeWatch ( ) : void {
233- isWatchRunning = false ;
242+ function _finalizeWatch ( ) : Promise < void > {
243+ isWatchRunning = false ;
234244
235- if ( shouldRerunWatch ) {
236- shouldRerunWatch = false ;
237- _runWatch ( ) ;
245+ if ( shouldRerunWatch ) {
246+ shouldRerunWatch = false ;
247+ return _runWatch ( ) ;
248+ }
249+ return Promise . resolve ( ) ;
238250 }
239- }
240251
241- return Promise . resolve ( ) ;
252+ setWatchMode ( ) ;
253+ buildConfig . gulp . watch ( watchMatch , _runWatch ) ;
254+
255+ _runWatch ( ) ;
256+ } ) ;
242257 }
243258 } ;
244259}
0 commit comments