Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (75 sloc) 2.35 KB
fs = require 'fs'
path = require 'path'
ws = require 'websocket.io'
version = '1.6'
defaultPort = 35729
defaultExts = [
'html', 'css', 'js', 'png', 'gif', 'jpg',
'php', 'php5', 'py', 'rb', 'erb'
]
defaultExclusions = ['.git/', '.svn/', '.hg/']
class Server
constructor: (@config) ->
@config ?= {}
@config.version ?= version
@config.port ?= defaultPort
@config.exts ?= []
@config.exclusions ?= []
@config.exts = @config.exts.concat defaultExts
@config.exclusions = @config.exclusions.concat defaultExclusions
@config.applyJSLive ?= false
@config.applyCSSLive ?= true
@sockets = []
listen: ->
@debug "LiveReload is waiting for browser to connect."
@server = ws.listen(@config.port)
@server.on 'connection', @onConnection.bind @
@server.on 'close', @onClose.bind @
onConnection: (socket) ->
@debug "Browser connected."
socket.send "!!ver:#{@config.version}"
socket.on 'message', (message) =>
@debug "Browser URL: #{message}"
@sockets.push socket
onClose: (socket) ->
@debug "Browser disconnected."
walkTree: (dirname, callback) ->
exts = @config.exts
exclusions = @config.exclusions
walk = (dirname) ->
fs.readdir dirname, (err, files) ->
if err then return callback err
files.forEach (file) ->
filename = path.join dirname, file
for exclusion in exclusions
return if filename.match exclusion
fs.stat filename, (err, stats) ->
if !err and stats.isDirectory()
walk filename
else
for ext in exts when filename.match "\.#{ext}$"
callback err, filename
break
walk dirname, callback
watch: (dirname) ->
@walkTree dirname, (err, filename) =>
throw err if err
fs.watchFile filename, (curr, prev) =>
if curr.mtime > prev.mtime
@refresh filename
refresh: (path) ->
@debug "Refresh: #{path}"
data = JSON.stringify ['refresh',
path: path,
apply_js_live: @config.applyJSLive,
apply_css_live: @config.applyCSSLive
]
for socket in @sockets
socket.send data
debug: (str) ->
if @config.debug
console.log "#{str}\n"
exports.createServer = (args...) ->
server = new Server args...
server.listen()
server
Something went wrong with that request. Please try again.