forked from jsfiddle/togetherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
788 lines (718 loc) · 27.3 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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
/*jshint forin:false */
var fs = require("fs");
var path = require('path');
var nunjucks = require("nunjucks");
var marked = require("marked");
var docco = require("docco");
var vars = {
enableExample: false,
enableHome: false,
GA_ACCOUNT: "UA-35433268-28",
base: ""
};
var TESTDIR = "test-build";
module.exports = function (grunt) {
if (! grunt.option("dest")) {
grunt.option("dest", "build");
}
var dumpLineNumbers = false;
if (grunt.option("less-line-numbers")) {
grunt.verbose.writeln("Enabling LESS line numbers");
dumpLineNumbers = true;
}
function copyLink(src, dest) {
if (grunt.file.isDir(src)) {
grunt.file.mkdir(dest);
return;
}
var destDir = path.dirname(dest);
if (! grunt.file.exists(destDir)) {
grunt.file.mkdir(destDir);
}
if (! grunt.option("no-hardlink")) {
try {
if (grunt.file.exists(dest)) {
grunt.file.delete(dest);
}
fs.linkSync(src, dest);
} catch (e) {
grunt.file.copy(src, dest);
}
} else {
grunt.file.copy(src, dest);
}
}
function copyMany(src, dest, patterns) {
var paths = grunt.file.expand({cwd: src}, patterns);
paths.forEach(function (p) {
var srcPath = path.join(src, p);
var destPath = path.join(dest, p);
copyLink(srcPath, destPath);
});
}
var libs = [];
grunt.file.expand(
["togetherjs/*.js", "!togetherjs/randomutil.js", "!togetherjs/recorder.js", "!togetherjs/togetherjs.js"]
).forEach(function (filename) {
filename = filename.replace(/^togetherjs\//, "");
filename = filename.replace(/\.js$/, "");
libs.push(filename);
});
var langs = [];
grunt.file.expand("togetherjs/locale/*.json").forEach(function (langFilename) {
var lang = path.basename(langFilename).replace(/\.json/, "");
langs.push(lang);
libs.push("templates-" + lang);
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
less: {
development: {
files: {
"<%= grunt.option('dest') || 'build' %>/togetherjs/togetherjs.css": "togetherjs/togetherjs.less",
"<%= grunt.option('dest') || 'build' %>/togetherjs/recorder.css": "togetherjs/recorder.less"
},
options: {
dumpLineNumbers: dumpLineNumbers
}
}
},
requirejs: {
compile: {
options: {
baseUrl: "togetherjs/",
//paths: requirejsPaths,
include: ["libs/almond"].concat(libs),
//Wrap any build bundle in a start and end text specified by wrap.
//Use this to encapsulate the module code so that define/require are
//not globals. The end text can expose some globals from your file,
//making it easy to create stand-alone libraries that do not mandate
//the end user use requirejs.
wrap: {
start: "(function() {",
end: "TogetherJS.require = TogetherJS._requireObject = require;\nTogetherJS._loaded = true;\nrequire([\"session\"]);\n}());"
},
optimize: "none",
out: function writer(text) {
var dest = path.join(grunt.option("dest"), "togetherjs/togetherjsPackage.js");
grunt.file.write(dest, text);
}
}
}
},
jshint: {
options: {
curly: true,
browser: true,
globals: {
define: true
}
},
all: [
"Gruntfile",
"togetherjs/*.js"
]
},
csslint: {
// Check here for options: https://github.com/stubbornella/csslint/wiki/Rules
options: {
csslintrc: ".csslint.rc"
},
src: [path.join(grunt.option("dest"), "togetherjs/togetherjs.css")]
},
watch: {
main: {
files: ["togetherjs/**/*", "Gruntfile.js"],
tasks: ["build"],
options: {
nospawn: true
}
},
site: {
files: ["togetherjs/**/*", "Gruntfile.js", "site/**/*", "!**/*_flymake*", "!**/*~", "!**/.*"],
tasks: ["build", "buildsite"]
},
// FIXME: I thought I wouldn't have to watch for
// togetherjs/**/*.js, but because the hard links are regularly
// broken by git, this needs to be run often, and it's easy to
// forget. Then between git action the build will be over-run,
// but that's harmless.
minimal: {
files: ["togetherjs/**/*.less", "togetherjs/togetherjs.js", "togetherjs/templates-localized.js",
"togetherjs/**/*.html", "togetherjs/**/*.js", "!**/*_flymake*", "togetherjs/locales/**/*.json"],
tasks: ["build"]
}
},
'http-server': {
'test': {
// the server root directory
root: '.',
cache: 30,
//showDir: true,
//autoIndex: true,
// run in parallel with other tasks
runInBackground: true
}
},
'phantom-tests': grunt.file.expand({
cwd:"togetherjs/tests/"
}, "test_*.js", "func_*.js", "interactive.js", "!test_ot.js").
reduce(function(o, k) { o[k] = {}; return o; }, {})
});
grunt.loadNpmTasks("grunt-contrib-less");
grunt.loadNpmTasks("grunt-contrib-csslint");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.loadNpmTasks("grunt-contrib-requirejs");
grunt.loadNpmTasks("grunt-contrib-watch");
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask("config-requirejs", function() {
// configure the requirejs paths based on the current options
var requirejsPaths = {
jquery: "libs/jquery-1.11.1.min",
walkabout: "libs/walkabout/walkabout",
esprima: "libs/walkabout/lib/esprima",
falafel: "libs/walkabout/lib/falafel",
tinycolor: "libs/tinycolor",
whrandom: "libs/whrandom/random",
jqueryui: "libs/jquery-ui.min",
jquerypunch: "libs/jquery.ui.touch-punch.min",
// Make sure we get the built form of this one:
templates: path.join("..", grunt.option("dest"), "togetherjs/templates")
};
langs.forEach(function(lang) {
requirejsPaths["templates-" + lang] =
path.join("..", grunt.option("dest"), "togetherjs", "templates-" + lang);
});
grunt.config.merge({
requirejs: {
compile: {
options: {
paths: requirejsPaths
}
}
}
});
grunt.task.run("requirejs");
});
grunt.registerTask("copylib", "copy the library", function () {
var pattern = ["**", "!togetherjs.js", "!templates-localized.js", "!**/*.less", "!#*", "!**/*_flymake*", "!**/*.md", "!**/*.tmp", "!**/#*"];
grunt.log.writeln("Copying files from " + "togetherjs/".cyan + " to " + path.join(grunt.option("dest"), "togetherjs").cyan);
if (grunt.option("exclude-tests")) {
pattern.push("!tests/");
pattern.push("!tests/**");
grunt.log.writeln(" (excluding tests)");
}
copyMany(
"togetherjs/", path.join(grunt.option("dest"), "togetherjs"),
pattern
);
});
grunt.registerTask("copysite", "copy the site (not library)", function () {
grunt.log.writeln("Copying files from " + "site/".cyan + " to " + grunt.option("dest").cyan);
copyMany(
"site/", grunt.option("dest"),
["**", "!**/*.tmpl", "!**/*.html", "!public/**", "!**/*_flymake*", "!**/*.md"]);
copyMany(
"site/public/", grunt.option("dest"),
["**"]);
});
grunt.registerTask("build", ["copylib", "maybeless", "substitute", "config-requirejs"]);
grunt.registerTask("buildsite", ["copysite", "render", "rendermd", "docco"]);
grunt.registerTask("devwatch", ["build", "watch:minimal"]);
// For some reason doing ["build", "buildsite", "watch:site"]
// doesn't work, it gets through buildsite and doesn't watch;
// instead just doing watch:site seems okay:
grunt.registerTask("sitewatch", ["buildsite", "watch:site"]);
function escapeString(s) {
if (typeof s != "string") {
throw new Error("Not a string: " + s);
}
var data = JSON.stringify(s);
return data.substr(1, data.length-2);
}
grunt.registerTask(
"substitute",
"Substitute templates-localized.js and parameters in togetherjs.js",
function () {
// FIXME: I could use grunt.file.copy(..., {process: function (content, path) {}}) here
var baseUrl = grunt.option("base-url") || ""; // baseURL to be entered by the user
if (! baseUrl) {
grunt.log.writeln("No --base-url, using auto-detect");
}
var destBase = grunt.option("dest") || "build"; // where to put the built files. If not indicated then into build/
var hubUrl = grunt.option("hub-url") || process.env.HUB_URL || "https://hub.togetherjs.com"; // URL of the hub server
grunt.log.writeln("Using hub URL " + hubUrl.cyan);
var gitCommit = process.env.GIT_COMMIT || "";
var subs = {
__interface_html__: grunt.file.read("togetherjs/interface.html"),
__help_txt__: grunt.file.read("togetherjs/help.txt"),
__walkthrough_html__: grunt.file.read("togetherjs/walkthrough.html"),
__baseUrl__: baseUrl,
__hubUrl__: hubUrl,
__gitCommit__: gitCommit
};
function substituteContent(content, s) {
for (var v in s) {
var re = new RegExp(v, "g");
if (typeof s[v] != "string") {
grunt.log.error("Substitution variable " + v.cyan + " is not a string")
}
content = content.replace(re, escapeString(s[v]));
}
return content;
}
var filenames = {
"togetherjs.js": {
src: "togetherjs/togetherjs.js",
extraVariables: {__min__: "no"}
},
"togetherjs-min.js": {
src: "togetherjs/togetherjs.js",
extraVariables: {__min__: "yes"}
}
};
for (var dest in filenames) {
var info = filenames[dest];
var src = info.src;
var extraVariables = info.extraVariables;
dest = destBase + "/" + dest;
var content = fs.readFileSync(src, "UTF-8");
var s = subs;
if (extraVariables) {
s = Object.create(subs);
for (var a in extraVariables) {
s[a] = extraVariables[a];
}
}
content = substituteContent(content, s);
grunt.log.writeln("writing " + src.cyan + " to " + dest.cyan);
grunt.file.write(dest, content);
}
grunt.file.expand("togetherjs/locale/*.json").forEach(function (langFilename) {
var templates = grunt.file.read("togetherjs/templates-localized.js");
var lang = path.basename(langFilename).replace(/\.json/, "");
var translation = JSON.parse(grunt.file.read(langFilename));
var dest = path.join(grunt.option("dest"), "togetherjs/templates-" + lang + ".js");
var translatedInterface = translateFile("togetherjs/interface.html", translation);
var translatedHelp = translateFile("togetherjs/help.txt", translation);
var translatedWalkthrough = translateFile("togetherjs/walkthrough.html", translation);
var vars = subs;
subs.__interface_html__ = translatedInterface;
subs.__help_txt__ = translatedHelp;
subs.__walkthrough_html__ = translatedWalkthrough;
subs.__names__ = translation.names;
templates = substituteContent(templates, subs);
grunt.file.write(dest, templates);
grunt.log.writeln("writing " + dest.cyan + " based on " + langFilename.cyan);
});
return true;
}
);
function translateFile(source, translation) {
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader("./"));
var tmpl = env.getTemplate(source);
return tmpl.render({
gettext: function (string) {
return translation[string] || string;
}
});
}
grunt.registerTask("maybeless", "Maybe compile togetherjs.less", function () {
var sources = grunt.file.expand(["togetherjs/**/*.less", "site/**/*.less"]);
var found = false;
sources.forEach(function (fn) {
var source = fs.statSync(fn);
var destFn = grunt.option("dest") + "/" + fn.substr(0, fn.length-4) + "css";
if (! fs.existsSync(destFn)) {
found = true;
return;
}
var dest = fs.statSync(destFn);
if (source.mtime.getTime() > dest.mtime.getTime()) {
grunt.log.writeln("Destination LESS out of date: " + destFn.cyan);
found = true;
}
});
if (found) {
grunt.task.run("less");
} else {
grunt.log.writeln("No .less files need regenerating.");
}
});
grunt.registerTask("render", "Render the site", function () {
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader("site/"));
var sources = grunt.file.expand({cwd: "site/"}, "**/*.html");
sources.forEach(function (source) {
var dest = grunt.option("dest") + "/" + source;
grunt.log.writeln("Rendering " + source.cyan + " to " + dest.cyan);
var data = grunt.file.read("site/" + source);
var tmplVars = Object.create(vars);
while (true) {
var match = /\{\#\s+set\s+([^\s]+)\s+([^#]+)\s*\#\}/.exec(data);
if (! match) {
break;
}
tmplVars[match[1]] = JSON.parse(match[2]);
grunt.log.writeln(" Found variable " + match[1] + " = " + match[2]);
data = data.substr(match.index + match[0].length);
}
tmplVars.base = path.relative(path.dirname("site/" + source), "site/");
if (tmplVars.base && tmplVars.base.search(/\/$/) == -1) {
tmplVars.base += "/";
}
if (tmplVars.absoluteLinks) {
tmplVars.base = "/";
}
tmplVars.base = tmplVars.base.replace(/\\/g, '/');
var tmpl = env.getTemplate(source);
var result = tmpl.render(tmplVars);
grunt.file.write(dest, result);
});
});
function parseMarkdownOutput(doc) {
var title = (/<h1[^>]*>(.*)<\/h1>/i).exec(doc);
title = title[1];
var body = doc.replace(/<h1[^>]*>.*<\/h1>/i, "");
return {
title: title,
body: body
};
}
function addHeaderIds(doc) {
var result = [];
while (doc) {
var match = (/(<h\d)>(.*)(<\/h\d>)/i).exec(doc);
if (! match) {
result.push(doc);
break;
}
var id = match[2];
id = id.toLowerCase();
id = id.replace(/ +/g, "-");
id = id.replace(/[^a-z0-9_\-]/g, "");
var header = match[1] + ' id="' + id + '">' + match[2] + match[3];
result.push(doc.substr(0, match.index));
result.push(header);
doc = doc.substr(match.index + match[0].length);
}
return result.join("");
}
function highlight(code, lang) {
var hjs = require("highlight.js");
var aliases = {
html: "xml",
js: "javascript"
};
lang = aliases[lang] || lang;
try {
if (lang) {
return hjs.highlight(lang, code).value;
} else {
return hjs.highlightAuto(code).value;
}
} catch (e) {
grunt.fail.fatal("Error highlighting: " + e);
throw e;
}
}
marked.setOptions({highlight: highlight});
grunt.registerTask("rendermd", "Render the site Markdown files", function () {
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader("site/"));
var sources = grunt.file.expand({cwd: "site/"}, "**/*.md", "!**/README.md");
sources.forEach(function (source) {
var basename = source.replace(/\.md$/, "");
var dest = grunt.option("dest") + "/" + basename + ".html";
grunt.log.writeln("Rendering " + source.cyan + " to " + dest.cyan);
var data = grunt.file.read("site/" + source);
var templateName = "generic-markdown.tmpl";
var match = (/template:\s*([a-zA-Z_\-0-9.]*)/).exec(data);
if (match) {
templateName = match[1];
}
var html = marked(data, {
smartypants: true
});
var parsed = parseMarkdownOutput(html);
parsed.body = addHeaderIds(parsed.body);
var tmpl = env.getTemplate(templateName);
var tmplVars = Object.create(vars);
tmplVars.markdownBody = parsed.body;
tmplVars.title = parsed.title;
tmplVars.base = path.relative(path.dirname("site/" + source), "site/");
if (tmplVars.base && tmplVars.base.search(/\/$/) == -1) {
tmplVars.base += "/";
}
tmplVars.base = tmplVars.base.replace(/\\/g, '/');
var result = tmpl.render(tmplVars);
grunt.file.write(dest, result);
});
});
function doccoFormat(source, sections) {
sections.forEach(function (section) {
var code = highlight(section.codeText, "javascript");
code = code.replace(/\s+$/, '');
section.codeHtml = "<div class='highlight'><pre>" + code + "</pre></div>";
section.docsHtml = marked(section.docsText);
});
}
grunt.registerTask("docco", "Create comment-separating source code", function () {
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader("site/"));
var sources = grunt.file.expand({cwd: "togetherjs/"}, "*.js");
sources.sort();
var sourceDescriptions = JSON.parse(grunt.file.read("togetherjs/module-descriptions.json"));
var sourceList = [];
sources.forEach(function (source) {
var name = source.replace(/\.js$/, "");
sourceList.push({
name: name,
link: source + ".html",
description: marked(sourceDescriptions[name] || "", {smartypants: true})
});
});
sources.forEach(function (source) {
var sourceName = source.replace(/\.js$/, "");
var dest = grunt.option("dest") + "/source/" + source + ".html";
grunt.log.writeln("Rendering " + source.cyan + " to " + dest.cyan);
var code = grunt.file.read("togetherjs/" + source);
var sections = docco.parse(source, code, {languages:{}});
doccoFormat(source, sections);
sections.forEach(function (section, i) {
section.index = i;
section.empty = section.codeText.replace(/\s/gm, "") === "";
});
var first = marked.lexer(sections[0].docsText)[0];
var hasTitle = first && first.type == 'heading' && first.depth == 1;
var title = hasTitle ? first.text : path.basename(source, ".js");
var tmpl = env.getTemplate("source-code.tmpl");
var tmplVars = Object.create(vars);
tmplVars.title = title;
tmplVars.sections = sections;
tmplVars.source = source;
tmplVars.sourceName = sourceName;
tmplVars.sourceDescription = marked(sourceDescriptions[sourceName] || "", {smartypants: true});
tmplVars.base = "../";
tmplVars.sourceList = sourceList;
var result = tmpl.render(tmplVars);
grunt.file.write(dest, result);
});
var tmplVars = Object.create(vars);
tmplVars.title = "TogetherJS Source Code";
tmplVars.sourceList = sourceList;
tmplVars.base = "../";
var tmpl = env.getTemplate("source-code-index.tmpl");
grunt.file.write(grunt.option("dest") + "/source/index.html", tmpl.render(tmplVars));
});
grunt.registerTask("buildaddon", "Build the Firefox addon and move the XPI into the site", function () {
var done = this.async();
grunt.util.spawn({
cmd: "cfx",
args: ["xpi"],
opts: {
cwd: "addon/"
}
}, function (error, result, code) {
if (error) {
grunt.log.error("Error running cfx xpi: " + error.toString().cyan);
grunt.fail.fatal("Error creating XPI");
done();
return;
}
var dest = path.join(grunt.option("dest"), "togetherjs.xpi");
grunt.file.copy("addon/togetherjs.xpi", dest);
grunt.log.writeln("Created " + dest.cyan);
done();
});
});
grunt.registerTask("publish", "Publish to togetherjs.mozillalabs.com/public/", function () {
if (! grunt.file.isDir("togetherjs.mozillalabs.com")) {
grunt.log.writeln("Error: you must check out togetherjs.mozillalabs.com");
grunt.log.writeln("Use:");
grunt.log.writeln(" $ git clone -b togetherjs.mozillalabs.com git:git@github.com:mozilla/togetherjs.git togetherjs.mozillalabs.com");
grunt.log.writeln(" $ cd togetherjs.mozillalabs.com/.git");
grunt.log.writeln(" $ echo '[remote \"staging\"]\n\turl = git@heroku.com:togetherjs-staging.git\n\tpush = refs/heads/togetherjs.mozillalabs.com:refs/heads/master\n[remote \"production\"]\n\turl = git@heroku.com:togetherjs.git\n\tpush = refs/heads/togetherjs.mozillalabs.com:refs/heads/master\n' >> config");
grunt.fail.fatal("Must checkout togetherjs.mozillalabs.com");
return;
}
var versions = "togetherjs.mozillalabs.com/public/versions";
if (! grunt.file.isDir(versions)) {
grunt.log.writeln("Error: " + versions.cyan + " does not exist");
grunt.fail.fatal("No versions/ directory");
return;
}
var tmp = "togetherjs.mozillalabs.com/public_versions_tmp";
fs.rename(versions, tmp);
grunt.file.delete("togetherjs.mozillalabs.com/public");
grunt.file.mkdir("togetherjs.mozillalabs.com/public");
fs.rename(tmp, versions);
if (! grunt.option("base-url")) {
grunt.option("base-url", "https://togetherjs.com");
}
grunt.option("dest", "togetherjs.mozillalabs.com/public");
grunt.option("exclude-tests", true);
grunt.option("no-hardlink", true);
grunt.task.run(["build", "buildsite", "buildaddon"]);
grunt.task.run(["movecss"]);
grunt.log.writeln("To actually publish you must do:");
grunt.log.writeln(" $ cd togetherjs.mozillalabs.com/");
grunt.log.writeln(" $ git add -A");
grunt.log.writeln(" $ git commit -a -m 'Publish'");
grunt.log.writeln(" $ git push && git push staging");
});
grunt.registerTask("publishversion", "Publish to togetherjs.mozillalabs.com/public/versions/", function () {
var version = grunt.option("togetherjs-version");
if (! version) {
grunt.log.error("You must provide a --togetherjs-version=X.Y argument");
grunt.fail.fatal("No --togetherjs-version");
return;
}
if (! grunt.file.isDir("togetherjs.mozillalabs.com/public/versions")) {
grunt.log.error("The directory togetherjs.mozillalabs.com/public/versions does not exist");
grunt.fail.fatal();
return;
}
var destDir = "togetherjs.mozillalabs.com/public/versions/" + version;
if (grunt.file.exists(destDir)) {
grunt.log.error("The directory " + destDir + " already exists");
grunt.log.error(" Delete it first to re-create version");
grunt.fail.fatal();
return;
}
grunt.option("base-url", "https://togetherjs.com/versions/" + version);
grunt.option("dest", destDir);
grunt.option("exclude-tests", true);
grunt.option("no-hardlink", true);
grunt.task.run(["build"]);
grunt.task.run(["movecss"]);
var readme = grunt.file.read("togetherjs.mozillalabs.com/public/versions/README.md");
readme += " * [" + version + "](./" + version + "/togetherjs.js)\n";
grunt.file.write("togetherjs.mozillalabs.com/public/versions/README.md", readme);
});
grunt.registerTask("movecss", "Publish generated css files to dest", function () {
// Can't figure out how to parameterize the less task, hence this lame move
["togetherjs/togetherjs.css", "togetherjs/recorder.css"].forEach(function (css) {
var src = path.join("build", css);
var dest = path.join(grunt.option("dest"), css);
grunt.file.copy(src, dest);
grunt.log.writeln("Copying " + src.cyan + " to " + dest.cyan);
});
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.registerTask('dev', function() {
grunt.util.spawn({
cmd: 'node',
args: ['devserver.js']
});
grunt.task.run('watch');
});
grunt.registerTask("test", "Run jshint and test suite", ["jshint", "phantom"]);
grunt.loadNpmTasks('grunt-http-server');
grunt.registerTask("phantom", ["phantom-setup", "phantom-tests"]);
grunt.registerTask("phantom-setup", "Run jdoctest test suite in phantomjs",
function() {
var done = this.async();
// find unused ports for web server and hub
var freeport = require("freeport");
freeport(function(err1, hubPort) {
freeport(function(err2, webPort) {
if (err1 || err2) { return done(err1 || err2); }
// build togetherjs using these default ports
grunt.option("base-url", "http://localhost:"+webPort+"/"+TESTDIR+"/");
grunt.option("hub-url", "http://localhost:"+hubPort);
grunt.option("no-hardlink", true);
grunt.option("dest", TESTDIR);
// make sure the web server will use the right port
grunt.config.merge({
'http-server': {
test: {
port: webPort,
host: "localhost"
}
}
});
// spawn a hub, using the hub port
var hub = require("./hub/server");
hub.startServer(hubPort, "localhost");
// build & start the web server
grunt.task.run("build", "http-server:test");
// ok, now we can run the tests in phantomjs!
done();
});
});
});
// PhantomJS event handlers
var phantomjs = require("grunt-lib-phantomjs").init(grunt);
var phantomStatus;
phantomjs.on('fail.load', function(url) {
phantomjs.halt();
grunt.verbose.write('Running PhantomJS...').or.write('...');
grunt.log.error('PhantomJS unable to load "' + url + '" URI.');
phantomStatus.failed += 1;
phantomStatus.total += 1;
});
phantomjs.on('fail.timeout', function() {
phantomjs.halt();
grunt.log.writeln();
grunt.log.error('PhantomJS timed out.');
phantomStatus.failed += 1;
phantomStatus.total += 1;
});
phantomjs.on('doctestjs.pass', function(result) {
phantomStatus.total += 1;
grunt.verbose.ok("Passed: "+result.example.summary);
});
phantomjs.on('doctestjs.fail', function(result) {
phantomStatus.failed += 1;
phantomStatus.total += 1;
grunt.log.error("Failed: "+result.example.expr);
grunt.log.subhead("Expected:");
grunt.log.writeln(result.example.expected);
grunt.log.subhead("Got:");
grunt.log.writeln(result.got);
});
phantomjs.on('doctestjs.end', function() {
phantomjs.halt();
});
// Pass through console.log statements (when verbose)
phantomjs.on('console', grunt.verbose.writeln);
grunt.registerMultiTask("phantom-tests", function() {
grunt.task.requires('phantom-setup');
var url = grunt.option('base-url') +
"togetherjs/tests/index.html?name=" + this.target;
grunt.verbose.writeln("Running tests at: "+url);
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
// PhantomJS timeout, in ms.
timeout: 10000,
// JDoctest-PhantomJS bridge file to be injected.
inject: path.join(__dirname, 'phantomjs', 'bridge.js'),
//screenshot: true,
page: {
// leave room for the togetherjs sidebar
viewportSize: { width: 1024, height: 1024 }
}
});
// Reset test status
phantomStatus = {failed: 0, passed: 0, total: 0, start: Date.now()};
// Start phantomjs on this URL
var done = this.async();
phantomjs.spawn(url, {
options: options,
done: function() {
var duration = Date.now() - phantomStatus.start;
// Log results.
if (phantomStatus.failed > 0) {
grunt.warn(phantomStatus.failed + '/' + phantomStatus.total +
' assertions failed (' + duration + 'ms)');
} else if (phantomStatus.total === 0) {
grunt.warn('0/0 assertions ran (' + duration + 'ms)');
} else {
grunt.verbose.writeln();
grunt.log.ok(phantomStatus.total + ' assertions passed (' + duration + 'ms)');
}
// All done!
done();
}
});
});
grunt.registerTask('default', 'start');
};