This repository has been archived by the owner on May 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
build.js
executable file
·670 lines (611 loc) · 20.2 KB
/
build.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
#!/usr/bin/env node
/* JSON 3 Builder | https://bestiejs.github.io/json3 */
var fs = require("fs"),
https = require("https"),
path = require("path"),
spawn = require("child_process").spawn,
url = require("url"),
util = require("util"),
zlib = require("zlib");
var vendorPath = path.join(__dirname, "vendor"),
highlightAuto = require("highlight.js").highlightAuto,
marked = require("marked"),
package = require(path.join(__dirname, "package.json")),
tar = require("tar");
// The path to the Closure Compiler `.jar` file and ETag.
var closurePath = path.join(vendorPath, "closure-compiler.jar"),
closureETag = path.join(vendorPath, "closure-compiler-etag.txt");
// The path to the project page template.
var pageTemplatePath = path.join(path.join(__dirname, "page", "page.html"));
var indexPageSrc = path.join(__dirname, "README.md"),
contribPageSrc = path.join(__dirname, "CONTRIBUTING.md"),
relPageSrc = path.join(__dirname, "CHANGELOG.md");
var indexPageDest = path.join(__dirname, "index.html"),
contribPageDest = path.join(__dirname, "contribute.html"),
relPageDest = path.join(__dirname, "changes.html");
// The Closure Compiler options: enable advanced optimizations and suppress all
// warnings apart from syntax and optimization errors.
var closureOptions = ["--compilation_level=ADVANCED_OPTIMIZATIONS", "--warning_level=QUIET"];
// A RegExp used to detect the `define` pragma used by asynchronous module
// loaders.
var definePattern = RegExp('(?:' +
// `typeof define == "function"`. Matches `==` and `===`; `'` and `"`.
'typeof\\s+define\\s*===?\\s*([\'"])function\\1|' +
// `"function" == typeof define`. Same rules as above.
'([\'"])function\\2\\s*===?\\s*typeof\\s+define' +
')' +
// `&&`.
'\\s*&&\\s*(?:' +
// `!!` (optional Boolean coercion)
'(?:!!)?' +
// `define`.
'define\\s*(?:' +
// `.amd`.
'\\.\\s*amd|' +
// `["amd"]` | `['amd']`.
'\\[\\s*([\'"])amd\\3\\s*\\]' +
')|' +
'(?:' +
'(?:' +
// `typeof define.amd`.
'typeof\\s+define\\.\\s*amd|' +
// `typeof define["amd"]` or `typeof define['amd']`.
'typeof\\s+define\\[\\s*([\'"])amd\\4\\s*\\]' +
')' +
// `=== "object"`. Same rules for quotes and equality operators.
'\\s*===?\\s*([\'"])object\\5' +
'|' +
// `"object" ===`.
'([\'"])object\\6\\s*===?\\s*' +
'(?:' +
'typeof\\s+define\\.\\s*amd|' +
'typeof\\s+define\\[\\s*([\'"])amd\\7\\s*\\]' +
')' +
')' +
'(?:' +
// `&&` (optional Boolean test for `define.amd`).
'\\s*&&\\s*' +
'(?:' +
// `!!` (optional Boolean coercion)
'(?:!!)?' +
// `define.amd`.
'define\\.\\s*amd|' +
// `define["amd"] | define['amd']`.
'define\\[\\s*([\'"])amd\\8\\s*\\]' +
')' +
')?' +
')', 'g');
// List of properties to prevent the Closure Compiler from minifying.
var propertyWhitelist = [
'Date',
'JSON',
'JSON3',
'Math',
'Number',
'Object',
'String',
'SyntaxError',
'TypeError',
'global',
'parse',
'runInContext',
'self',
'stringify',
'window'
];
function PageRenderer(options) {
marked.Renderer.call(this);
if (options) {
this.issueFormatters = options.issueFormatters;
this.imgClasses = options.imgClasses;
this.linkClasses = options.linkClasses;
}
}
util.inherits(PageRenderer, marked.Renderer);
PageRenderer.issuePattern = /(\s*|[\[])\s*(issue|pr)\s*#(\d+)([;\]]|\s*)/gi;
PageRenderer.prototype.issueFormatters = null;
PageRenderer.prototype.imgClasses = null;
PageRenderer.prototype.linkClasses = null;
PageRenderer.prototype.listitem = function listitem(text) {
var self = this;
if (this.issueFormatters) {
text = text.replace(PageRenderer.issuePattern, function linkIssue(match, pre, type, id, post) {
var getURI = self.issueFormatters[type.toLowerCase()];
if (!getURI) {
return match;
}
return pre + util.format('<a href="%s">#%s</a>',
getURI(id), id) + post;
});
}
return marked.Renderer.prototype.listitem.call(this, text);
};
PageRenderer.prototype.image = function image(src, title, alt) {
var attrs = [util.format('src="%s" alt="%s"', src, alt)];
if (title) {
attrs.push(util.format('title="%s"', title));
}
if (this.imgClasses) {
var className = this.imgClasses[path.basename(src)];
if (className) {
attrs.push(util.format('class="%s"', className));
}
}
return util.format('<img %s>', attrs.join(" "));
};
PageRenderer.prototype.link = function link(href, title, text) {
var uri = url.parse(href);
if (this.options && this.options.sanitize && uri.protocol == "javascript") {
return '';
}
var attrs = [util.format('href="%s"', href)];
if (title) {
attrs.push(util.format('title="%s"', title));
}
if (this.linkClasses) {
var className = this.linkClasses[uri.hostname];
if (className) {
attrs.push(util.format('class="%s"', className));
}
}
return util.format("<a %s>%s</a>", attrs.join(" "), text);
};
marked.setOptions({
"smartypants": true,
"highlight": function highlight(source) {
return highlightAuto(source).value;
}
});
main();
function main() {
genIndex();
genContrib();
genRel();
minify();
}
function genIndex() {
var r = new PageRenderer({
imgClasses: { "logo.png": "logo" },
linkClasses: { "travis-ci.org": "travis-ci" }
});
genPage("JSON 3", indexPageDest, indexPageSrc,
{ "renderer": r }, function afterGen(err) {
console.log(err || "GitHub project page generated successfully.");
});
}
function genContrib() {
var r = new PageRenderer();
genPage("Contribute | JSON 3", contribPageDest, contribPageSrc,
{ "renderer": r }, function afterGen(err) {
console.log(err || "Contributing page generated successfully.");
});
}
function genRel() {
var r = new PageRenderer({
issueFormatters: {
issue: function formatIssue(id) {
return "https://github.com/bestiejs/json3/issues/" + id;
},
pr: function formatPR(id) {
return "https://github.com/bestiejs/json3/pull/" + id;
}
}
});
genPage("Releases | JSON 3", relPageDest, relPageSrc,
{"renderer": r }, function afterGen(err) {
console.log(err || "Change log generated successfully.");
});
}
// Compress JSON 3 using the Closure Compiler.
function minify() {
getCompiler(function hasCompiler(error) {
if (error) {
console.log(error);
return;
}
fs.readFile(path.join(__dirname, "lib", "json3.js"), "utf8", function readSource(error, source) {
if (error) {
console.log(error);
return;
}
console.log("Development version size: %d KB.", Math.round(Buffer.byteLength(source) / 1024 * 100) / 100);
// Shell out to the Closure Compiler. Requires Java 7 or higher.
var error = [], errorLength = 0;
var output = [], outputLength = 0;
var compiler = spawn("java", ["-jar", closurePath].concat(closureOptions));
compiler.stdout.on("data", function onData(data) {
// Append the data to the output stream.
output.push(data);
outputLength += data.length;
});
compiler.stderr.on("data", function onError(data) {
// Append the error message to the error stream.
error.push(data);
errorLength += data.length;
});
compiler.on("exit", function onExit(status) {
var exception;
// `status` specifies the process exit code.
if (status) {
exception = new Error(Buffer.concat(error, errorLength));
exception.status = status;
}
compressSource(exception, '' + Buffer.concat(output, outputLength));
});
// Proxy the preprocessed source to the Closure Compiler.
compiler.stdin.end(preprocessSource(source));
// Post-processes the compressed source and writes the result to disk.
function compressSource(exception, compressed) {
if (exception) {
console.log(exception);
} else {
// Extract the JSON 3 header and clean up the minified source.
compressed = extractComments(source)[0] + '\n' + postprocessSource(compressed);
// Write the compressed version to disk.
fs.writeFile(path.join(__dirname, "lib", "json3.min.js"), compressed, writeSource);
}
// Checks the `gzip`-ped size of the compressed version.
function writeSource(exception) {
console.log(exception || "Compressed version generated successfully.");
zlib.gzip(compressed, function (exception, results) {
console.log("Compressed version size: %d KB.", Math.round(results.length / 1024 * 100) / 100);
});
}
}
});
});
}
// Internal: Extracts line and block comments from a JavaScript `source`
// string. Returns an array containing the comments.
function extractComments(source) {
var index = 0, length = source.length, results = [], symbol, position, original;
while (index < length) {
symbol = source[index];
switch (symbol) {
// Parse line and block comments.
case "/":
original = symbol;
symbol = source[++index];
switch (symbol) {
// Extract line comments.
case "/":
position = source.indexOf("\n", index);
if (position < 0) {
// Check for CR line endings.
position = source.indexOf("\r", index);
}
results.push(original + source.slice(index, index = position < 0 ? length : position));
break;
// Extract block comments.
case "*":
position = source.indexOf("*/", index);
if (position < 0) {
throw SyntaxError("Unterminated block comment.");
}
// Advance past the end of the comment.
results.push(original + source.slice(index, index = position += 2));
break;
default:
index++;
}
break;
// Parse strings separately to ensure that any JavaScript comments within
// them are preserved.
case '"':
case "'":
for (position = index, original = symbol; index < length;) {
symbol = source[++index];
if (symbol == "\\") {
// Skip past escaped characters.
index++;
} else if ("\n\r\u2028\u2029".indexOf(symbol) > -1) {
// According to the ES 5.1 spec, strings may not contain unescaped
// line terminators.
throw SyntaxError("Illegal line continuation.");
} else if (symbol == original) {
break;
}
}
if (source[index] == original) {
index++;
break;
}
throw SyntaxError("Unterminated string.");
default:
// Advance to the next character.
index++;
}
}
return results;
}
function preprocessSource(source) {
source = source.replace(definePattern, 'typeof define === "function" && define["amd"]');
// Add brackets to whitelisted properties so the Closure Compiler won't minify them.
// https://developers.google.com/closure/compiler/docs/api-tutorial3#export
return source.replace(RegExp('(["\'])(?:(?!\\1)[^\\n\\\\]|\\\\.)*\\1|\\.(' + propertyWhitelist.join('|') + ')\\b', 'g'), function(match, quote, prop) {
return quote ? match : "['" + prop + "']";
});
}
function postprocessSource(source) {
// Shift variables in the global scope into the IIFE and fix the
// `define` pragma.
var result = source.replace(/^(var [^;]*;)\s*(\(function\([^)]*\)\{)/m, '\n;$2$1');
return result.replace(definePattern, 'typeof define==="function"&&define.amd');
}
// Internal: Lazily downloads the Closure Compiler.
function getCompiler(callback) {
var hasCompiler = false,
isFinalized = false,
eTag;
var prePending = 2,
preDone = false;
var postPending = 1,
postDone = false;
// Step one: determine if the Closure Compiler has already been downloaded.
fs.stat(closurePath, function getStats(error, stats) {
if (error) {
if (error.code != "ENOENT") {
return updatePre(error);
}
return updatePre();
}
if (!stats.isFile()) {
return updatePre(new Error(util.format("`%s` must be a file.", closurePath)));
}
hasCompiler = true;
console.log('The Closure Compiler has already been downloaded.');
updatePre();
});
// Step two: Retrieve the locally-cached `ETag` from the previous download.
fs.readFile(closureETag, "utf8", function readETag(error, source) {
if (error) {
if (error.code != "ENOENT") {
return updatePre(error);
}
return updatePre();
}
eTag = source;
updatePre();
});
// Step three: download the Closure Compiler.
function download() {
var headers = {
"user-agent": util.format("JSON/%s", package.version)
};
if (eTag && hasCompiler) {
headers["if-none-match"] = eTag;
}
var request = https.request({
"hostname": "dl.google.com",
"port": 443,
"path": "/closure-compiler/compiler-latest.tar.gz",
"headers": headers,
"agent": false // Disable keep-alive.
});
request.on("response", onResponse);
function onResponse(response) {
request.removeListener("response", onResponse);
request.removeListener("error", onError);
if (response.statusCode == 304) {
// If the cached `ETag` matches that of the entity, there is no
// need to download the Compiler tarball or extract the `.jar`.
// Skip all post-processing steps.
console.log('No updates available.');
return finalize();
}
// Step four: write the new `ETag` to disk.
var eTag = response.headers.etag;
saveETag(eTag);
// Step five: extract the Compiler `.jar` from the downloaded
// tarball using `node-tar`'s streaming `.tar` parser.
var parser = new tar.Parse();
parser.on("entry", onEntry);
function onEntry(entry) {
if (path.extname(entry.path) != ".jar") {
return;
}
parser.removeListener("entry", onEntry);
// Step six: write the Compiler to disk.
console.log('Extracting the Closure Compiler...');
var writeStream = fs.createWriteStream(closurePath);
writeStream.on("close", onClose);
function onClose() {
writeStream.removeListener("close", onClose);
writeStream.removeListener("error", onError);
// The Compiler has been successfully downloaded.
hasCompiler = true;
updatePost();
}
writeStream.on("error", onError);
function onError(error) {
writeStream.removeListener("close", onClose);
writeStream.removeListener("error", onError);
updatePost(error);
}
entry.pipe(writeStream);
}
parser.on("error", onError);
function onError(error) {
parser.removeListener("entry", onEntry);
parser.removeListener("error", onError);
parser.removeListener("end", onEnd);
updatePost(error);
}
// Clean up attached event handlers.
parser.on("end", onEnd);
function onEnd() {
parser.removeListener("entry", onEntry);
parser.removeListener("error", onError);
parser.removeListener("end", onEnd);
}
// Begin downloading the Compiler tarball.
console.log('Downloading the Closure Compiler...');
response.pipe(zlib.createGunzip()).pipe(parser);
}
request.on("error", onError);
function onError(error) {
request.removeListener("response", onResponse);
request.removeListener("error", onError);
// Connection errors are not fatal, as it is possible to use the
// previously-downloaded copy of Closure Compiler. If a cached copy
// is not available, the `hasCompiler` flag will be set to `false`,
// and `finalize` will yield an error to the `callback`.
console.log('A new version of the Closure Compiler could not be downloaded.');
updatePost();
}
console.log('Updating the Closure Compiler...');
request.end();
}
function finalize(error) {
if (isFinalized) {
return;
}
isFinalized = true;
if (error) {
return callback(error);
}
if (!hasCompiler) {
return callback(new Error("The Closure Compiler is required to build JSON 3."));
}
callback();
}
function saveETag(eTag) {
postPending++;
fs.writeFile(closureETag, eTag, function writeETag(error) {
if (error) {
// This error is not fatal. If the write fails, the Compiler will be
// downloaded again when `getCompiler` is called.
console.log("The Closure Compiler `ETag` could not be written to disk.");
}
updatePost();
});
}
function updatePre(error) {
if (preDone) {
return;
}
if (error) {
preDone = true;
return finalize(error);
}
prePending--;
if (!prePending) {
preDone = true;
download();
}
}
function updatePost(error) {
if (postDone) {
return;
}
if (error) {
postDone = true;
return finalize(error);
}
postPending--;
if (!postPending) {
postDone = true;
finalize();
}
}
}
// Generates the navigation section for a Markdown document.
// Ported from `mdtoc.rb` by Sam Stephenson.
function genNav(source) {
var headers = [];
var lines = source.split(/\r?\n/);
// First pass: Scan the Markdown source looking for titles of the format:
// `### Title ###`. Record the line number, header level (number of
// octothorpes), and text of each matching title.
lines.forEach(function (line, index) {
var match = /^(\#{1,6})\s+(.+?)\s+\1$/.exec(line);
if (match) {
headers.push([index, match[1].length, match[2]]);
}
});
// Second pass: Iterate over all matched titles and compute their
// corresponding section numbers. Then replace the titles with annotated
// anchors.
var lastSection, lastLevel;
headers.forEach(function (value) {
var index = value[0], level = value[1], text = value[2], section, length;
if (lastSection) {
// Clone the last section metadata array.
section = lastSection.slice(0);
if (lastLevel < level) {
section.push(1);
} else {
length = lastLevel - level;
while (length--) {
section.pop();
}
section[section.length - 1] += 1;
}
} else {
section = [1];
}
lines[index] = Array(level + 1).join("#") + " " + text + "<a name=\"section_" + section.join(".") + "\"></a>";
value.push(section);
lastSection = section;
lastLevel = level;
});
// Third pass: Iterate over matched titles once more to produce the table of
// contents.
var navigation = headers.map(function (value) {
var index = value[0], level = value[1], text = value[2], section = value[3], name = section.join(".");
return "<li><a href=\"#section_" + name + "\">" + text + "</a></li>";
});
navigation.push("");
return {
"navigation": navigation,
"lines": lines
};
}
function buildPage(title, template, src, options) {
var parts = genNav(src);
return template.replace(/<%=\s*(.+?)\s*%>/g, function interpolate(match, section) {
if (section == "title") {
return title;
}
if (section == "navigation") {
// Insert the table of contents directly into the template.
return parts.navigation.join("\n");
}
if (section == "source") {
// Convert the page to HTML and insert it into the body.
return marked(parts.lines.join("\n"), options);
}
return "";
});
}
// Generates a GitHub project page using the template.
function genPage(title, destPath, srcPath, options, callback) {
var pendingReads = 2;
var contents = { "src": null, "tmpl": null };
function afterRead(err, content, prop) {
if (pendingReads === 0) {
return;
}
if (err) {
pendingReads = 0;
callback(err);
return;
}
pendingReads--;
contents[prop] = content;
if (pendingReads > 0) {
return;
}
// Interpolate the page template.
var page = buildPage(title, contents.tmpl, contents.src, options);
// Write the page to disk.
fs.writeFile(destPath, page, callback);
}
fs.readFile(srcPath, "utf8", function afterReadSrc(err, src) {
afterRead(err, src, "src");
});
fs.readFile(pageTemplatePath, "utf8",
function afterReadTemplate(err, template) {
afterRead(err, template, "tmpl");
});
}