-
Notifications
You must be signed in to change notification settings - Fork 0
/
gruntfile.coffee
339 lines (290 loc) · 9.65 KB
/
gruntfile.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
###
Created by Evgeny Bogdanov on 10.10.2013
Copyright (c) 2013 Evgeny Bogdanov. All rights reserved.
###
module.exports = (grunt) ->
# Project configuration.
grunt.initConfig
watch:
# spawn: false - is needed to be able to change the grunt config on the fly
# recompile jade/coffee/stylus templates when they change
jade:
files: ["./**/*.jade"]
tasks: ["jade"]
options:
spawn: false
livereload: true
stylus:
files: ["./**/*.styl"]
tasks: ["stylus"]
options:
spawn: false
livereload: true
# tasks "coffee" and "test" will be called automatically via "watch" event
coffee:
files: ["./**/*.coffee"]
options:
spawn: false
livereload: true
jade:
compile:
options:
pretty: true
files: [
expand: true # Enable dynamic expansion.
cwd: 'test/' # Src matches are relative to this path.
src: ['**/*.jade'] # Actual pattern(s) to match.
dest: 'test/' # Destination path prefix.
ext: '.html' # Dest filepaths will have this extension.
]
stylus:
compile:
files: [
expand: true # Enable dynamic expansion.
cwd: 'test/' # Src matches are relative to this path.
src: ['**/*.styl'] # Actual pattern(s) to match.
dest: 'test/' # Destination path prefix.
ext: '.css' # Dest filepaths will have this extension.
]
options:
compress: false
coffee:
compile:
files: [
expand: true
cwd: '.'
src: ['**/*.coffee']
dest: '.'
ext: '.js'
filter: (filepath) ->
return (not filepath.match /node_modules/)
]
grunt:
src: "gruntfile.coffee"
dest: "gruntfile.js"
cssmin:
minify:
expand: true
cwd: "public/cdn/css/"
src: ["*.css"]
dest: "public/cdn/css/"
ext: ".css"
uglify:
options:
mangle: true
minify:
expand: true
cwd: 'lib/'
src: ['*.js']
dest: 'lib/'
ext: '.min.js'
# Run all the tests (or a single test)
simplemocha:
options:
globals: ["should"]
timeout: 3000
ignoreLeaks: false
ui: "bdd"
reporter: "dot" # specify the reporter to use
growl: true # enable growl notification support
bail: true # bail after first test failure
recursive: true # include sub directories
all:
src: ["test/unit/**/*_test.js", "public/test/unit/**/*_test.js"]
nodemon:
dev:
options:
file: "server.js"
args: []
nodeArgs: ["--debug=5081"]
ignoredFiles: ["README.md", "node_modules/**", ".DS_Store", "public/**", "test/**"]
watchedExtensions: ["js"]
watchedFolders: ["."]
delayTime: 1
cwd: __dirname
env:
PORT: 8000
concurrent:
target:
tasks: ["watch"]
options:
logConcurrentOutput: true
###
Custom tasks
###
# default when grunt starts (calls concurrent:target internally)
# $ grunt
start:
target: {}
# mock task for testing (calls simplemocha internally)
# $ grunt test
test:
run: {}
shell:
# runs a mocha test with --debug-brk
debugTest:
options:
stdout: true
command: ""
# starts node-inspector processes for debuging the app and tests
debug:
options:
stdout: true
command:
"make debug"
# copy minified file to remote server
publish:
options:
stdout: true
stderr: true
command: [
# minify the iwc.js file
"grunt uglify:minify"
# using rsync with compression
"rsync -az --delete --force ./lib/*.min.js admin@graasp.epfl.ch:/Graaasp/shared/system/gadgets_src/libs/"
].join '&&'
# Cleans up the repo after build is done and tests are run
cleanUp:
options:
stdout: true
stderr: true
command: [
# remove min files
"rm -rf lib/*.min.js"
].join '&&'
# Load NPM tasks
grunt.loadNpmTasks "grunt-contrib-watch"
grunt.loadNpmTasks "grunt-nodemon"
grunt.loadNpmTasks "grunt-concurrent"
grunt.loadNpmTasks 'grunt-contrib-stylus'
grunt.loadNpmTasks 'grunt-contrib-coffee'
grunt.loadNpmTasks 'grunt-shell'
grunt.loadNpmTasks 'grunt-contrib-jade'
grunt.loadNpmTasks 'grunt-contrib-cssmin'
grunt.loadNpmTasks 'grunt-contrib-uglify'
grunt.loadNpmTasks 'grunt-mocha'
###
Register all tasks
###
grunt.registerTask "default", ["start:target"]
grunt.registerTask "debug", ["shell:debug"]
grunt.registerTask "deploy", ["shell:publish", "shell:cleanUp"]
# compiles all jade/styl/coffee
grunt.registerTask "compile", ["jade", "stylus", "coffee:compile"]
###
Graspio custom tasks
###
# default task with some options
# to run a single test or coffee from command line:
# $ grunt --test=path_to_test.js
# $ grunt --coffee=path_to_test.js
#
# $ grunt
# -> starts concurrent for the whole development server
grunt.registerMultiTask "start", "Starts the server", ->
# process single test provided on command line
if grunt.option("test")
test = grunt.option("test")
grunt.config "simplemocha.all.src", test
grunt.task.run("simplemocha")
return
# process single test provided on command line
if grunt.option("debugtest")
test = grunt.option("debugtest")
mochaCommand = "./node_modules/mocha/bin/mocha --debug-brk=5082 " + test
grunt.config "shell.debugTest.command", mochaCommand
grunt.task.run("shell:debugTest")
return
# process single coffee provided on command line
if grunt.option("coffee")
coffee = grunt.option("coffee")
grunt.config "coffee.compile.src", coffee
grunt.config "coffee.compile.dest", coffee.replace(".coffee", ".js")
grunt.task.run("coffee:compile")
return
if grunt.option.flags().length is 0
grunt.task.run "concurrent:target"
# runs one test or all tests in the project
grunt.registerMultiTask "test", "Runs unit mocha tests", ->
# when src and dest are given, it runs only one test
if @data.src
# if file is .coffee, replace it into .js since we use .js for tests
file = @data.src.replace ".coffee", ".js"
test = file
if not file.match /_test.js/
# the file is not a test file, find the corresponding test
# make it a test file
test = test.replace /\.[\w]+$/, "_test.js"
# file to test mapping
if test.match /^app/
test = test.replace /^app/, "test/unit"
if test.match /^lib/
test = test.replace /^lib/, "test/unit/lib"
if test.match /^config/
test = test.replace /^config/, "test/unit/config"
if test.match /^public\/js/
test = test.replace /^public\/js/, "public/test/unit"
# run the test with mocha
grunt.config "simplemocha.all.src", test
grunt.task.run("simplemocha")
else
# run all tests
grunt.task.run("simplemocha")
###
Misc
###
# on watch events configure coffee/jade/stylus to only run on a changed file
#
# it replaces the default behavior of the compile task by assigning to it
# src and dest fields referencing the changed file
# the paths for src and dest are taken from the corresponding compile task
grunt.event.on 'watch', (action, filepath, target) ->
# specific processing of the gruntfile
if filepath.match /gruntfile.coffee/
grunt.task.run("coffee:grunt")
return
srcExt = filepath.match(/\.[\w]+$/)[0] # file extension
switch srcExt
when ".styl"
task = "stylus"
when ".jade"
task = "jade"
when ".coffee"
task = "coffee"
else
task = false
if task
taskConfig = grunt.config(task + ".compile.files")[0]
srcDir = taskConfig.cwd # directory with src files: public/jade/
destDir = taskConfig.dest # directory for dest: public/views/
destExt = taskConfig.ext # file extension of destination files: .html
# creat new target file path
target = filepath.replace(srcExt, destExt)
.replace(srcDir, destDir)
grunt.config(task + '.compile.src', filepath)
grunt.config(task + '.compile.dest', target)
# compile coffee file
if task == "coffee"
grunt.task.run("coffee:compile")
# run unit test if .coffee or .js file was changed
if srcExt == ".coffee" or srcExt == ".js"
grunt.config('test.run.src', filepath)
grunt.task.run("test")
requireUncache = require "require-uncache"
# Modified grunt task taken from https://github.com/yaymukund/grunt-simple-mocha
# added force require of mocha, otherwise tests don't run in watch
grunt.registerMultiTask "simplemocha", "Run tests with mocha", ->
# !!! force reloading of cached mocha module
requireUncache("mocha")
Mocha = require "mocha"
options = @options()
mocha_instance = new Mocha(options)
@filesSrc.forEach mocha_instance.addFile.bind(mocha_instance)
# We will now run mocha asynchronously and receive number of errors in a
# callback, which we'll use to report the result of the async task by
# calling done() with the appropriate value to indicate whether an error
# occurred.
done = @async()
mocha_instance.run (errCount) ->
withoutErrors = (errCount is 0)
done withoutErrors