@@ -10,12 +10,16 @@ var Cli = module.exports,
1010 Ionitron = require ( './ionic/ionitron' ) ,
1111 optimist = require ( 'optimist' ) ,
1212 path = require ( 'path' ) ,
13+ fs = require ( 'fs' ) ,
1314 settings = require ( '../package.json' ) ,
1415 Tasks = require ( './tasks/cliTasks' ) ,
1516 Utils = IonicAppLib . utils ,
1617 logging = IonicAppLib . logging ,
1718 Q = require ( 'q' ) ,
18- semver = require ( 'semver' ) ;
19+ semver = require ( 'semver' ) ,
20+ gutil = require ( 'gulp-util' ) ,
21+ chalk = require ( 'chalk' ) ,
22+ prettyTime = require ( 'pretty-hrtime' ) ;
1923
2024Cli . Tasks = TASKS = Tasks ;
2125
@@ -130,16 +134,90 @@ Cli.run = function run(processArgv) {
130134 argv . v2 = true ;
131135 }
132136
133- // Only some tasks return promises
134- var promise = Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
137+ var gulpFound = loadGulp ( ) ;
138+ if ( gulpFound ) {
139+ logging . logger . debug ( 'Gulpfile found' ) ;
140+ var gulp = require ( path . resolve ( process . cwd ( ) + '/node_modules/gulp' ) ) ;
141+ logEvents ( gulp ) ;
142+
143+ return Q . nfcall ( gulp . start . bind ( gulp ) , argv . _ [ 0 ] + ':before' ) . then (
144+ function ( ) {
145+ // Only some tasks return promises
146+ return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
147+ }
148+ ) . then ( function ( ) {
149+ return Q . nfcall ( gulp . start . bind ( gulp ) , argv . _ [ 0 ] + ':after' ) ;
150+ } ) ;
151+ } else {
152+ logging . logger . debug ( 'No gulpfile found' ) ;
153+ return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv ) ;
154+ }
135155
136- return promise ;
137156 } catch ( ex ) {
138157 logging . logger . debug ( 'Cli.Run - Error' , ex ) ;
139158 return Utils . fail ( ex ) ;
140159 }
141160} ;
142161
162+ function loadGulp ( ) {
163+ var names = [ 'gulpfile.js' , 'Gulpfile.js' ] ;
164+ var found = false ;
165+ for ( var i = 0 , ii = names . length ; i < ii ; i ++ ) {
166+ try {
167+ require ( path . resolve ( process . cwd ( ) + '/' + names [ i ] ) ) ;
168+ found = true ;
169+ } catch ( e ) {
170+ if ( e instanceof SyntaxError ) {
171+ console . error ( '\nThere is a syntax error in your gulpfile:' ) ;
172+ console . error ( e . message )
173+ process . exit ( 1 ) ;
174+ }
175+ if ( e . code === 'MODULE_NOT_FOUND' && e . message . indexOf ( names [ i ] ) === - 1 ) {
176+ console . log ( '\nLooks like you\'re missing a module in your gulpfile:' ) ;
177+ console . error ( e . message ) ;
178+ console . log ( '\nDo you need to `npm install`?\n' ) ;
179+ process . exit ( 1 ) ;
180+ }
181+ }
182+ }
183+ return found ;
184+ }
185+
186+ function logEvents ( gulpInst ) {
187+ gulpInst . on ( 'task_start' , function ( e ) {
188+ // TODO: batch these
189+ // so when 5 tasks start at once it only logs one time with all 5
190+ gutil . log ( 'Starting' , '\'' + chalk . cyan ( e . task ) + '\'...' ) ;
191+ } ) ;
192+
193+ gulpInst . on ( 'task_stop' , function ( e ) {
194+ var time = prettyTime ( e . hrDuration ) ;
195+ gutil . log (
196+ 'Finished' , '\'' + chalk . cyan ( e . task ) + '\'' ,
197+ 'after' , chalk . magenta ( time )
198+ ) ;
199+ } ) ;
200+
201+ gulpInst . on ( 'task_err' , function ( e ) {
202+ var msg = formatError ( e ) ;
203+ var time = prettyTime ( e . hrDuration ) ;
204+ gutil . log (
205+ '\'' + chalk . cyan ( e . task ) + '\'' ,
206+ chalk . red ( 'errored after' ) ,
207+ chalk . magenta ( time )
208+ ) ;
209+ gutil . log ( msg ) ;
210+ } ) ;
211+
212+ gulpInst . on ( 'task_not_found' , function ( err ) {
213+ gutil . log (
214+ chalk . red ( 'Task \'' + err . task + '\' is not in your gulpfile' )
215+ ) ;
216+ gutil . log ( 'Please check the documentation for proper gulpfile formatting' ) ;
217+ process . exit ( 1 ) ;
218+ } ) ;
219+ }
220+
143221Cli . getBooleanOptionsForTask = function getBooleanOptionsForTask ( task ) {
144222 var availableTaskOptions = task . options ;
145223 var booleanOptions = [ ] ;
0 commit comments