Skip to content

Instantly share code, notes, and snippets.

@alxhill
Created November 26, 2012 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alxhill/4148831 to your computer and use it in GitHub Desktop.
Save alxhill/4148831 to your computer and use it in GitHub Desktop.
Cakefile that creates a self rebooting server on file change
fs = require 'fs'
{print} = require 'sys'
{spawn, fork} = require 'child_process'
processes = {}
process.on "exit", ->
for k, p of processes
p.kill()
return
addFuncs = (child) ->
child.stderr.on 'data', (data) -> process.stderr.write data.toString()
child.stdout.on 'data', (data) -> print data.toString()
build = ->
processes.build_coffee = spawn 'coffee', ['-c', '-o', 'app/scripts/main.coffee', 'server.coffee']
addFuncs(processes.build_coffee)
watch = ->
processes.watch_coffee = spawn 'coffee', ['-w', '-c', 'app/scripts/main.coffee', 'server.coffee']
addFuncs(processes.watch_coffee)
server = ->
processes.node = fork 'server'
dev = ->
server()
fs.watch "server.js", (event, filename) ->
console.log "Restarting server"
processes.node.kill()
processes.node = fork 'server'
return
task 'watch', 'watch and update server.coffee and main.coffee', ->
watch()
task 'build', 'Build server.coffee and main.coffee', ->
build()
task 'server', 'Run the server', ->
server()
task 'dev', 'watch files and run server', ->
invoke 'watch'
dev()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment