forked from ColemanGariety/gulp-nodemon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
… change" functionality and the use of gulp.run().
- Loading branch information
Jackson Gariety
committed
Mar 19, 2015
1 parent
d75c5b3
commit 7610549
Showing
4 changed files
with
65 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
console.log('foojs') | ||
console.log('foo') |