forked from ColemanGariety/gulp-nodemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (91 loc) · 3.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict'
var nodemon = require('nodemon')
, colors = require('colors')
, gulp = require('gulp')
, cp = require('child_process')
, bus = require('nodemon/lib/utils/bus')
module.exports = function (options){
options = options || {};
// Our script
var script = nodemon(options)
, originalOn = script.on
, originalEmit = script.emit
, originalListeners = bus.listeners('restart')
// Allow for injection of tasks on file change
if (options.tasks) {
// Remove all 'restart' listeners
bus.removeAllListeners('restart')
// Place our listener in first position
bus.on('restart', function (files){
if (!options.quiet) nodemonLog('running tasks...')
if (typeof options.tasks === 'function') run(options.tasks(files))
else run(options.tasks)
})
// Re-add all other listeners
for (var i = 0; i < originalListeners.length; i++) {
bus.on('restart', originalListeners[i])
}
}
// Capture ^C
var exitHandler = function (options){
if (options.exit) script.emit('exit')
if (options.quit) process.exit(0)
}
process.once('exit', exitHandler.bind(null, { exit: true }))
process.once('SIGINT', exitHandler.bind(null, { quit: true }))
// Forward log messages and stdin
script.on('log', function (log){
nodemonLog(log.message)
})
// Shim 'on' for use with gulp tasks
script.on = function (event, tasks){
var tasks = Array.prototype.slice.call(arguments)
, event = tasks.shift()
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
}
// Shim 'emit' for use with gulp
script.emit = function (event, timeoutInSecs){
if (typeof event !== 'string')
throw new Error('Event must be a string!')
if (!timeoutInSecs) {
console.info('restarting')
originalEmit(event)
}
else if (typeof timeoutInSecs !== 'number')
throw new Error('Timeout must be a number!')
else {
console.info('restarting in ' + timeoutInSecs + 'secs')
setTimeout(function (){
originalEmit(event)
}, timeoutInSecs * 1000)
}
}
return script
// Synchronous alternative to gulp.run()
function run(tasks){
if (typeof tasks === 'string') tasks = [tasks]
if (tasks.length === 0) return
if (!(tasks instanceof Array)) throw new Error('Expected task name or array but found: ' + tasks)
cp.spawnSync(process.platform === 'win32' ? 'gulp.cmd' : 'gulp', tasks, { stdio: [0, 1, 2] })
}
}
function nodemonLog(message){
console.log('[' + new Date().toString().split(' ')[4].gray + '] ' + ('[nodemon] ' + message).yellow)
}