/
main.coffee
92 lines (61 loc) · 2.19 KB
/
main.coffee
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
path = require "path"
fs = require "fs"
{exec} = require "child_process"
watch = require "watch"
gex = require "gex"
findit = require "findit"
iniparser = require "iniparser"
class Watcher
constructor: (@name, @cwd, @settings) ->
@settings.glob ||= "*"
@settings.watchdir ||= "."
if @settings.watchdir.substr(0,1) isnt "/"
@settings.watchdir = path.join(@cwd, @settings.watchdir)
@rerun = false
@running = false
start: ->
watch.createMonitor @settings.watchdir, (monitor) =>
console.log "Found watch '#{ @name }' from directory #{ @settings.watchdir }"
monitor.on "created", (file) => @onModified(file)
monitor.on "changed", (file) => @onModified(file)
onModified: (filepath) ->
for match in @settings.glob.split(" ")
if gex(match).on path.basename filepath
ok = true
break
return unless ok
# Oh we are already running. Just request restart for this change.
if @running
@rerun = true
else
@runCMD()
console.log "Change on '#{ filepath }' running '#{ @name }'!"
runCMD: ->
@running = true
cmd = exec @settings.cmd, cwd: @cwd, (err) =>
@running = false
if err
console.log "Error in '#{ @name }'"
if @rerun
# There has been a change(s) during this run. Let's rerun it.
console.log "Rerunning '#{ @name }'"
@rerun = false
@runCMD()
else
console.log "\nRan", @settings.name, "successfully!", (new Date) + 2*60*60
cmd.stdout.on "data", (data) -> process.stdout.write data
cmd.stderr.on "data", (data) -> process.stderr.write data
exports.run = ->
console.log "Searching projectwatch.cfg files from #{ searchDir }\n"
dirs = process.argv.splice(2)
dirs.push process.cwd() unless dirs.length
for searchDir in dirs
finder = findit.find(searchDir)
finder.on "file", (filepath) ->
if path.basename(filepath) is "projectwatch.cfg"
iniparser.parse filepath, (err, settingsObs) ->
throw err if err
for name, settings of settingsObs
watcher = new Watcher name, path.dirname(filepath), settings
watcher.start()
watcher.runCMD()