-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
189 lines (138 loc) · 4.39 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var spawn = require('child_process').spawn
, EE = require('events').EventEmitter
, path = require('path')
, fs = require('fs')
var debounce = require('just-debounce')
, watcher = require('node-watch')
, subdirs = require('subdirs')
module.exports = createJung
function Jung(options, command) {
EE.call(this)
command = command || []
this.blocked = false
this.watcher = null
this.timeout = null
this.child = null
this.queue = []
this.options = options || {}
this.options.notdirs = this.options.notdirs || []
this.options.notdirs.push(/\.git/)
this.command = Array.isArray(command) ? command : command.split(' ')
if(!this.options.wait) this.options.wait = 300
if(!this.options.root) this.options.root = process.cwd()
if(!this.options.timeout) this.options.timeout = 5000
return this
}
Jung.prototype = Object.create(EE.prototype)
Jung.prototype.execute = function Jung$execute(triggerFile) {
var self = this
self.emit('triggered')
if(!self.blocked) return execute()
if(self.options.kill) {
self.queue = [triggerFile]
if(!self.child) return self.blocked = false
self.emit('killing')
self.timeout = setTimeout(forceKill, self.options.timeout)
return self.child.kill()
}
self.emit('queueing', triggerFile)
return self.queue.push(triggerFile)
function execute() {
var filename = path.basename(triggerFile)
, extension = path.extname(filename)
, dirname = path.dirname(triggerFile)
, barename = filename.slice(0, -extension.length)
self.blocked = true
var env = process.env
, command = self.command.map(replaceEnv)
, options = {env: env, cwd: process.cwd()}
if(!self.options.quiet) {
options.stdio = 'inherit'
}
env.JUNG_FILE = triggerFile
env.JUNG_FILENAME = filename
env.JUNG_EXTENSION = extension
env.JUNG_DIR = dirname
env.JUNG_BARENAME = barename
self.emit('running', command.join(' '))
self.child = spawn(command[0], command.slice(1), options)
self.child.on('exit', finishChild)
function replaceEnv(str) {
return str.replace(/\$JUNG_FILENAME/g, filename)
.replace(/\$JUNG_FILE/g, triggerFile)
.replace(/\$JUNG_EXTENSION/g, extension)
.replace(/\$JUNG_DIR/g, dirname)
.replace(/\$JUNG_BARENAME/g, barename)
}
function finishChild(code) {
self.emit('ran', command.join(' '), code)
self.blocked = false
if(self.timeout) self.timeout = clearTimeout(self.timeout)
if(self.queue.length) self.execute(self.queue.shift())
}
}
function forceKill() {
self.child && self.child.kill('SIGKILL')
}
}
Jung.prototype.start = function Jung$start() {
var self = this
if(!fs.existsSync(self.options.root)) {
self.emit(
'error'
, new Error('!! Root dir `' + self.options.root + '` does not exist !!')
)
return process.exit(1)
}
subdirs(self.options.root, startJung)
function startJung(err, dirs) {
if(err) {
throw err
}
dirs = dirs.filter(function filterDirs(path) {
return fileFilter(false, path)
})
self.watcher = watcher(
dirs.concat(self.options.root)
, {recursive: false}
, debounce(filterEvent, self.options.wait)
)
self.emit('started')
if(self.options.run) self.execute('')
}
function filterEvent(name) {
if(fileFilter(true, name)) self.execute(name)
}
function fileFilter(isFile, name) {
var opts = self.options
var notArray = isFile ? opts.notfiles : opts.notdirs
, goodArray = isFile ? opts.files : opts.dirs
, goodFiles = isFile ? opts.names : []
notArray = (notArray || []).map(regex)
goodArray = (goodArray || []).map(regex)
if(goodFiles.indexOf(name) > -1) return true
for(var i = 0, l = goodArray.length; i < l; ++i) {
if(goodArray[i].test(name)) return true
}
for(i = 0, l = notArray.length; i < l; ++i) {
if(notArray[i].test(name)) return false
}
return !(goodArray.length + goodFiles.length)
}
function regex(str) {
if(str instanceof RegExp) return str
return new RegExp(str)
}
}
Jung.prototype.stop = function Jung$stop() {
var self = this
if(!self.blocked) return stop()
self.child.on('exit', stop)
self.child.kill()
function stop() {
self.watcher && self.watcher.close()
}
}
function createJung(options, command) {
return new Jung(options, command)
}