/
index.coffee
134 lines (106 loc) · 3.89 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"use strict"
path = require 'path'
fs = require 'fs'
config = require './config'
clientLibOutPath = null
clientLibText = null
sockets = {}
directoryWatchSetup = false
logger = null
registration = (mimosaConfig, register) ->
logger = mimosaConfig.log
register ['preClean'], 'init', _removeClientLibrary
clientLibOutPath = path.join mimosaConfig.watch.compiledJavascriptDir, 'reload-client.js'
clientLibText = fs.readFileSync path.join(__dirname, 'assets', 'reload-client.js'), 'ascii'
if mimosaConfig.isServer
unless mimosaConfig.liveReload.enabled
return logger.debug "Live-Reload is turned off, not registering reload functionality."
register ['postBuild'], 'afterServer', _writeClientLibrary
register ['postBuild'], 'afterServer', connect
register ['add','update','remove'], 'afterWrite', _writeClientLibrary
register ['add','update','remove'], 'afterWrite', _refreshPage
disconnect = ->
socket.disconnect() for socketId, socket of sockets
connect = (mimosaConfig, options, next) ->
unless options.userServer?
return logger.warn "Live-Reload module is configured, but is unable to find your server. Did you forget to return it from your startServer function? Disabling Live-Reload."
io = if options.socketio?
logger.debug "Using user's socketio"
options.socketio
else
logger.debug "Using module's socketio"
socketio = require 'socket.io'
io = socketio.listen(options.userServer)
# Not available with 1.0 socket.io
# io.enable 'browser client minification'
# io.enable 'browser client etag'
# io.set 'log level', 1
io
io.sockets.on 'connection', (socket) ->
socket.on 'disconnect', ->
delete sockets[socket.id] if sockets[socket.id]
sockets[socket.id] = socket
_setupDirectoryWatch(mimosaConfig.liveReload.additionalDirs) unless directoryWatchSetup
next()
_setupDirectoryWatch = (dirsToWatch) ->
if dirsToWatch? and dirsToWatch.length? > 0
watch = require 'chokidar'
directoryWatchSetup = true
watcher = watch.watch dirsToWatch, {persistent: true}
watcher.on 'all', (event, filePath) ->
ext = path.extname(filePath)
if(ext is '.css')
_emit 'css'
else
_emit 'page'
watcher.on 'error', (error) ->
# Doing nothing at the moment, just need to trap error event
# console.log("ERROR: ", error)
_writeClientLibrary = (mimosaConfig, options, next) ->
if options.userServer?
fs.exists clientLibOutPath, (exists) ->
if exists
next()
else
_makeDirectory path.dirname(clientLibOutPath)
if logger.isDebug()
logger.debug "Writing live reload client library to [[ #{clientLibOutPath} ]]"
fs.writeFile clientLibOutPath, clientLibText, 'ascii', (err) ->
if err
logger.error err
next()
else
next()
_removeClientLibrary = (mimosaConfig, options, next) ->
fs.exists clientLibOutPath, (exists) ->
if exists
fs.unlink clientLibOutPath, (err) ->
if err
logger.error "Error occured removing client library: #{err}"
else
logger.info "mimosa-live-reload: removed live reload client."
next()
else
next()
_makeDirectory = (dir) ->
unless fs.existsSync(dir)
if logger.isDebug()
logger.debug("Making folder [[ " + dir + " ]]")
wrench = require 'wrench'
wrench.mkdirSyncRecursive(dir, 0o0777)
_refreshPage = (mimosaConfig, options, next) ->
type = if options.isCSS then "css" else "page"
_emit(type)
next()
_emit = (type) ->
if logger.isDebug()
logger.debug "Sending message to client to refresh page for type [[ #{type} ]]"
Object.keys(sockets).forEach (s) ->
sockets[s].emit type
module.exports =
registration: registration
defaults: config.defaults
placeholder: config.placeholder
validate: config.validate
connect: connect
disconnect: disconnect