-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
executable file
·356 lines (318 loc) · 12.9 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
#!/usr/bin/env node
/**
* qrvoice.net builder
* Copyright (c) 2015, Marcel Duran
*/
var
// files, dirs and programs
TMP_DIR = '/tmp/',
SRC_DIR = './',
BUILD_DIR = process.argv[3] || './build/',
IMG_DIR = 'images/',
JS_DIR = 'js/qrvoice/',
LANG_DIR = JS_DIR + 'lang/',
CSS_DIR = 'css/',
CSS_URL = 'http://qrvoice.net/' + CSS_DIR,
INDEX_HTML = 'index.html',
FAVICON = 'favicon.ico',
LOGO = 'logo.png',
HELP = 'help.jpg',
STARTUP_4 = 'startup-640x920.png',
STARTUP_5 = 'startup-640x1096.png',
MAIN_JS = 'qrvoice.js',
MAIN_CSS = 'qrvoice.css',
IE_MHT = 'qrvoice.mht',
IE_CSS = 'qrvoice.ie',
IE_MHTML = 'qrvoice.mhtml',
COPYRIGHT = 'Copyright (c) ' +
(new Date()).getFullYear() + ', Marcel Duran',
// required modules
fs = require('fs'),
exec = require('child_process').exec,
htmlMinifier = require('html-minifier'),
uglifyjs = require('uglify-js'),
cleancss = new (require('clean-css'))(),
// timestamp
ts = new Date(),
// create directory
createDir = function (dir, callback) {
console.log('creating ' + dir);
exec('mkdir -p ' + dir, callback);
},
// copy all files
copyFiles = function (src, dest) {
console.log('copying files from ' + src + ' to ' + dest);
exec('cp -r ' + src + ' ' + dest);
},
// js minifier
minifyJSCode = function (code) {
var minified;
// set version
code = code.replace(/, '\d+\.\d+\.\d+'/, ',\'' + version + '\'');
minified = uglifyjs.minify(code, {fromString: true}).code;
return {
type: 'JS',
original: code,
minified: minified
};
},
// css minifier
minifyCSSCode = function (input, output, callback) {
fs.readFile(input, function (error, data) {
callback(input, output, {
type: 'CSS',
original: data.toString('utf8'),
minified: cleancss.minify(data.toString('utf8')).styles
});
});
},
// save file displaying statistics
saveFile = function (input, output, data) {
var content = data.minified;
if (data.type === 'HTML') {
content += '\n<!-- ' + COPYRIGHT + '-->';
} else {
content = '/* ' + COPYRIGHT + ' */\n' + content;
}
fs.writeFile(output, content, function (error) {
var sourceLen, targetLen, savings;
if (error) {
throw error;
}
sourceLen = data.original.length;
targetLen = data.minified.length;
savings = (1 - targetLen / sourceLen) * 100;
console.log([
data.type + ' minified (' + savings.toFixed(1) + '% savings):',
'\tsource: ' + input + ' = ' + sourceLen,
'\ttarget: ' + output + ' = ' + targetLen
].join('\n'));
});
};
// minify file
minifyFile = function (input, output, conf) {
var addSuffix = function (name) {
var ext,
idx = name.lastIndexOf('.');
idx = idx > -1 ? idx : name.length;
ext = name.slice(idx);
return name.slice(0, idx) + conf.suffix + ext;
};
if (conf.suffix) {
output = addSuffix(output);
}
if (conf.append) {
output += conf.append;
}
if (conf.async) {
conf.minifier(input, output, saveFile);
} else {
fs.readFile(input, function (error, data) {
if (error) {
throw error;
}
data = conf.minifier(data.toString('utf8'));
if (conf.post) {
data.minified += conf.term + conf.post.minified;
data.original += conf.term + conf.post.original;
}
if (conf.callback) {
conf.callback(data, '_' + addSuffix(conf.filename));
} else {
saveFile(input, output, data);
}
});
}
};
// minify dir
minifyDir = function (srcDir, destDir, conf) {
fs.readdir(srcDir, function (error, files) {
files.forEach(function (filename) {
if (filename.slice(filename.lastIndexOf('.')) === conf.ext) {
conf.filename = filename;
minifyFile(srcDir + filename, destDir + filename, conf);
}
});
});
},
// minify HTML after creating dir
goHTML = function () {
// minify index.html inline js and html
fs.readFile(SRC_DIR + INDEX_HTML, function (error, data) {
var minified,
reScript = /<script>([\d\w\s\-:=;,\.\(\)\{\}\/\\\|&'\[\]!\+\?#@]+)<\/script>/g;
if (error) {
throw error;
}
data = data.toString('utf8').replace(reScript, function (all, match) {
// remove filter, so use YUI default
match = match.replace(/\s*filter:.*,/, '');
// combine
match = match.replace(/\s*base:.*,/, '');
match = match.replace(/\s*combine:.*,/g, 'combine:true,');
// set module path
match = match.replace(/\s*path:.*,/,
'path:\'qrvoice' + ts + '.js\',');
// minify code
return '<script>' + minifyJSCode(match).minified + '</script>';
});
// set css path
data = data.replace(RegExp(MAIN_CSS, 'g'),
MAIN_CSS.replace('.', ts + '.'));
data = data.replace(RegExp(IE_MHT, 'g'),
IE_MHT.replace('.', + ts + '.'));
// minify html
minified = htmlMinifier.minify(data, {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
});
// write output
saveFile(SRC_DIR + INDEX_HTML, BUILD_DIR + INDEX_HTML, {
type: 'HTML',
original: data,
minified: minified
});
});
// minify faqs inline js/css and html
fs.readdir(SRC_DIR, function (error, files) {
if (error) {
throw error;
}
files.forEach(function (file) {
if (/^faq_.+\.html/.test(file)) {
fs.readFile(SRC_DIR + file, function (error, data) {
var minified, tempfile,
reScript = /<script>([\d\w\s\-:=;,\.\(\)\{\}\/\\\|&'\[\]!\+\?#@]+)<\/script>/g,
reStyle = /<style type="text\/css">([\d\w\s\-:=;,\.\(\)\{\}\/\\\|&'\[\]!\+\?#@%]+)<\/style>/;
if (error) {
throw error;
}
// minify js
data = data.toString('utf8').replace(reScript, function (all, match) {
return '<script>' + minifyJSCode(match).minified + '</script>';
});
// minify css
inline = reStyle.exec(data);
inline = (inline && inline[1]) || '';
tempfile = 'qr_' + parseInt(Math.random() * 1e9, 10) + '.css',
// save content of inline css into temp file
(function (file, data, tempfile, inline) {
fs.writeFile(TMP_DIR + tempfile, inline, function (error) {
if (error) {
throw error;
}
// minify temp css file
minifyCSSCode(TMP_DIR + tempfile, null, function (input, output, inlineData) {
var minified;
// remove temp file
fs.unlink(TMP_DIR + tempfile);
// replace minified inline css
data = data.replace(reStyle, function () {
return '<style type="text/css">' + inlineData.minified + '</style>';
});
// minify html
minified = htmlMinifier.minify(data, {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
});
// write output
saveFile(SRC_DIR + file, BUILD_DIR + file, {
type: 'HTML',
original: data,
minified: minified
});
});
});
}(file, data, tempfile, inline));
});
}
});
});
// copy favicon
copyFiles(SRC_DIR + FAVICON, BUILD_DIR + FAVICON);
};
// minify JSs after creating dirs
goJS = function () {
minifyDir(SRC_DIR + JS_DIR, BUILD_DIR + JS_DIR, {
ext: '.js',
minifier: minifyJSCode,
suffix: ts,
callback: function (post, append) {
minifyDir(SRC_DIR + LANG_DIR, BUILD_DIR + LANG_DIR, {
ext: '.js',
minifier: minifyJSCode,
post: post,
append: append,
term: ';'
});
}
});
},
// minify CSSs after creating dirs
goCSS = function () {
minifyDir(SRC_DIR + CSS_DIR, BUILD_DIR + CSS_DIR, {
ext: '.css',
minifier: minifyCSSCode,
suffix: ts,
async: true
});
// IE [6,7]
// get main css content
fs.readFile(SRC_DIR + CSS_DIR + MAIN_CSS, function (err, css) {
css = css.toString('utf8');
// replace data uri from main css
css = css.replace(/url\(data:.+\)/, 'url(mhtml:{CSS_URL}!qr)');
css = css.replace(/url\(data:.+\)/, 'url(mhtml:{CSS_URL}!icons)');
// get ie css content
fs.readFile(SRC_DIR + CSS_DIR + IE_CSS, function (err, ie) {
// generate temp file for css compressor and final css ie filename
var filename = 'qr_' + parseInt(Math.random() * 1e9, 10) + '.css',
cssFilename = IE_MHT.replace('.', ts + '.');
ie = ie.toString('utf8');
// assign new css ie filename to mhtml refs
css = css.replace(/\{CSS_URL\}/g, CSS_URL + cssFilename);
// save content of main css and ie css into temp file
fs.writeFile(TMP_DIR + filename, css + ie, function (error) {
if (error) {
throw error;
}
// minify temp css file
minifyCSSCode(TMP_DIR + filename, null, function (input, output, data) {
// remove temp file
fs.unlink(TMP_DIR + filename);
// get mhtml content
fs.readFile(SRC_DIR + CSS_DIR + IE_MHTML, function (err, mhtml) {
// prepend html into minified css
mhtml = mhtml.toString('utf8');
data.original = mhtml + data.original;
data.minified = mhtml + data.minified;
// save final css ie file
saveFile(TMP_DIR + filename, BUILD_DIR + CSS_DIR +
cssFilename, data);
});
});
});
});
});
};
// set timestamp
ts = '-' + ts.getFullYear().toString().slice(2) + (('00' + (ts.getMonth() + 1))
.slice((ts.getMonth() + 1).toString().length)) + ts.getDate() +
(process.argv[2] || '');
// get version
version = fs.readFileSync(SRC_DIR + 'CHANGELOG')
.toString('utf8').split('\n')[0].split(' ');
version = version[version.length - 1];
console.log('QR voice ' + version);
// build directories
createDir(BUILD_DIR, goHTML);
createDir(BUILD_DIR + LANG_DIR, goJS);
createDir(BUILD_DIR + CSS_DIR, goCSS);
createDir(BUILD_DIR + IMG_DIR, function () {
copyFiles(SRC_DIR + IMG_DIR + LOGO, BUILD_DIR + IMG_DIR + LOGO);
copyFiles(SRC_DIR + IMG_DIR + HELP, BUILD_DIR + IMG_DIR + HELP);
copyFiles(SRC_DIR + IMG_DIR + STARTUP_4, BUILD_DIR + IMG_DIR + STARTUP_4);
copyFiles(SRC_DIR + IMG_DIR + STARTUP_5, BUILD_DIR + IMG_DIR + STARTUP_5);
});