-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
173 lines (126 loc) · 3.67 KB
/
gulpfile.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
var fs = require('fs'),
cheerio = require('cheerio'),
gulp = require('gulp'),
concat = require('gulp-concat'),
htmlmin = require('gulp-htmlmin'),
rimraf = require('gulp-rimraf'),
rename = require('gulp-rename'),
replace = require('gulp-replace'),
webserver = require('gulp-webserver'),
uglify = require('gulp-uglify'),
unzip = require('gulp-unzip'),
zip = require('gulp-zip'),
exclude_min = [],
config = { js: [] };
gulp.task('build', ['initbuild', 'jsmin', 'addjs', 'copy', 'zip', 'unzip', 'clean', 'report']);
gulp.task('serve', function() {
gulp.src('.')
.pipe(webserver({
livereload: true,
host: 'localhost',
port: 8013,
open: true
}));
});
gulp.task('initbuild', function() {
var stream, html, $, src, js = [];
// delete prev files
stream = gulp.src('game.zip')
.pipe(rimraf());
stream = gulp.src('g.js')
.pipe(rimraf());
stream = gulp.src('index.html')
.pipe(rimraf());
// get a list of all js scripts from our dev file
html = fs.readFileSync('dev.html', 'utf-8', function(e, data) {
return data;
});
$ = cheerio.load(html);
$('script').each(function() {
src = $(this).attr('src');
if (exclude_min.indexOf(src) === -1) {
js.push(src);
}
});
config.js = js;
console.log(js);
});
gulp.task('jsmin', ['initbuild'], function() {
var stream = gulp.src(config.js)
.pipe(concat('g.js'))
.pipe(uglify())
.pipe(gulp.dest('.'));
return stream;
});
gulp.task('addjs', ['jsmin'], function() {
var js = fs.readFileSync('g.js', 'utf-8', function(e, data) {
return data;
});
var i, tmp, extra_js = '';
for (i = 0; i < exclude_min.length; i += 1) {
console.log(exclude_min[i])
extra_js += fs.readFileSync(exclude_min[i], 'utf-8', function(e, data) {
return data;
});
}
console.log(extra_js.length, 'OK', exclude_min);
var stream = gulp.src('dev.html')
.pipe(replace(/<.*?script.*?>.*?<\/.*?script.*?>/igm, ''))
.pipe(replace(/<\/body>/igm, '<script>'+extra_js+' '+js+'</script></body>'))
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(rename('index.html'))
.pipe(gulp.dest('./tmp'));
return stream;
});
gulp.task('copy', function() {
var stream = gulp.src('index.html')
.pipe(gulp.dest('docs/'));
return stream;
});
gulp.task('zip', ['addjs'], function() {
var stream = gulp.src('tmp/index.html')
.pipe(zip('game.zip'))
.pipe(gulp.dest('.'));
return stream;
});
gulp.task('unzip', ['zip'], function() {
var stream = gulp.src('game.zip')
.pipe(unzip())
.pipe(gulp.dest('.'));
return stream;
});
gulp.task('clean', ['unzip'], function() {
var stream = gulp.src('tmp/')
.pipe(rimraf());
return stream;
});
gulp.task('report', ['clean'], function() {
var stat = fs.statSync('game.zip'),
limit = 1024 * 13,
size = stat.size,
remaining = limit - size,
percentage = (remaining / limit) * 100;
percentage = Math.round(percentage * 100) / 100
console.log('\n\n-------------');
console.log('BYTES USED: ' + stat.size);
console.log('BYTES REMAINING: ' + remaining);
console.log(percentage +'%');
console.log('-------------\n\n');
});
gulp.task('encode', function() {
var files = fs.readdirSync('./a'),
gifs = [],
n, parts, base64;
for ( n in files) {
if (files[n].indexOf('.gif') !== -1) {
gifs.push(files[n]);
}
}
for (n = 0; n < gifs.length; n += 1) {
fs.readFileSync('.a/'+gifs[n], function(err, data) {
console.log(err, data);
});
parts = gifs[n].split('.');
console.log(parts[0], gifs[n], base64);
}
});