@@ -12,6 +12,7 @@ var Emitter = require('events').EventEmitter,
1212 watcher = require ( '../lib/watcher' ) ,
1313 stdout = require ( 'stdout-stream' ) ,
1414 stdin = require ( 'get-stdin' ) ,
15+ chalk = require ( 'chalk' ) ,
1516 fs = require ( 'fs' ) ;
1617
1718/**
@@ -271,7 +272,11 @@ function watch(options, emitter) {
271272 var handler = function ( files ) {
272273 files . forEach ( function ( file ) {
273274 if ( path . basename ( file ) [ 0 ] !== '_' ) {
274- renderFile ( file , options , emitter ) ;
275+ renderFile ( file , options , emitter , function ( err ) {
276+ if ( err ) {
277+ emitter . emit ( 'error' , chalk . red ( err ) ) ;
278+ }
279+ } ) ;
275280 }
276281 } ) ;
277282 } ;
@@ -344,12 +349,32 @@ function run(options, emitter) {
344349 }
345350 }
346351
347- if ( options . watch ) {
348- watch ( options , emitter ) ;
349- } else if ( options . directory ) {
350- renderDir ( options , emitter ) ;
352+ if ( options . directory ) {
353+ renderDir ( options , emitter , function ( err ) {
354+ if ( err ) {
355+ emitter . emit ( 'error' , chalk . red ( err ) ) ;
356+ return process . exit ( 1 ) ;
357+ }
358+
359+ if ( options . watch ) {
360+ watch ( options , emitter ) ;
361+ } else {
362+ process . exit ( ) ;
363+ }
364+ } ) ;
351365 } else {
352- render ( options , emitter ) ;
366+ render ( options , emitter , function ( err ) {
367+ if ( err ) {
368+ emitter . emit ( 'error' , chalk . red ( err ) ) ;
369+ return process . exit ( 1 ) ;
370+ }
371+
372+ if ( options . watch ) {
373+ watch ( options , emitter ) ;
374+ } else {
375+ process . exit ( ) ;
376+ }
377+ } ) ;
353378 }
354379}
355380
@@ -361,12 +386,12 @@ function run(options, emitter) {
361386 * @param {Object } emitter
362387 * @api private
363388 */
364- function renderFile ( file , options , emitter ) {
389+ function renderFile ( file , options , emitter , done ) {
365390 options = getOptions ( [ path . resolve ( file ) ] , options ) ;
366391 if ( options . watch && ! options . quiet ) {
367392 emitter . emit ( 'info' , util . format ( '=> changed: %s' , file ) ) ;
368393 }
369- render ( options , emitter ) ;
394+ render ( options , emitter , done ) ;
370395}
371396
372397/**
@@ -376,7 +401,7 @@ function renderFile(file, options, emitter) {
376401 * @param {Object } emitter
377402 * @api private
378403 */
379- function renderDir ( options , emitter ) {
404+ function renderDir ( options , emitter , done ) {
380405 var globPath = path . resolve ( options . directory , globPattern ( options ) ) ;
381406 glob ( globPath , { ignore : '**/_*' , follow : options . follow } , function ( err , files ) {
382407 if ( err ) {
@@ -386,14 +411,17 @@ function renderDir(options, emitter) {
386411 }
387412
388413 forEach ( files , function ( subject ) {
389- emitter . once ( 'done' , this . async ( ) ) ;
390- renderFile ( subject , options , emitter ) ;
414+ renderFile ( subject , options , emitter , this . async ( ) ) ;
391415 } , function ( successful , arr ) {
392416 var outputDir = path . join ( process . cwd ( ) , options . output ) ;
393417 if ( ! options . quiet ) {
394418 emitter . emit ( 'info' , util . format ( 'Wrote %s CSS files to %s' , arr . length , outputDir ) ) ;
395419 }
396- process . exit ( ) ;
420+ if ( successful ) {
421+ done ( ) ;
422+ } else {
423+ done ( new Error ( 'Some files failed to compile' ) ) ;
424+ }
397425 } ) ;
398426 } ) ;
399427}
0 commit comments