-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
417 lines (360 loc) · 11.6 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
var fs = require('fs'),
gulp = require('gulp'),
uglify = require('gulp-uglify'),
less = require('gulp-less'),
path = require('path'),
concat = require('gulp-concat'),
size = require('gulp-size'),
debug = require('gulp-debug'),
gzip = require("gulp-gzip"),
changed = require('gulp-changed'),
sh = require('execSync'),
csso = require('gulp-csso'),
base64 = require('gulp-base64'),
wrap = require('gulp-wrap'),
rename = require("gulp-rename"),
gutil = require('gulp-util'),
replace = require('gulp-batch-replace'),
Q = require('q'),
http = require('http'),
ecstatic = require('ecstatic'),
spawn = require('child_process').spawn,
download = require('gulp-download'),
convert = require('gulp-convert'),
StringDecoder = require('string_decoder').StringDecoder,
through = require('through2'),
docco = require("gulp-docco"),
gfi = require('gulp-file-insert'),
exec = require('gulp-exec'),
runSequence = require('run-sequence'),
svg2png = require('gulp-rsvg'),
imagemin = require('gulp-imagemin'),
pngcrush = require('imagemin-pngcrush'),
open = require('open');
var port = '8888';
// Branch info - Jenkins does some funky detached HEAD stuff, but is kind enough to give
// us an environment variable, however, this is not set in a normal environment
var jenkins_branch = (process.env.GIT_BRANCH || '').replace('origin/', '');
var travis_branch = (process.env.TRAVIS_BRANCH || '');
var res = sh.exec('git rev-parse --abbrev-ref HEAD').stdout.trim();
var gitBranch = (travis_branch || jenkins_branch || res);
console.log("current branch: ", gitBranch);
var git_sha = sh.exec("git rev-parse HEAD").stdout.trim();
var paths = {
widget: [
'lib/jquery-cookie/jquery.cookie.js',
'lib/UUID/Math.uuid.js',
'lib/keen/dist/keen-tracker.js',
'lib/detectmobilebrowser.js',
'lib/jquery-debounce/jquery.debounce.js',
'src/zoetrope.js',
],
gfi : {
"/* languages.json */" : 'src/languages.json',
"/* analytics.js */": 'src/analytics.js',
},
less: 'less/style.less',
htc: 'less/backgroundsize.min.htc',
testHTMLFiles: 'src/*.html',
docs:'docs/developer_docs.md',
languageStringsCSV: 'https://docs.google.com/spreadsheets/d/1y5McuTIe4G7F0xKM6JPm6STRKOHxE1G2IkDMiSxSeKo/export?format=csv&id=1y5McuTIe4G7F0xKM6JPm6STRKOHxE1G2IkDMiSxSeKo&gid=52872048',
mobileDetect: 'http://detectmobilebrowsers.com/download/jquery',
dist: 'dist/' + gitBranch,
keen: 'lib/keen',
svgs: 'img/svg/*.svg'
};
var module_name = "zoetrope.jquery";
var cdns = {
'v3-dev': 'd34tuy4jppw3dn.cloudfront.net',
'master': 'd34tuy4jppw3dn.cloudfront.net',
v1: 'd34tuy4jppw3dn.cloudfront.net'
};
var keenKeys = {
project: {
'v1': '53356fc8ce5e4322f7000000',
'v3': '53356fc8ce5e4322f7000000',
'v2-dev': '533151a900111c0925000022',
'master': '533151a900111c0925000022',
'v3-dev': '533151a900111c0925000022',
},
write: {
'v1': 'b286b63be854ac9ff4a371d31fa7a918112f413ebd98e3032be129bac1906145e34cc519c9e5601888b331398aef830fae871b96013b721d1ea603625187ef6fa2242dc0eaadc0a1685cc6a45fcfefa135290e5cbf6e3b4c89d5fe544328e6c1893f437ffb041ae5f9d34e5792ea9c48',
'v3': 'b286b63be854ac9ff4a371d31fa7a918112f413ebd98e3032be129bac1906145e34cc519c9e5601888b331398aef830fae871b96013b721d1ea603625187ef6fa2242dc0eaadc0a1685cc6a45fcfefa135290e5cbf6e3b4c89d5fe544328e6c1893f437ffb041ae5f9d34e5792ea9c48',
'v2-dev': '7dfc9a13209610eecaa3217d40df89bb186f575c82fb3a75d6dbe4b770fc7939db131680b979caf0c1aa802177d7d8a6494366f388230cac0ac56d55567262c45442102aff66ae0cfdb785e96ffbb6cd9696e8913c7b3393ff9bf8834fc6d9449e3fa62061a03e181cdc4e653f0e255e',
'master': '7dfc9a13209610eecaa3217d40df89bb186f575c82fb3a75d6dbe4b770fc7939db131680b979caf0c1aa802177d7d8a6494366f388230cac0ac56d55567262c45442102aff66ae0cfdb785e96ffbb6cd9696e8913c7b3393ff9bf8834fc6d9449e3fa62061a03e181cdc4e653f0e255e',
'v3-dev': '7dfc9a13209610eecaa3217d40df89bb186f575c82fb3a75d6dbe4b770fc7939db131680b979caf0c1aa802177d7d8a6494366f388230cac0ac56d55567262c45442102aff66ae0cfdb785e96ffbb6cd9696e8913c7b3393ff9bf8834fc6d9449e3fa62061a03e181cdc4e653f0e255e',
}
};
var replacements = [
['{{branch}}', gitBranch],
['{{commit-sha}}', git_sha],
['{{cdn:url}}', cdns[gitBranch]],
['{{cdn:base}}', '.'],
['{{image-cdn:url}}', cdns.v1],
['{{keen-project-id}}', keenKeys.project[gitBranch]],
['{{keen-write-key}}', keenKeys.write[gitBranch]]
];
var exitCode = 0;
callfunction = function (options) {
options = options || {};
return through.obj(function (file, enc, cb) {
cb();
}, function (cb) {
options.cb();
cb();
});
};
killserver = function (server) {
return function () {
server.close();
};
};
spawnConcurrentProcess = function (child) {
var stdout = '',
stderr = '';
var prom = Q.defer();
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
stdout += data;
gutil.log(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
stderr += data;
gutil.log(gutil.colors.red(data));
gutil.beep();
});
child.on('exit', function(code) {
if (child) {
child.kill();
}
if (code) {
gutil.log('protractor exited with code: ' + code);
exitCode = code;
}
prom.resolve();
});
return prom.promise;
};
gulp.task('docs', function () {
var options = {
layout : 'linear',
};
return gulp.src(paths.docs)
.pipe(docco(options))
.pipe(gulp.dest('./docs'));
});
gulp.task('watch-docs', ['docs'], function (){
gulp.watch(paths.docs, ['docs']);
});
gulp.task('less', ['htc', 'convert-images'], function () {
gulp.src(paths.less)
.pipe(less({
paths: [path.join(__dirname, 'less', 'includes')]
}))
.pipe(replace(replacements))
.pipe(rename(module_name+'.css'))
.pipe(base64({
extensions: ['png', 'jpg', 'svg'],
baseDir: paths.dist,
})) // Embed the images in the CSS
.pipe(size({
showFiles: true
}))
.pipe(gulp.dest(paths.dist + '/css'))
.pipe(csso()) //Optimise the CSS
.pipe(rename(module_name+'.min.css'))
.pipe(size({
showFiles: true
}))
.pipe(rename(module_name+'.min.css'))
.pipe(gulp.dest(paths.dist + '/css'))
.pipe(gzip())
.pipe(size({
showFiles: true
}))
.pipe(rename(module_name+'.min.css.gz'))
.pipe(gulp.dest(paths.dist + '/css'));
});
gulp.task('htc', function () {
gulp.src(paths.htc)
.pipe(gulp.dest(paths.dist + '/css'));
});
var decoder = new StringDecoder('utf8');
/* Returns a function which picks the right string
or the backup string, then trims it */
function getLangStringFunc(lang, defaultLang) {
var getString = function (nameString) {
var str = lang[nameString] || defaultLang[nameString];
// return without whitespace
return str.replace(/(^\s+|\s+$)/g, '');
};
return getString;
}
/* formats the json into our dictionary format. */
function makeLangugeObject(lang, defaultLang) {
var getString = getLangStringFunc(lang, defaultLang);
var output = {
callToAction: {
desktop: getString('click and drag image to rotate in 3D'),
mobile: getString('swipe image to rotate in 3D'),
},
inlineCallToAction: {
desktop: getString('Click to rotate'),
mobile: getString('Tap to rotate'),
},
rotate: getString('Rotate'),
elevate: getString('Elevate'),
zoom: getString('Zoom'),
connErr : getString('Cannot load, internet connection lost.'),
changed: lang.Timestamp,
};
return output;
}
gulp.task('lang-strings', function () {
var prom = Q.defer(),
outputName = './src/languages.json',
langColumn = '2 letter language Code', //language name column from form
outputString = '';
download(paths.languageStringsCSV)
.pipe(convert({
from: 'csv',
to: 'json'
}))
.on('data', function (jsonString) {
var languages = JSON.parse(decoder.write(jsonString.contents));
languagesObj = {};
for (var index in languages) {
var lang = languages[index],
defaultLang = languages[0];
languageName = lang[langColumn].toLowerCase();
languagesObj[languageName] = makeLangugeObject(lang, defaultLang);
}
//wrap JSON up as JS
outputString = JSON.stringify(languagesObj, null, 4);
fs.writeFile(outputName, outputString, function (err) {
if (err) {
gutil.error(err);
} else {
gutil.log("JSON saved to " + outputName);
}
prom.resolve(); // We're done when the file's written out
});
});
return prom.promise;
});
gulp.task('detect-mobile', function(){
var prom = Q.defer();
download(paths.mobileDetect)
.pipe(rename('detectmobilebrowser.js'))
.pipe(gulp.dest('lib'))
.on('end', function() {
prom.resolve();
});
return prom.promise;
});
gulp.task('html', function () {
return gulp.src(paths.testHTMLFiles)
.pipe(replace(replacements))
.pipe(gulp.dest(paths.dist));
});
gulp.task('javascript', function () {
var comment_preserve = function (node, comment) {
return /Zoetrope Ltd/i.test(comment.value);
};
return gulp.src(paths.widget)
.pipe(concat(module_name+'.js')) //merge sources in the order from the paths.widget array
.pipe(wrap('/* COPYRIGHT Zoetrope Ltd 2014. Build: ' + git_sha + ' */\n!(function(){<%= contents %>})();')) //wrap in function to keep global NS clean
.pipe(gfi(paths.gfi))
.pipe(replace(replacements))
.pipe(gulp.dest(paths.dist + '/js'))
.pipe(uglify({
compress: {
drop_console: true
},
preserveComments: comment_preserve
}).on('error', function(e) { console.log('\x07',e.message); console.log(e); return this.end(); })) //the minified version drops all the console logging
.pipe(rename(module_name+'.min.uncomp.js')) //Keep a dev version without gzipping
.pipe(gulp.dest(paths.dist + '/js'))
.pipe(gzip())
.pipe(size({
showFiles: true
}))
.pipe(rename(module_name+'.min.js.gz'))
.pipe(gulp.dest(paths.dist + '/js'));
});
gulp.task('test', ['default'], function () {
//set up http server
var server = http.createServer(
ecstatic({
root: 'dist/'
})
).listen(port);
console.log('Listening on : ' + port);
gutil.log("Spawning protractor processes");
var children = {};
children.desktop = spawn(
'protractor', ['desktopProtractor.conf.js']
);
children.mobile = spawn(
'protractor', ['mobileProtractor.conf.js']
);
return Q.allSettled([spawnConcurrentProcess(children.mobile),
spawnConcurrentProcess(children.desktop)])
.spread(function(mob, desk) {
gutil.log("Mobile Testing: ", mob.state === "fulfilled");
gutil.log("Desktop Testing: ", desk.state === "fulfilled");
gutil.log("Exit code: ", exitCode);
})
.then(function() {
killserver(server);
process.exit(exitCode);
});
});
gulp.task('build-keen', function() {
var result = sh.exec('cd lib/keen; grunt');
console.log(result.stderr);
console.log(result.stdout);
});
gulp.task('convert-images', function(cb) {
return gulp.src(paths.svgs)
.pipe(svg2png())
.pipe(imagemin({
use:[pngcrush()]
}))
.pipe(gulp.dest(paths.dist + '/img'));
});
// Rerun the task when a file changes
gulp.task('watch', ['default'], function () {
//set up http server
http.createServer(
ecstatic({
root: 'dist/',
showDir: true,
autoIndex : true,
cache : 0,
})
).listen(port);
console.log('Listening on :' + port);
//open the page in the users browser
var pageURL = 'http://localhost:' + port + '/' + gitBranch + '/test.html';
console.log(pageURL);
open(pageURL);
//gulp watch tasks
gulp.watch(paths.testHTMLFiles, ['html']);
gulp.watch(paths.widget, ['javascript']);
var gfi = [];
for(var i in paths.gfi){
gfi.push(paths.gfi[i]);
}
gulp.watch(gfi, ['javascript']);
gulp.watch('less/*', ['less']);
gulp.watch(paths.svgs, ['convert-images']);
});
// Get some language strings etc
gulp.task('fetch', ['detect-mobile','lang-strings', 'build-keen']);
gulp.task('build-all', ['less', 'javascript', 'html']);
// The default task (called when you run `gulp` from cli)
gulp.task('default', ['build-all']);
var imagemin = require('gulp-imagemin');
var pngcrush = require('imagemin-pngcrush');