-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuilder.js
executable file
·254 lines (207 loc) · 5.8 KB
/
builder.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
#!/usr/bin/env node
/**
* Builder for Swarm Client Javascript Library.
* It merges files and creates distributable versions.
*
**/
var fs = require('fs'),
sys = require('sys'),
uglify = require('uglify-js'),
path = require('path');
/* Configuration */
var config = {
name: 'swarm',
srcpath: 'lib',
socketio_base: 'node_modules/socket.io-client/dist/',
socketio_file: 'socket.io.js',
socketio_cd: 'crossdomain.xml',
socketio_swf: ['WebSocketMain.swf', 'WebSocketMainInsecure.swf'],
distdir: 'dist',
combine: ['util.js', 'swarm.js'],
version: {
major: '0',
minor: '7',
micro: '2',
qualifier: ''
},
servers: [ //development
{ 'env': 'dev',
'api': 'stream.dev.bugswarm.com'
},
{ //staging
'env': 'staging',
'api': 'stream.staging.bugswarm.com'
},
{ //test
'env': 'test',
'api': 'stream.test.bugswarm.com'
},
{ //production
'env': 'prod',
'api': 'stream.bugswarm.com'
}
]
};
//Utility functions
function copy(src, dst) {
if (!fs.existsSync(src)) {
throw new Error(src + ' does not exists. Nothing to be copied');
}
if (fs.statSync(src).isDirectory()) {
throw new Error(src + ' is a directory. It must be a file');
}
if (src == dst) {
throw new Error(src + ' and ' + dst + 'are identical');
}
var infd = fs.openSync(src, 'r');
var size = fs.fstatSync(infd).size;
var outfd = fs.openSync(dst, 'w');
fs.sendfileSync(outfd, infd, 0, size);
fs.closeSync(infd);
fs.closeSync(outfd);
}
function copytree(src, dst) {
if (!fs.existsSync(src)) {
throw new Error(src + ' does not exists. Nothing to be copied');
}
if (!fs.statSync(src).isDirectory()) {
throw new Error(src + ' must be a directory');
}
var filenames = fs.readdirSync(src);
var basedir = src;
if (!fs.existsSync(dst)) {
fs.mkdirSync(dst, 0755);
}
for (var name in filenames) {
var file = basedir + '/' + filenames[name];
var newdst = dst + '/' + filenames[name];
if (fs.statSync(file).isDirectory()) {
copytree(file, newdst);
} else {
copy(file, newdst);
}
}
}
var rlevel = 0;
var root;
function rmtree(_path) {
if (fs.statSync(_path).isFile()) {
throw new Error(_path + ' is a file. Use fs.unlink instead');
}
if (!root) {
root = _path;
}
var filenames = fs.readdirSync(_path);
var basedir = _path;
for (var name in filenames) {
var file = basedir + '/' + filenames[name];
if (fs.statSync(file).isDirectory()) {
rlevel++;
rmtree(file);
rlevel--;
fs.rmdirSync(file);
} else {
fs.unlinkSync(file);
}
}
if (rlevel === 0 && fs.existsSync(root)) {
fs.rmdirSync(root);
}
}
function combine() {
var api = '';
var files = config.combine;
var srcpath = config.srcpath;
var distdir = config.distdir;
var len = files.length;
for (var i = 0; i < len; i++) {
api += '\n';
api += fs.readFileSync(srcpath + '/' + files[i]);
}
var socketio = fs.readFileSync(config.socketio_base + '/' + config.socketio_file);
if (!fs.existsSync(distdir)) {
fs.mkdirSync(distdir, 0755);
}
var release = socketio + '\n' + api;
return release;
}
function minimize(code) {
var ast = uglify.parser.parse(code);
ast = uglify.uglify.ast_mangle(ast);
ast = uglify.uglify.ast_squeeze(ast);
code = uglify.uglify.gen_code(ast, {ascii_only: true});
return code;
}
// Builder
function Builder() {}
Builder.prototype.dist = function() {
var version = config.version;
var release = combine();
var name = config.name;
name += '-v' + version.major + '.' + version.minor +
'.' + version.micro;
if(version.qualifier !== '') {
name += '.' + version.qualifier;
}
var distdir = config.distdir;
rmtree('./' + distdir);
fs.mkdirSync('./' + distdir, 0755);
var servers = config.servers;
var len = servers.length;
for(var i = 0; i < len; i++) {
var _release = release.replace(/@@API_SERVER@@/ig, servers[i].api);
_release = _release.replace(/@@XMPP_SERVER@@/ig, servers[i].xmpp);
var release_min = minimize(_release);
var envdir = distdir + '/' + servers[i].env;
if(!fs.existsSync(envdir)) {
fs.mkdirSync(envdir, 0755);
}
fs.writeFileSync(envdir + '/' + name + '.js', _release);
fs.writeFileSync(envdir + '/' + name + '.min.js', release_min);
for (var file in config.socketio_swf) {
var src = config.socketio_base + '/' + config.socketio_swf[file];
var dst = envdir + '/' + config.socketio_swf[file];
copy(src, dst);
}
copy(config.srcpath + '/' + config.socketio_cd, envdir + '/' + config.socketio_cd);
}
};
/**
* Arguments.
*/
var args = process.argv.slice(2);
/**
* Usage information.
*/
var usage = '' +
'\x1b[1mUsage\x1b[0m: ./builder [task]\n' +
'\n' +
'\x1b[1mTasks:\x1b[0m\n' +
' help Print out instructions to use the builder\n' +
' dist Generates distributable version\n' +
' jslint Runs JSlint to verify the code\n';
// Parse arguments
var arg;
var builder = new Builder();
var alength = args.length;
if (!alength) {
sys.puts(usage);
process.exit(1);
}
while (args.length) {
arg = args.shift();
switch (arg) {
case 'help':
sys.puts(usage);
process.exit(1);
break;
case 'dist':
builder.dist();
break;
case 'jslint':
builder.doc();
break;
default:
sys.puts(usage);
}
}