-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGruntfile.js
451 lines (429 loc) · 12.2 KB
/
Gruntfile.js
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
module.exports = function (grunt) {
'use strict';
var path = require('path');
var derequire = require('derequire');
var insertModuleGlobals = require('insert-module-globals');
var fs = require('fs');
var banner = '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> */\n' +
grunt.file.read('LICENCE');
function insertVars (names) {
return names.reduce(function (vars, name) {
vars[name] = insertModuleGlobals.vars[name];
return vars;
}, {});
}
function processBuildFile (src, filepath) {
if (filepath === 'build/version.js') { // please tell me there is a saner way to do this.
return grunt.template.process(src);
} else {
return src;
}
}
var shouldjs = require.resolve('should');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
bundle: {
files: {
'dist/im.min.js': ['dist/im.js']
}
}
},
coffeelint: {
options: grunt.file.readJSON('coffeelint.json'),
source: ['src/*.coffee'],
mocha: ['test/mocha/*.coffee']
},
compile: {
source: {
src: 'src/*',
dest: 'build'
},
shims: {
src: 'src/shims/*',
dest: 'build/shims'
},
tests: {
src: 'test/mocha/*.coffee',
dest: 'test/compiled'
},
testlib: { // This is ugly - should be one task.
src: 'test/mocha/lib/*.coffee',
dest: 'test/compiled/lib'
}
},
jshint: {
// Follow github's style guidelines and Crockford's where those are not sufficient
// * https://github.com/styleguide/javascript
// * http://javascript.crockford.com/code.html
options: {
laxcomma: true,
asi: true,
curly: true,
maxparams: 5
},
grunt: {
options: {
asi: true,
curly: true,
maxparams: 5,
indent: 2,
node: true
},
files: {
src: ["Gruntfile.js", "tasks/*.js"]
}
},
tests: {
options: {
asi: true,
curly: true,
maxparams: 5,
indent: 2,
browser: true,
devel: true,
jquery: true
},
globals: {
TestCase: true,
_: true,
test: true,
asyncTest: true,
start: true,
ok: true,
equal: true,
notEqual: true,
module: true,
deepEqual: true,
intermine: true
},
files: {
src: ['test/browser/acceptance.js']
}
}
},
clean: {
build: ['build'],
tests: ['test/compiled'],
cdnlinks: {
src: ['<%= CDN %>/js/intermine/imjs/latest', '<%= CDN %>/js/intermine/imjs/<%= pkg.version.replace(/\\d+$/, "x") %>'],
options: {force: true}
}
},
mochaTest: {
test: {
src: ['test/compiled/*.js'],
options: grunt.file.readJSON('mocha-opts.json')
}
},
simplemocha: {
all: {
src: 'test/mocha/*.coffee',
options: grunt.file.readJSON('mocha-opts.json')
}
},
mocha_phantomjs: {
all: ['test/browser/index.html'],
bundle: ['test/browser/bundle-index.html']
},
bump: {
options: {
files: ['package.json'],
commitFiles: ['-a'],
updateConfigs: ['pkg'],
pushTo: 'origin'
}
},
CDN: process.env.CDN,
copy: {
dist: {
files: [{
expand: true,
src: ['*.js'],
cwd: 'dist',
dest: 'js/',
flatten: true,
filter: 'isFile'
}]
},
version: {
files: [{
expand: true,
src: ['*.js'],
cwd: 'dist',
flatten: true,
dest: 'js/<%= pkg.version %>/',
filter: 'isFile'
}]
},
cdn: {
files: [{
expand: true,
cwd: 'js/<%= pkg.version %>',
src: ['*.js'],
filter: 'isFile',
flatten: true,
dest: '<%= CDN %>/js/intermine/imjs/<%= pkg.version %>/'
}]
},
mockResponses: {
files: [{
expand: true,
cwd: 'test/mocha/lib/responses/',
src: ['**'],
dest: 'test/compiled/lib/responses/'
}]
},
mockBundledResponses: {
files: [{
expand: true,
cwd: 'test/mocha/lib/bundledResponses/',
src: ['**'],
dest: 'test/compiled/lib/bundledResponses/'
}]
}
},
browserify: {
dist: {
files: {
'dist/im.js': ['build/export.js']
},
options: {
browserifyOptions: {
standalone: 'imjs',
insertGlobalVars: insertVars(['process', 'global', '__filename', '__dirname'])
},
postBundleCB: bundled,
}
},
tests: {
files: {
'test/browser/mocha-test.js': ['test/compiled/**/*.js'],
},
options: {
transform: ['envify'],
alias: [
shouldjs + ':should'
],
browserifyOptions: {
insertGlobalVars: insertVars(['process', 'global', '__filename', '__dirname'])
}
}
}
},
jscoverage: {
options: {
inputDirectory: 'build',
outputDirectory: 'build-cov'
}
},
symlink: {
options: {
overwrite: true,
force: true
},
latest: {
src: '<%= CDN %>/js/intermine/imjs/<%= pkg.version %>',
dest: '<%= CDN %>/js/intermine/imjs/latest'
},
group: {
src: '<%= CDN %>/js/intermine/imjs/<%= pkg.version %>',
dest: '<%= CDN %>/js/intermine/imjs/<%= pkg.version.replace(/\\d+$/, "x") %>'
}
},
env: {
unit: {
IMJS_TESTS: 'UNIT'
},
integration: {
IMJS_TESTS: 'INTEGRATION'
}
}
})
function bundled (error, src, next) {
if (error) {
return next(error);
}
try {
var bundleBanner = grunt.template.process(banner)
var openIFE = "(function (intermine) {";
var closeIFE ='})(window.intermine);';
next(null, derequire([bundleBanner, openIFE, src, closeIFE].join("\n")));
} catch (e) {
next(e)
}
}
function injectVersion(file) {
var src, dest, temp, output
src = dest = 'build/version.js'
temp = grunt.file.read(src)
output = grunt.template.process(temp)
grunt.file.write(dest, output, {encoding: 'utf8'})
grunt.log.writeln("Injected version")
}
grunt.loadNpmTasks("grunt-jscoverage")
grunt.loadNpmTasks('grunt-bump')
grunt.loadNpmTasks('grunt-contrib-copy')
grunt.loadNpmTasks('grunt-contrib-uglify')
grunt.loadNpmTasks('grunt-contrib-jshint')
grunt.loadNpmTasks('grunt-coffeelint')
grunt.loadNpmTasks('grunt-mocha-test')
grunt.loadNpmTasks('grunt-contrib-clean')
grunt.loadNpmTasks('grunt-mocha-phantomjs')
grunt.loadNpmTasks('grunt-contrib-symlink')
grunt.loadNpmTasks('grunt-browserify')
grunt.loadNpmTasks('grunt-env')
grunt.loadTasks('tasks')
grunt.registerTask('-load-test-globals', function () {
global.should = require('should')
})
function writeTestIndex(src, dest, options) {
var templ = grunt.file.read(src, 'utf8')
var outf = dest;
var host = getVar('host', null);
var port = getVar('port', null);
var load = getVar('load', null);
var path = getVar('path', 'intermine-demo');
var tokn = getVar('token', 'test-user-token');
var obj = {
args: {
host: host,
path: path,
port: port,
token: tokn
},
load: load,
mocha: {
css: "../../node_modules/mocha/mocha.css",
js: "../../node_modules/mocha/mocha.js"
},
expect: {
js: "../../node_modules/expect/expect.js"
},
promise: {
js: "../../node_modules/q/q.js"
}
}
var processed = grunt.template.process(templ.toString(), {data: obj})
grunt.file.write(outf, processed)
grunt.log.writeln('Wrote ' + outf)
function getVar(key, otherwise) {
return process.env['TESTMODEL_' + key.toUpperCase()] ||
grunt.option(key) ||
options[key] ||
otherwise;
}
}
grunt.registerTask(
'-inject-version',
'Inject the current version number into the build',
injectVersion
)
grunt.registerTask(
'build-unit-test-suite',
'build the test suite loaded by phantom js', function () {
writeTestIndex('test/browser/bundle-template.html', 'test/browser/bundle-index.html', {
host: 'localhost',
port: '8080',
load: 'mocha-test.js'
})
})
grunt.registerTask(
'build-acceptance-index',
'Build a index.html page to run acceptance tests in the browser. Since by default this task generates an html file' +
' that connects to a test server on the same host and port as the current browser location, the primary function' +
' of this task is to generate a page that does not require cross-domain requests and enables ie8 to be tested.',
function () {
writeTestIndex('test/browser/template.html', 'test/browser/accept.html', {load: '../../js/im.js'})
})
grunt.registerTask(
'build-phantom-acceptance-index',
'Build a index.html page to run acceptance tests in phantomjs', function () {
writeTestIndex('test/browser/template.html', 'test/browser/index.html', {
host: 'localhost',
port: '8080',
load: '../../js/im.js'
})
})
grunt.registerTask(
'build-static-acceptance-index',
'Build a index.html page to run acceptance tests as a static file', function () {
writeTestIndex('test/browser/template.html', 'test/browser/acceptance.html', {
host: 'demo.intermine.org',
port: '80',
path: 'testmine',
load: '../../js/im.js'
})
})
grunt.registerTask('phantomjs', [
'build-phantom-acceptance-index',
'build-unit-test-suite',
'mocha_phantomjs'
])
grunt.registerTask('browser-indices', [
'build-acceptance-index',
'build-static-acceptance-index'
])
grunt.registerTask('mocha-node', 'Run tests in the nodejs VM', function () {
grunt.task.run('-set-test-files')
grunt.task.run('mochaTest:test')
})
grunt.registerTask('-checkcdn', 'Check that the CDN is initialised', function () {
var done = this.async()
var cdn = grunt.config('CDN')
if (!cdn) {
grunt.log.error("No CDN location provided. Please set the CDN environment variable")
done(false)
}
fs.stat(cdn, function (err, stats) {
if (err) {
grunt.log.error("Problem with CDN location: " + err)
done(false)
} else if (!stats.isDirectory()) {
grunt.log.error("CDN location is not a directory")
done(false)
} else {
grunt.log.writeln("CDN configured as: " + cdn);
done();
}
})
})
grunt.registerTask(
'-set-test-files',
'Set the value of the browserify test files', function () {
var grep = grunt.option('grep')
var reporter = grunt.option('reporter')
if (grep) {
grunt.config('mochaTest.test.options.grep', grep)
grunt.config('browserify.tests.files', {
'test/browser/mocha-test.js': ['test/compiled/*' + grep + '*.js']
})
}
if (reporter) {
grunt.config('mochaTest.test.options.reporter', reporter)
}
})
grunt.registerTask('cdn', ['default', '-checkcdn', 'copy:cdn', 'clean:cdnlinks', 'symlink'])
grunt.registerTask('bmp', ['bump-only', 'default', 'bump-commit'])
grunt.registerTask('build', [
'clean:build',
'clean:tests',
'compile',
'-inject-version',
'browserify',
'uglify',
'copy:mockResponses',
'copy:mockBundledResponses',
'copy:dist',
'copy:version'
])
grunt.registerTask('demo', ['build', 'browser-indices'])
grunt.registerTask('lint', ['jshint', 'coffeelint'])
grunt.registerTask('test', ['lint', 'build', 'mocha-node'])
grunt.registerTask('test:unit', ['lint', 'build', 'env:unit', 'mocha-node'])
grunt.registerTask('test:integration', ['lint', 'build', 'env:integration', 'mocha-node'])
grunt.registerTask('test:node', ['compile', 'mocha-node'])
// grunt.registerTask('test:browser', ['-set-test-files', 'build', 'browser-indices', 'phantomjs'])
grunt.registerTask('default', ['lint','build'])
}