This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
forked from download13/blockies
-
Notifications
You must be signed in to change notification settings - Fork 285
/
build.js
69 lines (61 loc) · 1.57 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
var http = require('http');
var qs = require('querystring');
var fs = require('fs');
function build(inFile, outFile) {
console.log('Reading ' + inFile);
var inFileContents = fs.readFileSync(inFile, {encoding: 'utf8'});
console.log('Read ' + inFileContents.length + ' bytes');
console.log('Compressing...');
compress(inFileContents, function(err, code) {
if(err) throw err;
var percent = Math.floor((code.length / inFileContents.length) * 100);
console.log('Compressed ' + inFileContents.length + ' to ' +
code.length + ' bytes (' + percent + '%)');
console.log('Writing ' + outFile);
fs.writeFileSync(outFile, code);
});
}
function compress(code, cb) {
cb = once(cb);
var code; // TODO
var params = {
output_format: 'json',
output_info: 'compiled_code',
compilation_level: 'SIMPLE_OPTIMIZATIONS',
warning_level: 'verbose',
output_file_name: 'default.js',
js_code: code
};
var req = http.request({
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
hostname: 'closure-compiler.appspot.com',
path: '/compile'
}, function(res) {
res.setEncoding('utf8');
var body = '';
res.on('readable', function() {
body += res.read();
}).on('end', function() {
try {
var code = JSON.parse(body).compiledCode;
cb(null, code);
} catch(e) {
cb(e);
}
}).on('error', cb);
});
req.end(qs.stringify(params));
req.on('error', cb);
}
function once(fn) {
var f = function() {
if(f.called) return;
f.called = true;
return fn.apply(this, arguments);
}
return f;
}
build('blockies.js', 'blockies.min.js');