-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.js
150 lines (122 loc) · 4.07 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
#!/usr/bin/env node
var https = require('https'),
fs = require('fs'),
Path = require('path'),
jqtversion = 'v1.2.6',
dist_path = __dirname + '/content/download/dist/' + jqtversion,
mode = fs.statSync(__dirname).mode,
jsp = require("uglify-js").parser,
pro = require("uglify-js").uglify;
function githubOpts(path) {
return {
host: 'api.github.com',
path: path,
port: 443
};
};
function mkdirp(path, mode, callback, position) {
var parts = Path.normalize(path).split('/');
position = position || 0;
if (position >= parts.length) {
return callback();
}
var directory = parts.slice(0, position + 1).join('/') || '/';
fs.stat(directory, function(err) {
if (err === null) {
mkdirp(path, mode, callback, position + 1);
} else {
fs.mkdir(directory, mode, function (err) {
if (err && err.errno != 17) {
return callback(err);
} else {
mkdirp(path, mode, callback, position + 1);
}
});
}
});
}
//1 Get Tag SHA
var tagopts = githubOpts('/repos/jquerytools/jquerytools/git/refs/tags/' + jqtversion);
https.get(tagopts, function(res) {
if (res.statusCode === 200) {
res.on('data', function(d) {
var j = JSON.parse(d);
processTag(j.object.sha);
});
} else {
console.error("Tag request Failed with status code:" + res.statusCode);
}
}).on('error', function(e) {
console.error("Error");
console.error(e);
});
//2 Get Tree
function processTag(sha) {
var treeopts = githubOpts('/repos/jquerytools/jquerytools/git/trees/' + sha + '?recursive=true');
https.get(treeopts, function(res) {
var result = "";
if (res.statusCode === 200) {
res.on('data', function(d) {
result += d.toString();
}).on('end', function() {
processTree(JSON.parse(result).tree);
});
} else {
console.error("Tree request Failed with status code:" + res.statusCode);
}
}).on('error', function(e) {
console.error("Error");
console.error(e);
});
}
//3 Get Individual files and copy them to downloads/dist/version
function processTree(tree) {
var length = tree.length;
for (var i=0; i < length; i++) {
var item = tree[i];
if (item.path.indexOf('src/') == 0) {
processItem(item);
}
}
}
function processItem(item) {
if (item.path.indexOf(".js") > 0) {
processFile(item);
}
}
//4 Run uglify to minize the files to min.js
function processFile(item) {
var frag = item.path.replace('src/', ''),
newpath = dist_path + '/' + frag,
minpath = dist_path + '/' + Path.dirname(frag) + '/' + Path.basename(frag, '.js') + '.min.js';
mkdirp(Path.dirname(newpath), mode, function() {
var fileopts = githubOpts(item.url);
fileopts.headers = {
Accept: "application/vnd.github-blob.raw"
};
https.get(fileopts, function(res) {
var result = "";
if (res.statusCode === 200) {
res.on('data', function(d) {
result += d.toString();
}).on('end', function() {
fs.writeFile(newpath, result, function() {
console.log("Downloaded:" + newpath);
var ast = jsp.parse(result);
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
var compressed = pro.gen_code(ast);
fs.writeFile(minpath, compressed, function() {
console.log("Compressed:" + minpath);
});
});
});
} else {
console.error("Blob request Failed with status code:" + res.statusCode);
}
}).on('error', function(e) {
console.error("Error");
console.error(e);
});
});
}