This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Gruntfile.js
694 lines (600 loc) · 21.1 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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/* global require, module, console */
/*
* All source files are created and managed by type in `src`.
*
* There three output directories:
*
* - `local` - Development and testing
* - `build` - Intermediate directory prior to optimizations and/or
* concatentation
* - `dist` - Final output for release purposes. This carries over to the master
* branch to be copied in the root of the repo.
* - `cdn` - Final output for deployment on CDNs. This includes *only* Cilantro
* files, no source maps, and no third-party libraries.
*
* Each Grunt task has a target named `local` which performs copying,
* compiling or symlinking in the `local` directory for development.
* Additional targets may be defined for `build` or `dist` depending on
* the requirements.
*
* Shared options for a task should be defined first, followed by each
* task entry. Each task can define it's own set of options to override
* the shared ones defined for the task.
*/
module.exports = function(grunt) {
var shell = require('shelljs');
var pkg = grunt.file.readJSON('package.json');
var run = function(cmd) {
grunt.log.ok(cmd);
shell.exec(cmd);
};
var vendorModules = [
'jquery',
'backbone',
'underscore',
'marionette',
'highcharts',
'bootstrap',
'json2',
'loglevel'
];
grunt.initConfig({
pkg: pkg,
srcDir: 'src',
specDir: 'spec',
localDir: 'local',
buildDir: 'build',
distDir: 'dist',
cdnDir: 'cdn',
serve: {
forever: {
options: {
keepalive: true,
port: 8125
}
},
jasmine: {
options: {
port: 8126
}
}
},
watch: {
grunt: {
tasks: ['local'],
files: ['Gruntfile.js']
},
copy: {
tasks: ['copy:local'],
files: [
'<%= srcDir %>/js/**/**/**/*',
'<%= srcDir %>/css/**/**/**/*',
'<%= srcDir %>/font/**/**/**/*',
'<%= srcDir %>/img/**/**/**/*'
]
},
tests: {
tasks: ['jasmine:local:build'],
files: ['<%= specDir %>/**/**/**/*']
},
sass: {
tasks: ['sass:local'],
files: ['<%= srcDir %>/scss/**/**/**/*']
}
},
sass: {
options: {
sourcemap: true
},
local: {
options: {
trace: true,
style: 'expanded'
},
files: {
'<%= localDir %>/css/style.css': '<%= srcDir %>/scss/style.scss'
}
},
dist: {
options: {
quiet: true,
style: 'compressed'
},
files: {
'<%= distDir %>/css/style.css': '<%= srcDir %>/scss/style.scss'
}
},
cdn: {
options: {
quiet: true,
sourcemap: false,
style: 'compressed'
},
files: {
'<%= cdnDir %>/css/style.css': '<%= srcDir %>/scss/style.scss'
}
}
},
copy: {
local: {
files: [{
expand: true,
cwd: '<%= srcDir %>/js',
src: ['**/*'],
dest: '<%= localDir %>/js'
}, {
expand: true,
cwd: '<%= srcDir %>/css',
src: ['**/*'],
dest: '<%= localDir %>/css'
}, {
expand: true,
cwd: '<%= srcDir %>/font',
src: ['**/*'],
dest: '<%= localDir %>/font'
}, {
expand: true,
cwd: '<%= srcDir %>/img',
src: ['**/*'],
dest: '<%= localDir %>/img'
}, {
expand: true,
src: ['bower.json', 'package.json'],
dest: '<%= localDir %>'
}]
},
build: {
files: [{
expand: true,
cwd: '<%= srcDir %>/templates',
src: ['**/*'],
dest: '<%= buildDir %>/js/templates'
}, {
expand: true,
cwd: '<%= srcDir %>/js',
src: ['**/*'],
dest: '<%= buildDir %>/js'
}]
},
dist: {
files: [{
expand: true,
cwd: '<%= srcDir %>/css',
src: ['**/*'],
dest: '<%= distDir %>/css'
}, {
expand: true,
cwd: '<%= srcDir %>/font',
src: ['**/*'],
dest: '<%= distDir %>/font'
}, {
expand: true,
cwd: '<%= srcDir %>/img',
src: ['**/*'],
dest: '<%= distDir %>/img'
}, {
expand: true,
src: ['bower.json', 'package.json'],
dest: '<%= distDir %>'
}]
},
cdn: {
files: [{
expand: true,
cwd: '<%= srcDir %>/img',
src: ['checkmark.png'],
dest: '<%= cdnDir %>/img'
}]
}
},
symlink: {
options: {
type: 'dir'
},
templates: {
relativeSrc: '../../<%= srcDir %>/templates',
dest: '<%= localDir %>/js/templates'
}
},
requirejs: {
options: {
mainConfigFile: '<%= srcDir %>/js/cilantro/main.js',
baseUrl: '.',
inlineText: true,
preserveLicenseComments: false,
wrap: false,
logLevel: 1,
throwWhen: {
optimize: true
},
modules: [{
name: pkg.name,
exclude: vendorModules
}],
done: function(done, output) {
var dups = require('rjs-build-analysis').duplicates(output);
if (dups.length > 0) {
grunt.log.subhead('Duplicates found in RequireJS build:');
grunt.log.warn(dups);
done(new Error('r.js built duplicate modules, please ' +
'check the `excludes` option.'));
}
done();
}
},
dist: {
options: {
appDir: '<%= buildDir %>/js',
dir: '<%= distDir %>/js',
optimize: 'uglify2',
generateSourceMaps: true,
removeCombined: false
}
},
cdn: {
options: {
appDir: '<%= buildDir %>/js',
dir: '<%= cdnDir %>/js',
optimize: 'uglify2',
generateSourceMaps: false,
removeCombined: true
}
}
},
clean: {
local: ['<%= localDir %>'],
build: ['<%= buildDir %>'],
dist: ['<%= distDir %>'],
cdn: ['<%= cdnDir %>'],
postdist: ['<%= distDir %>/js/templates'],
postcdn: [
'<%= cdnDir %>/js/templates',
'<%= cdnDir %>/js/require.js'
].concat(vendorModules.map(function(mod) {
return '<%= cdnDir %>/js/' + mod + '.js';})),
release: [
'<%= localDir %>/js/build.txt',
'<%= distDir %>/js/build.txt',
'<%= cdnDir %>/js/build.txt'
]
},
jasmine: {
options: {
specs: '<%= specDir %>/**/**/**/*.js',
host: 'http://127.0.0.1:8126',
helpers: './specConfig.js',
keepRunner: true,
template: require('grunt-template-jasmine-requirejs'),
templateOptions: {
version: '<%= srcDir %>/js/require.js'
}
},
local: {
options: {
templateOptions: {
requireConfigFile: '<%= localDir %>/js/cilantro/main.js',
requireConfig: {
baseUrl: '<%= localDir %>/js'
}
}
}
},
dist: {
options: {
templateOptions: {
requireConfigFile: '<%= distDir %>/js/cilantro/main.js',
requireConfig: {
baseUrl: '<%= distDir %>/js'
}
}
}
}
},
amdcheck: {
local: {
options: {
removeUnusedDependencies: false
},
files: [{
expand: true,
src: ['<%= localDir %>/js/**/**/**/**/*.js']
}]
}
},
jshint: {
options: {
camelcase: true,
immed: true,
indent: 4,
latedef: true,
noarg: true,
noempty: true,
undef: true,
unused: true,
trailing: true,
maxdepth: 3,
maxlen: 90,
browser: true,
eqeqeq: true,
globals: {
define: true,
require: true
},
reporter: require('jshint-stylish'),
ignores: [
'<%= srcDir %>/js/backbone.js',
'<%= srcDir %>/js/bootstrap.js',
'<%= srcDir %>/js/highcharts.js',
'<%= srcDir %>/js/jquery.js',
'<%= srcDir %>/js/json2.js',
'<%= srcDir %>/js/loglevel.js',
'<%= srcDir %>/js/marionette.js',
'<%= srcDir %>/js/require.js',
'<%= srcDir %>/js/text.js',
'<%= srcDir %>/js/tpl.js',
'<%= srcDir %>/js/underscore.js',
'<%= srcDir %>/js/plugins/bootstrap-datepicker.js',
'<%= srcDir %>/js/plugins/jquery-easing.js',
'<%= srcDir %>/js/plugins/jquery-ui.js'
],
},
src: ['<%= srcDir %>/js/**/**/**/**/*.js']
}
});
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-symlink');
grunt.loadNpmTasks('grunt-amdcheck');
grunt.registerMultiTask('serve', 'Run a Node server for testing', function() {
var http = require('http');
var path = require('path');
var url = require('url');
var fs = require('fs');
var contentTypes = {
'.js': 'text/javascript',
'.css': 'text/css',
'.html': 'text/html'
};
var options = this.options({
hostname: 'localhost',
base: '.',
port: 8125,
keepalive: false
});
var serveResponse = function(filename, response) {
var extname = path.extname(filename);
var contentType = contentTypes[extname] || 'text/plain';
response.writeHead(200, {'Content-Type': contentType});
var stream = fs.createReadStream(filename);
stream.pipe(response);
};
var serve404 = function(filename, response) {
response.writeHead(404);
response.end();
};
var serve500 = function(filename, response) {
response.writeHead(500);
response.end();
};
var server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
var filename = path.join(options.base, uri);
fs.exists(filename, function(exists) {
if (exists) {
fs.readFile(filename, function(error) {
if (error) {
if (error.code === 'EISDIR') {
filename += 'index.html';
fs.exists(filename, function(exists) {
if (exists) {
fs.readFile(filename, function(error) {
if (error) {
serve500(filename, response);
}
else {
serveResponse(filename, response);
}
});
}
else {
serve404(filename, response);
}
});
}
else {
serve500(filename, response);
}
}
else {
serveResponse(filename, response);
}
});
}
else {
serve404(filename, response);
}
});
});
if (options.hostname === '*') {
options.hostname = null;
}
if (options.port === '?') {
options.port = 0;
}
var done = this.async();
return server.listen(options.port, options.hostname).on('listening', function() {
var address = server.address();
var hostname = server.hostname || 'localhost';
if (!options.keepalive) {
done();
}
else {
grunt.log.writeln('Listening on ' + hostname + ':' +
address.port + '...');
}
}).on('error', function(error) {
if (error.code === 'EADDRINUSE') {
grunt.fatal('Port ' + options.port + ' is already in use by ' +
'another process.');
}
else {
grunt.fatal(error);
}
});
});
grunt.registerTask('local', 'Creates a build for local development and testing', [
'sass:local',
'copy:local',
'symlink',
'jasmine:local:build'
]);
grunt.registerTask('dist', 'Creates a build for distribution', [
'clean:build',
'copy:build',
'clean:dist',
'requirejs:dist',
'sass:dist',
'copy:dist',
'clean:postdist'
]);
grunt.registerTask('cdn', 'Creates a build for CDN distribution', [
'clean:build',
'copy:build',
'clean:cdn',
'requirejs:cdn',
'sass:cdn',
'copy:cdn',
'clean:postcdn',
]);
grunt.registerTask(
'work',
'Performs the initial local build and starts a watch process',
[
'local',
'watch'
]
);
grunt.registerTask('test', 'Runs the headless test suite', [
'copy:local',
'symlink',
'serve:jasmine',
'jasmine:local'
]);
var changeVersion = function(fname, version) {
var contents = grunt.file.readJSON(fname);
var current = contents.version;
contents.version = version;
grunt.file.write(fname, JSON.stringify(contents, null, 2));
grunt.log.ok('' + fname + ': ' + current + ' => ' + version);
};
var replaceVersion = function(fname, current, version) {
var options = {encoding: 'utf8'};
var content = grunt.file.read(fname, options);
var regexp = new RegExp("version: '" + current + "'");
if (!regexp.test(content)) {
grunt.fatal('File contents does not match version');
}
content = content.replace(regexp, "version: '" + version + "'");
grunt.file.write(fname, content, options);
grunt.log.ok('' + fname + ': ' + current + ' => ' + version);
};
// Uses package.json as the canonical version.
grunt.registerTask('bump-final', 'Updates the version to final', function() {
var svutil = require('semver-utils');
var current = pkg.version;
var version = svutil.parse(pkg.version);
// Ensure it is able to be bumped.
if (version.release !== 'beta') {
grunt.fatal('Version ' + current + ' not beta. Is this ready for release?');
}
// Remove release and build strings.
version.release = '';
version.build = '';
pkg.version = svutil.stringify(version);
replaceVersion('src/js/cilantro/core.js', current, pkg.version);
var files = ['package.json', 'bower.json'];
for (var i = 0; i < files.length; i++) {
changeVersion(files[i], pkg.version);
}
});
grunt.registerTask(
'bump-patch',
'Updates the version to next patch-release',
function() {
var svutil = require('semver-utils');
var current = pkg.version;
var version = svutil.parse(pkg.version);
// Ensure it is able to be bumped.
console.log(version.release);
if (version.release) {
grunt.fatal('Version ' + current + ' not final. Should this ' +
'be bumped to a pre-release?');
}
// Remove release and build strings
version.patch = '' + (parseInt(version.patch, 10) + 1);
version.release = 'beta';
version.build = '';
pkg.version = svutil.stringify(version);
replaceVersion('src/js/cilantro/core.js', current, pkg.version);
var files = ['package.json', 'bower.json'];
for (var i = 0; i < files.length; i++) {
changeVersion(files[i], pkg.version);
}
run('git add bower.json package.json src/js/cilantro/core.js');
run("git commit -s -m '" +
[version.major, version.minor, version.patch].join('.') + " Beta'");
}
);
grunt.registerTask('tag-release', 'Create a release on master', function() {
run('git add bower.json package.json src/js/cilantro/core.js cdn/');
run("git commit -s -m '" + pkg.version + " Release'");
run('git tag ' + pkg.version);
});
grunt.registerTask(
'release-binaries',
'Create a release binary for upload',
function() {
var releaseDirName = '' + pkg.name + '-' + pkg.version;
run('rm -rf ' + pkg.name);
run('mkdir -p ' + pkg.name);
run('cp -r dist/* ' + pkg.name);
run('zip -r ' + releaseDirName + '.zip ' + pkg.name);
run('tar -Hzcf ' + releaseDirName + '.tar.gz ' + pkg.name);
run('rm -rf ' + pkg.name);
run('mkdir -p ' + pkg.name);
run('cp -r local/* ' + pkg.name);
run('zip -r ' + releaseDirName + '-src.zip ' + pkg.name);
run('tar -Hzcf ' + releaseDirName + '-src.tar.gz ' + pkg.name);
run('rm -rf ' + pkg.name);
}
);
grunt.registerTask(
'release-help',
'Prints the post-release steps',
function() {
grunt.log.ok('Push the code and tags: git push && git push --tags');
grunt.log.ok('Go to ' + pkg.homepage + '/releases to update the ' +
'release descriptions and upload the binaries.');
}
);
grunt.registerTask(
'release',
'Builds distribution files, creates release binaries, and creates Git tag',
[
'bump-final',
'local',
'dist',
'cdn',
'clean:release',
'release-binaries',
'tag-release',
'release-help',
'bump-patch'
]
);
};