Skip to content

Commit

Permalink
Closes ColemanGariety#20, closes ColemanGariety#33. Overhauls the "on…
Browse files Browse the repository at this point in the history
… change" functionality and the use of gulp.run().
  • Loading branch information
Jackson Gariety committed Mar 19, 2015
1 parent d75c5b3 commit 7610549
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 28 deletions.
18 changes: 17 additions & 1 deletion Gulpfile.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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'
Expand All @@ -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')
})
67 changes: 44 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -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] })
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion test/server.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('foojs')
console.log('foo')

0 comments on commit 7610549

Please sign in to comment.