@@ -160,6 +160,9 @@ Cli.run = function run(processArgv) {
160160 return Q . fcall ( task . run . bind ( task ) , Cli , argv , rawCliArguments ) ;
161161 }
162162
163+ // Check if there are npm scripts in the package.json
164+ var npmScripts = Cli . loadNpmScripts ( ) ;
165+
163166 try {
164167 var gulpLoaded = Cli . loadGulpfile ( ) ;
165168 } catch ( e ) {
@@ -174,20 +177,28 @@ Cli.run = function run(processArgv) {
174177 process . exit ( 1 ) ;
175178 }
176179
177- if ( ! gulpLoaded ) {
180+ log . debug ( '\nNpm scripts:' , npmScripts ) ;
181+ log . debug ( 'Gulpfile found:' , gulpLoaded , '\n' ) ;
182+
183+ if ( npmScripts ) {
184+ return Cli . runWithNpmScripts ( argv , task , rawCliArguments ) ;
185+ } else if ( gulpLoaded ) {
186+ return Cli . runWithGulp ( argv , task , rawCliArguments ) ;
187+ }
178188
179- // warn if no gulpfile and it's a command that requires a build step
189+ if ( ! gulpLoaded ) {
190+ // warn if no build file and it's a command that requires a build step
180191 // but still run the command with no hooks
181- if ( Cli . isBuildCommand ( taskName ) && argv . v2 ) {
182- log . warn ( 'WARN: No gulpfile found!' ) ;
192+ if ( Cli . isBuildCommand ( taskName ) && argv . v2 && ! npmScripts ) {
193+ log . warn ( 'WARN: No build file found!' ) ;
183194 log . warn ( 'If your app requires a build step, you may want to ensure it runs before ' + taskName + '.\n' ) ;
184195 }
185- log . debug ( 'No gulpfile found, not running gulp hooks' ) ;
186-
187- return Q . fcall ( task . run . bind ( task ) , Cli , argv , rawCliArguments ) ;
196+ if ( ! npmScripts ) {
197+ log . debug ( 'No gulpfile found, not running gulp hooks' ) ;
198+ }
188199 }
189200
190- return Cli . runWithGulp ( argv , task , rawCliArguments ) ;
201+ return Q . fcall ( task . run . bind ( task ) , Cli , argv , rawCliArguments ) ;
191202
192203 } catch ( ex ) {
193204 return appLibUtils . fail ( ex ) ;
@@ -272,6 +283,80 @@ Cli.loadGulpfile = function loadGulpfile() {
272283 return false ;
273284} ;
274285
286+ Cli . runWithNpmScripts = function runWithNpmScripts ( argv , taskInstance , rawCliArguments ) {
287+ var cmdName = argv . _ [ 0 ] ;
288+ var beforeHook = cmdName + ':before' ;
289+ var afterHook = cmdName + ':after' ;
290+
291+ var packageFile = require ( path . resolve ( process . cwd ( ) + '/package.json' ) ) ;
292+ var scripts = packageFile . scripts ;
293+
294+ var beforeHookPromise = Q ( true ) ;
295+ if ( scripts [ beforeHook ] ) {
296+ log . info ( '\nRunning \'' + beforeHook + '\' npm script before ' + cmdName ) ;
297+ beforeHookPromise = Cli . runNpmHook ( scripts [ beforeHook ] ) ;
298+ }
299+
300+ // run beforeHook
301+ return beforeHookPromise
302+
303+ // run cmd
304+ . then ( function ( ) {
305+ return Q . fcall ( taskInstance . run . bind ( taskInstance ) , Cli , argv , rawCliArguments ) ;
306+ } )
307+
308+ // run afterHook
309+ . then ( function ( ) {
310+ if ( scripts [ afterHook ] ) {
311+ log . info ( '\nRunning \'' + afterHook + '\' npm script before ' + cmdName ) ;
312+ return Cli . runNpmHook ( scripts [ afterHook ] ) ;
313+ }
314+ } ) ;
315+ }
316+
317+ Cli . runNpmHook = function runNpmHook ( hook ) {
318+ var cmd = 'npm' ;
319+ var args = [ 'run' , hook ] ;
320+ var command = cmd + ' ' + args ;
321+
322+ var q = Q . defer ( ) ;
323+ var spawn = require ( 'cross-spawn-async' ) ;
324+
325+ var spawned = spawn ( 'npm' , args ) ;
326+ spawned . on ( 'error' , function ( err ) {
327+ log . error ( 'Unable to run spawn command ' + err ) ;
328+ } ) ;
329+ spawned . stdout . on ( 'data' , function ( data ) {
330+ log . info ( data . toString ( ) ) ;
331+ } ) ;
332+ spawned . stderr . on ( 'data' , function ( data ) {
333+ log . info ( data . toString ( ) ) ;
334+ } ) ;
335+ spawned . on ( 'exit' , function ( code ) {
336+ log . debug ( 'Spawn command' , command , 'completed' ) ;
337+ if ( code !== 0 ) {
338+ return q . reject ( 'There was an error with the spawned command: ' + command ) ;
339+ }
340+ return q . resolve ( ) ;
341+ } ) ;
342+
343+ return q . promise ;
344+ }
345+
346+ Cli . loadNpmScripts = function loadNpmScripts ( ) {
347+ var fileName = 'package.json' ;
348+
349+ try {
350+ var packageFile = require ( path . resolve ( process . cwd ( ) + '/' + fileName ) ) ;
351+ log . verbose ( 'Package.json found scripts:' , packageFile . scripts ) ;
352+ return packageFile . scripts ;
353+ } catch ( e ) {
354+ throw e ;
355+ }
356+
357+ return undefined ;
358+ } ;
359+
275360Cli . logEvents = function logEvents ( gulpInst , finalTaskNames ) {
276361 var prettyTime = require ( 'pretty-hrtime' ) ;
277362 var gutil = require ( 'gulp-util' ) ;
0 commit comments