Skip to content

Commit

Permalink
Closes ColemanGariety#19 and closes ColemanGariety#17. Also adds a us…
Browse files Browse the repository at this point in the history
…eful change event so you can

run your gulp compilation tasks without spawning a new instance using
nodemon's `exec` options.
  • Loading branch information
Jackson Gariety committed Mar 9, 2014
1 parent a2e3e4e commit 6e3f9ff
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ gulp-nodemon returns a stream just like any other NodeJS stream, **except for th

#### **`.on([event], [tasks])`**

1. `[event]` is an event name as a string. (see: [nodemon events](https://github.com/remy/nodemon/blob/master/doc/events.md)
1. `[event]` is an event name as a string. See [nodemon events](https://github.com/remy/nodemon/blob/master/doc/events.md). *I've also added an `.on('change')` event which fires **before** the server restarts so that you can run your compile tasks all within the same gulp process.*
2. `[tasks]` A gulp task name, array of gulp task names, or a function to execute.

## Example

The following example will run your code with nodemon and lint it when your make changes.
The following example will run your code with nodemon, lint it when your it change, then log a message when it nodemon runs it again.

```javascript
// Gulpfile.js
Expand All @@ -42,6 +42,9 @@ gulp.task('lint', function () {

gulp.task('develop', function () {
nodemon({ script: 'server.js', ext: 'html js', ignore: ['ignored.js'] })
.on('restart', ['lint'])
.on('change', ['lint'])
.on('restart', function () {
console.log('restarted!')
})
})
```
67 changes: 43 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,51 @@ var nodemon = require('nodemon')
, gulp = require('gulp')

module.exports = function (options) {
try {

// Our script
var script = nodemon(options)
, originalDon = script.on

process.on('exit', script.emit.bind(script, 'exit'))

// Forward log messages
script.on('log', function (log) {
console.log('[gulp] ' + ('[nodemon] ' + log.message).yellow)
})

// Shim 'on' for use with gulp tasks
script.on = function (event, tasks) {
if (tasks instanceof Function) originalDon(event, tasks)
else originalDon(event, function () {
if (Array.isArray(tasks)) {
tasks.forEach(function (task) {
gulp.run(task)
if (options.exec instanceof Array) options.exec = options.exec.join(' ')
if (typeof options.exec === 'string') options.exec = 'gulp ' + options.exec

// Our script
var script = nodemon(options)
, originalOn = script.on // http://www.youtube.com/watch?v=dKKdJoXF7PI&feature=kp

script.on('log', function (log) {
if (log.message === 'restarting due to changes...') nodemon.emit('change')
})

process.on('exit', script.emit.bind(script, 'exit'))

// Forward log messages
script.on('log', function (log) {
console.log('[gulp] ' + ('[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 () {
console.log(tasks)
if (Array.isArray(tasks)) {
tasks.forEach(function (task) {
run(task)
})
} else run(tasks)
})
} else gulp.run(tasks)
})
}
}(tasks[i])
}

return script
}

return script

} catch (e) { throw '[gulp] ' + ('[nodemon]' + String(e.message)).yellow }
function run(tasks) {
tasks = tasks.length ? [tasks] : ['default']
gulp.start.apply(gulp, tasks)
}
}

0 comments on commit 6e3f9ff

Please sign in to comment.