diff --git a/Gulpfile.js b/Gulpfile.js index 73d2c36..2f7bd2e 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -1,6 +1,7 @@ var gulp = require('gulp') , jshint = require('gulp-jshint') , nodemon = require('./index') + , path = require('path') // gulp.task('test', function () { // gulp.src('./test/*-test.js') @@ -13,6 +14,12 @@ gulp.task('lint', function () { .pipe(jshint()) }) +gulp.task('cssmin', function () { /* void */ }) + +gulp.task('afterstart', function () { + console.log('proc has finished restarting!') +}) + gulp.task('test', ['lint'], function () { nodemon({ script: './test/server.js' @@ -22,7 +29,16 @@ gulp.task('test', ['lint'], function () { } , watch: './' , ext: 'js coffee' + , tasks: ['lint'] + // , tasks: function (files) { + // var tasks = [] + // files.forEach(function (file) { + // if (path.extname(file) === '.js' && !~tasks.indexOf('lint')) tasks.push('lint') + // if (path.extname(file) === '.css' && !~tasks.indexOf('cssmin')) tasks.push('cssmin') + // }) + // return tasks + // } // , nodeArgs: ['--debug'] }) - .on('restart', 'lint') + .on('restart', 'cssmin') }) diff --git a/index.js b/index.js index ad97f89..3426bbf 100644 --- a/index.js +++ b/index.js @@ -1,55 +1,76 @@ -"use strict" +'use strict' var nodemon = require('nodemon') , colors = require('colors') , gulp = require('gulp') + , cp = require('child_process') module.exports = function (options) { options = options || {}; if (options.exec instanceof Array) options.exec = options.exec.join(' ') if (typeof options.exec === 'string') options.exec = 'gulp ' + options.exec + if (typeof options.tasks === 'function') options.verbose = true // Enable verbose mode if file change list is needed // Our script var script = nodemon(options) - , originalOn = script.on // http://www.youtube.com/watch?v=dKKdJoXF7PI&feature=kp + , originalOn = script.on - script.on('log', function (log) { - if (log.message.indexOf('change') > -1) nodemon.emit('change'); - }) + // Allow for injection of tasks on file change + if (options.verbose) { + script.on('log', function (log) { + if (~log.message.indexOf('files triggering change check')) { + if (typeof options.tasks === 'function') options.tasks = options.tasks(log.message.split('files triggering change check: ').pop().split(' ')) + run(options.tasks) + } + }) + } else { + script.on('log', function (log) { + if (~log.message.indexOf('restarting due to changes...')) { + run(options.tasks) + } + }) + } + // Capture ^C process.on('exit', script.emit.bind(script, 'exit')) - // Forward log messages + // Forward log messages and stdin script.on('log', function (log) { - console.log('[gulp] ' + ('[nodemon] ' + log.message).yellow) + console.log('[' + new Date().toString().split(' ')[4].gray + '] ' + ('[nodemon] ' + log.message).yellow) }) - + // Shim 'on' for use with gulp tasks script.on = function (event, tasks) { var tasks = Array.prototype.slice.call(arguments) , event = tasks.shift() - for (var i = 0; i < tasks.length; i++) { - void function (tasks) { - if (tasks instanceof Function) originalOn(event, tasks) - else { - originalOn(event, function () { - if (Array.isArray(tasks)) { - tasks.forEach(function (task) { - run(task) - }) - } else run(tasks) - }) - } - }(tasks[i]) + if (event === 'change') script.changeTasks = tasks + else { + for (var i = 0; i < tasks.length; i++) { + void function (tasks) { + if (tasks instanceof Function) originalOn(event, tasks) + else { + originalOn(event, function () { + if (Array.isArray(tasks)) { + tasks.forEach(function (task) { + run(task) + }) + } else run(tasks) + }) + } + }(tasks[i]) + } } + return script } return script + // Synchronous alternative to gulp.run() function run(tasks) { - tasks = tasks.length ? [tasks] : ['default'] - gulp.start.apply(gulp, tasks) + if (typeof tasks === 'string') tasks = [tasks] + if (!(tasks instanceof Array)) throw new Error('Expected task name or array but found: ' + tasks) + cp.spawnSync('gulp', tasks, { stdio: [0, 1, 2] }) } } diff --git a/package.json b/package.json index 66e87e5..bd81b3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gulp-nodemon", - "version": "1.0.5", + "version": "2.0.0", "description": "A nodemon wrapper for Gulp that keeps your \"watch, edit, restart\" routine contained to one process.", "main": "index.js", "scripts": { @@ -27,8 +27,8 @@ }, "homepage": "https://github.com/JacksonGariety/gulp-nodemon", "dependencies": { - "gulp": "^3.8.10", - "nodemon": "^1.3.1", + "gulp": "^3.8.11", + "nodemon": "^1.3.7", "event-stream": "^3.2.1", "colors": "^1.0.3" }, diff --git a/test/server.js b/test/server.js index 9280936..a5b9709 100644 --- a/test/server.js +++ b/test/server.js @@ -1 +1 @@ -console.log('foojs') +console.log('foo')