Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b2329f0e47
Fetching contributors…

Cannot retrieve contributors at this time

95 lines (72 sloc) 2.551 kb
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
exit 0, npmVersion() if args.version?
exit 1, usage unless args.command? "Will kill and restart: #{args.command}"
if args.only.length is 0 "when any files added or updated"
else "when files matching {#{args.only.join(',')}} added or updated"
if args.except.length isnt 0 "except those matching {#{args.except.join(',')}}"
child = null
shouldRestart = no
diedOnItsOwn = no
runCommand = -> "starting ..."
child = spawn 'sh', ['-c', "exec #{args.command}"]
child.stdout.pipe process.stdout
child.stderr.pipe process.stderr
child.on 'exit', (code) ->
if shouldRestart "killed"
shouldRestart = no
else "process exited by itself, crash?"
diedOnItsOwn = yes
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 "" "changed: #{changedPath} (#{localTime()})" "killin ..."
if diedOnItsOwn
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.
wachs [options] <command>
The command to restart every time an update occurs in the directory being monitored.
-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.
wachs node server.js
wachs coffee
wachs ruby sinatra-app.rb
wachs -o server/*.js, node server.js
Jump to Line
Something went wrong with that request. Please try again.