forked from nenadg/traquer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.js
108 lines (86 loc) · 2.88 KB
/
compress.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
var fs = require('fs'),
compressor = require('node-minify'),
walk = require('walk'),
src = 'public/lib',
out = 'public/lib/traquer.joined.js',
opt = 'public/traquer.min.js',
outcss = 'public/traquer.min.css',
walker = walk.walk(src, { followLinks: false }),
files = [],
styles = [];
walker.on('file', function(root, stat, next) {
var parts = stat.name.split('.'),
ext = parts[parts.length - 1];
if(ext === 'js')
files.push(root + '/' + stat.name);
if(ext == 'css')
styles.push(root + '/' + stat.name);
next();
});
walker.on('end', function() {
spin('Compressing');
new compressor.minify({
type: 'no-compress',
fileIn: recompose(files),
fileOut: out,
callback: function(err, min){
if(err)
console.log(err);
console.log('[1] joined ' + files.length + ' js files.');
var gccCompressed = fs.readFileSync(out, 'utf8')
gccCompressed = gccCompressed;
fs.writeFile(out, gccCompressed, function(err) {
new compressor.minify({
type: 'yui-js',
fileIn: out,
fileOut: opt,
callback: function(err, min){
if(err)
console.log(err);
console.log('[2] yui-js compressed ' + files.length + ' js files to', opt);
fs.unlink(out);
process.exit();
}
});
});
}
});
new compressor.minify({
type: 'yui-css',
fileIn: styles,
fileOut: outcss,
callback: function(err, min){
if(err)
console.log(err);
console.log('[2] yui-css compressed ' + styles.length + ' css files to', outcss);
}
});
});
var recompose = function(files){
var traquer = files.filter(function(c){ if(c.indexOf('traquer.js') > -1) return c; })[0],
loader = files.filter(function(c){ if(c.indexOf('loader.js') > -1) return c; })[0],
adequate = [], i;
for(i in files){
if(files.hasOwnProperty(i))
if(files[i].indexOf('traquer') == -1 &&
files[i].indexOf('loader') == -1 ){
adequate.push(files[i]);
}
}
if(!traquer || !loader){
console.log('[e] core files missing.');
process.exit();
}
adequate.unshift(traquer);
adequate.push(loader);
return adequate;
}
var spin = function(message){
var spinner = '|/-\\'.split(''), i = 0;
return setInterval(function(){
process.stdout.write(message + '\t\t\t\t\t\t [' + spinner[i] + '] \033[0G');
if(i == spinner.length - 1)
i = 0;
i++;
}, 50);
}