-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cakefile
192 lines (152 loc) · 5.77 KB
/
Cakefile
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{exec, spawn} = require 'child_process'
fs = require 'fs'
watch = require 'nodewatch'
crypto = require 'crypto'
paths = require 'path'
appWatcher = watch
nodeInstance = false
testing = false
projectDir = "#{__dirname}"
assets = "#{ projectDir }/assets"
configFile = "#{ projectDir }/config.json"
jsDir = "#{ projectDir }/public/javascripts"
coffeeFiles = "#{ projectDir }/assets/coffee"
lessFiles = "#{ projectDir }/assets/less"
lessIncPaths = ["base/"]
cssDir = "#{ projectDir }/public/stylesheets"
config = require configFile
generateHash = ->
now = new Date().getTime().toString()
crypto.createHash('md5').update(now).digest "hex"
updateConfigFile = (json) ->
config.css = json.css if json.css
config.js = json.js if json.js
console.log "-- --- --- updating config file"
fs.writeFile configFile, JSON.stringify(config, null, 4), (err) ->
if(err)
console.log err
else
console.log("config file updated")
restartServerOrCreate = ->
if nodeInstance is false
console.log "starting app server"
nodeInstance = spawn("node", ["#{projectDir}/app.js"])
bindEvents nodeInstance
else
restartServer()
restartServer = ->
if nodeInstance isnt false
console.log "restarting app server"
nodeInstance.kill()
console.log "-- stopped app server"
nodeInstance = spawn("node", ["#{projectDir}/app.js"])
console.log "-- -- started app server"
nodeInstance = bindEvents nodeInstance
bindEvents = (node) ->
node.stdout.on 'data', (data) ->
console.log "--node --out : #{data}"
node.stderr.on 'data', (data) ->
console.log "--node --err : #{data}"
node.on 'exit', (code) ->
if (code isnt 0)
console.log "--node --exited : #{code}"
console.log "binding events"
return node
task 'compile:coffee', 'Compliles the coffee source code into javascript', (callback) ->
console.log "compiling coffee"
hash = generateHash()
exec "coffee --join #{jsDir}/application.#{hash}.js --compile #{coffeeFiles}/", (err, stdout, stderr) ->
if err
console.log stderr
#throw err
console.log "-- deleting old js file"
paths.exists "#{jsDir}/application.#{config.js}.js", (exists) ->
if(exists)
fs.unlink "#{jsDir}/application.#{config.js}.js", (err) ->
if(err)
console.log err
else
console.log '-- -- old js file deleted'
else
console.log "-- -- could not find application.#{config.js}.js"
updateConfigFile {"js" : hash}
restartServer() if nodeInstance
console.log "#{stdout} #{stderr}"
console.log("compilation coffee completed")
task 'compile:project:coffee', 'Compliles the coffee source code into javascript', (callback) ->
exec "coffee --compile --output #{projectDir}/common/ #{projectDir}/src/", (err, stdout, stderr) ->
if err
console.log stderr
#throw err
console.log "#{stdout} #{stderr}"
console.log("project compilation coffee completed")
exec "coffee --compile #{projectDir}/app.coffee", (err, stdout, stderr) ->
if err
console.log stderr
#throw err
console.log "#{stdout} #{stderr}"
console.log("app compilation coffee completed")
restartServer() if nodeInstance
invoke "test" if testing
task 'compile:less', 'Compliles the less source into css', (callback) ->
console.log "Compiling Less"
include_paths = ""
for path in lessIncPaths
include_paths += "#{lessFiles}/libs/#{path}:"
include_paths = include_paths[0..include_paths.length - 2]
csshash = generateHash()
exec "lessc --include-path=#{include_paths} #{lessFiles}/application.less #{cssDir}/application.#{csshash}.css", (err, stdout, stderr) ->
if err
console.log "#{stdout} #{stderr}"
#throw err
console.log "-- deleting old css file"
paths.exists "#{cssDir}/application.#{config.css}.css", (exists) ->
if(exists)
fs.unlink "#{cssDir}/application.#{config.css}.css", (err) ->
if(err)
console.log err
else
console.log '-- -- old css file deleted'
else
console.log "-- -- could not find application.#{config.css}.css"
updateConfigFile {"css" : csshash}
restartServer() if nodeInstance
console.log "#{stdout} #{stderr}"
console.log "Compilation Less Completed"
task 'watch:less', 'Watches the less files and compiles them if they change', (callback) ->
watch.add "#{lessFiles}"
watch.onChange (file , prev, curr, action) ->
console.log("LESS: #{file} (#{action}) ")
invoke 'compile:less'
task 'watch:coffee', 'Watches the coffee files and compiles them if they change', (callback) ->
watch.add "#{coffeeFiles}"
watch.onChange (file , prev, curr, action) ->
console.log("COFFEE: #{file} (#{action}) ")
invoke 'compile:coffee'
task 'watch:assets', 'Watches all the assets and compiles them if they change', (callback) ->
watch.add "#{assets}", true
watch.onChange (file , prev, curr, action) ->
is_less = /\.less/.test(file)
is_coffee = /\.coffee/.test(file)
console.log("#{file} (#{action})")
invoke 'compile:less' if is_less
invoke 'compile:coffee' if is_coffee
task 'watch:project', 'Watches all the assets and compiles them if they change', (callback) ->
restartServerOrCreate()
appWatcher.add "#{projectDir}/app.coffee"
appWatcher.add "#{projectDir}/src"
appWatcher.onChange (file , prev, curr, action) ->
invoke 'compile:project:coffee'
task 'watch:build:all', 'Watches all files and complies and restart server if change', ->
invoke 'watch:assets'
invoke 'watch:project'
task 'watch:test', 'Watches all files and complies and restart server if change', ->
testing = true
invoke 'test'
appWatcher.add "#{projectDir}/src"
appWatcher.add "#{projectDir}/test"
appWatcher.onChange (file , prev, curr, action) ->
invoke 'compile:project:coffee'
task 'test', 'runs the test suite', ->
test = exec "./node_modules/mocha/bin/mocha --growl", (err, stdout, stderr) ->
console.log "#{stdout} #{stderr}"