aboutsummaryrefslogtreecommitdiffstats
path: root/library/moment/tasks/history.js
blob: 85e83338fdae0c642e4e4c090e515ca780c86c2e (plain) (blame)
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
var https = require('https'),
    zlib = require('zlib'),
    path = require('path'),
    fs = require('fs');

var count = 0;
var resolved = 0;

var outputs = [];

var done;

function check() {
    if (resolved === count) {
        normalize();
        display();
    }
}

function makeBar(length) {
    var i = '';
    while (i.length < length) {
        i += '=';
    }
    return i;
}

function normalize() {
    var i,
        max = 0,
        max2 = 0;
    for (i = 0; i < count; i++) {
        max = Math.max(max, outputs[i].gzip);
        max2 = Math.max(max2, outputs[i].original);
    }
    for (i = 0; i < count; i++) {
        outputs[i].bargraph = makeBar((outputs[i].gzip / max) * 80);
        outputs[i].bargraph2 = makeBar((outputs[i].original / max2) * 80);
    }
}

function display() {
    var i;
    for (i = 0; i < count; i++) {
        console.log(outputs[i].version + ' ' + outputs[i].gzip + ' ' + outputs[i].original);
        console.log('gzip ' + outputs[i].bargraph);
        console.log('orig ' + outputs[i].bargraph2);
    }
    done();
}

function getSizeAtVersion(version, path) {
    var data = '',
        op = {},

        req = https.request({
        host: 'raw.github.com',
        port: 443,
        path: '/timrwood/moment/' + version + path
    }, function (res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            data += chunk;
        });
        res.on('end', function (e) {
            zlib.gzip(data, function (error, result) {
                op.version = version;
                op.gzip = result.length;
                op.original = data.length;
                resolved++;
                check();
            });
        });
    });

    req.on('error', function (e) {
        console.log('problem with request: ' + e.message);
    });
    req.end();
    count++;
    outputs.push(op);
}

function getRemote() {
    var oldVersions = '1.0.1 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.4.0'.split(' '),
        newVersions = '1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.7.1'.split(' '),
        i;

    for (i = 0; i < oldVersions.length; i++) {
        getSizeAtVersion(oldVersions[i], '/moment.min.js');
    }
    for (i = 0; i < newVersions.length; i++) {
        getSizeAtVersion(newVersions[i], '/min/moment.min.js');
    }
}

function getLocal() {
    count++;
    var op = {};
    outputs.push(op);
    fs.readFile(path.normalize(__dirname + '/../min/moment.min.js'), 'utf8', function (err, data) {
        if (err) {
            throw err;
        }
        zlib.gzip(data, function (error, result) {
            op.version = '.next';
            op.gzip = result.length;
            op.original = data.length;
            resolved++;
            check();
        });
    });
}



module.exports = function (grunt) {
    grunt.registerTask('history', 'Check the codebase filesize over different releases.', function () {
        done = this.async();
        getRemote();
        getLocal();
    });
};