/
wachs-cli.coffee
94 lines (72 loc) · 2.49 KB
/
wachs-cli.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
89
90
91
92
93
94
path = require 'path'
{spawn} = require 'child_process'
{parseArgs,log,matchesGlobs,localTime,exit,npmVersion} = require './support'
watch = require './wach'
@run = (rawArgs) ->
args = parseArgs rawArgs
exit 0, usage if args.help?
exit 0, npmVersion() if args.version?
exit 1, usage unless args.command?
log.info "Will kill and restart: #{args.command}"
if args.only.length is 0
log.info "when any files added or updated"
else
log.info "when files matching {#{args.only.join(',')}} added or updated"
if args.except.length isnt 0
log.info "except those matching {#{args.except.join(',')}}"
child = null
shouldRestart = no
diedOnItsOwn = no
runCommand = ->
log.info "starting ..."
child = spawn 'sh', ['-c', "exec #{args.command}"]
child.stdout.pipe process.stdout
child.stderr.pipe process.stderr
child.on 'exit', (code) ->
if shouldRestart
log.info "killed"
runCommand()
shouldRestart = no
else
log.info "process exited by itself, crash?"
diedOnItsOwn = yes
runCommand()
cwd = process.cwd()
watch cwd, (changedPath) ->
changedPath = path.relative cwd, changedPath
# do nothing for deletes
return unless path.existsSync changedPath
# do nothing for ignored paths
return if (args.only.length isnt 0) and (not matchesGlobs changedPath, args.only)
return if (args.except.length isnt 0) and ( matchesGlobs changedPath, args.except)
shouldRestart = yes
log.info ""
log.info "changed: #{changedPath} (#{localTime()})"
log.info "killin ..."
child.kill()
if diedOnItsOwn
runCommand()
diedOnItsOwn = no
usage = """
The "server" version of `wach`. Pass it a command to start a long running
process (such as a web server) and it will run that process then monitor
the current directory for file modifications. When a file changes it will
automatically restart the process.
Usage:
wachs [options] <command>
Required:
<command>
The command to restart every time an update occurs in the directory being monitored.
Options:
-o|--only <globs>
Only run <command> when the path that changed matches <globs>.
-e|--except <globs>
Only run <command> when the path that changed doesn't match <globs>.
Quote the <globs> ("*.c") or add a trailing comma (*.c,) to prevent your shell from
automatically expanding them.
Examples:
wachs node server.js
wachs coffee server.coffee
wachs ruby sinatra-app.rb
wachs -o server/*.js, node server.js
"""