This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgulpfile.js
551 lines (466 loc) · 15.3 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
'use strict';
// What files to monitor - when to rebuild and reload emulator?
var watchedFiles = [
'./src/*.{js,xml,css}',
'./src/**/*.{js,xml,css}'
];
// Where are your ES6 files which are going to be translated?
// These are also the files which will be linted by eslint
var babelSrc = [
'./src/*.js',
'./src/**/*.js',
'!./src/app/thirdparty/*',
'!./src/app/dev-resources/*',
'!./src/app/dev-resources/**'
];
// Where are your resource files which are just being
// moved without any translation.
var resources = [
'./src/*.{xml,css}',
'./src/**/*.{xml,css}',
'./src/ap*/thirdparty/*',
'./src/ap*/dev-resources/*',
'./src/ap*/dev-resources/**'
];
// These folders are cleaned every time a build is done.
// This pattern should be all the folders you have in /app/src/
var generatedSources = [
'./rekapp/app/{components,shared,test,thirdparty,views}'
];
// Destination to save files
var destination = './rekapp';
// Concatinated with CWD
var pathToStartAppFrom = 'rekapp';
// Which emulator to run?
// Valid emulators are
// iPhone-4s, iPhone-5, iPhone-5s, iPhone-6-Plus, iPhone-6,
// iPad-2, iPad-Retina, iPad-Air, Resizable-iPhone, Resizable-iPa
var iOSEmulator = 'iPhone-5';
var androidEmulator = 'Nexus-5';
// Images settings
var iconColor = '#879c9c';
var imageDestinationPath = './rekapp/app/images';
var svgPath = './src/app/images/md/*/svg/production/*_24px.svg';
var fixedImagesPath = './src/app/images-fixed/**'; // Images which just should be copied.
/**
* Dependencies
*/
var runSequence = require('run-sequence');
var debug = require('gulp-debug');
// Build Dependencies
var gulp = require('gulp');
var chalk = require('chalk');
var spawn = require('child_process').spawn;
var babel = require('gulp-babel');
var mocha = require('gulp-mocha');
var mergeStream = require('merge-stream');
var del = require('del');
var eslint = require('gulp-eslint');
var watch = require('gulp-watch');
// Image Dependencies
var raster = require('gulp-raster');
var rename = require('gulp-rename');
var lazypipe = require('lazypipe');
var clone = require('gulp-clone');
var cheerio = require('gulp-cheerio');
var rimraf = require('gulp-rimraf');
gulp.task('watchForNSWatch', function() {
watch(babelSrc)
.pipe(debug({title: 'Compiling JS:'}))
.pipe(babel({
stage: 1,
plugins: ['object-assign']
}))
.pipe(gulp.dest(destination));
watch(resources)
.pipe(debug({title: 'Moving Resource:'}))
.pipe(gulp.dest(destination));
});
gulp.task('startIOS', function(cb) {
var child = spawn('tns', ['emulate', 'ios', '--device', iOSEmulator], {cwd: process.cwd() + '/' + pathToStartAppFrom});
var stdout = '';
var stderr = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
stdout += data;
console.log(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
stderr += data;
console.log(chalk.red(data));
});
child.on('close', function(code) {
console.log('Done with exit code', code);
});
cb();
});
gulp.task('livesyncIOS', function(callback) {
console.log();
console.log(chalk.blue('Watcher started, will restart emulator when files change'));
console.log();
gulp.watch(watchedFiles, function () {
runSequence(
'_clean',
'_compile',
'_livesyncIOS',
callback);
}
);
});
gulp.task('watchFullIOS', function(callback) {
console.log();
console.log(chalk.blue('Watcher started, will restart emulator when files change'));
console.log();
gulp.watch(watchedFiles, function () {
runSequence(
'_clean',
'_compile',
'_watchFullIOS',
callback);
}
);
});
gulp.task('watchFullAndroid', function(callback) {
gulp.watch(watchedFiles, function () {
runSequence(
'_clean',
'_compile',
'_emulateAndroid',
callback);
}
);
});
gulp.task('fullAndroid', function(callback) {
runSequence(
'_clean',
'_compile',
'_emulateAndroid',
callback);
});
gulp.task('lint', function () {
return gulp.src(babelSrc)
.pipe(eslint())
.pipe(eslint.format());
});
gulp.task('test', function(callback) {
runSequence(
'_clean',
'_compile',
'_test',
callback);
});
gulp.task('deployToIphone', function(callback) {
runSequence(
'_clean',
'_compile',
'_deployToIphone',
callback);
});
gulp.task('cleanCompile', function(callback) {
runSequence(
'_clean',
'_compile',
callback);
});
gulp.task('images', function(callback) {
runSequence('_cleanImages',
'_ios1x',
'_ios2x',
'_ios3x',
'_android1x',
'_android15x',
'_android2x',
'_android3x',
'_android4x',
'_copyFixedImages',
callback);
});
/** ************************************************************************ *\
*
* SUB-TASKS
*
\* ************************************************************************* */
gulp.task('_livesyncIOS', function(cb) {
var child = spawn('tns', ['livesync', 'ios', '--emulator'], {cwd: process.cwd() + '/' + pathToStartAppFrom});
var stdout = '';
var stderr = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
stdout += data;
console.log(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
stderr += data;
console.log(chalk.red(data));
});
child.on('close', function(code) {
console.log('Done with exit code', code);
});
cb();
});
gulp.task('_watchFullIOS', function(cb) {
var child = spawn('tns', ['emulate', 'ios', '--device', iOSEmulator], {cwd: process.cwd() + '/' + pathToStartAppFrom});
var stdout = '';
var stderr = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
stdout += data;
console.log(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
stderr += data;
console.log(chalk.red(data));
});
child.on('close', function(code) {
console.log('Done with exit code', code);
});
cb();
});
gulp.task('_emulateAndroid', function(cb) {
//▶ tns emulate android --geny Nexus-5
//var child = spawn('tns', ['emulate', 'android', '--geny', androidEmulator], {cwd: process.cwd() + '/' + pathToStartAppFrom});
//tns deploy android --device 1
var child = spawn('tns', ['deploy', 'android', '--device', '1'], {cwd: process.cwd() + '/' + pathToStartAppFrom});
var stdout = '';
var stderr = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
stdout += data;
console.log(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
stderr += data;
console.log(chalk.red(data));
});
child.on('close', function(code) {
console.log('Done with exit code', code);
});
cb();
});
gulp.task('_clean', function(cb) {
del(generatedSources, cb);
});
gulp.task('_compile', function() {
var js = gulp.src(babelSrc)
.pipe(debug({title: 'Compiling: '}))
.pipe(babel({
stage: 1,
plugins: ['object-assign']
}))
.pipe(gulp.dest(destination));
var res = gulp.src(resources)
.pipe(debug({title: 'Moving Resource:'}))
.pipe(gulp.dest(destination));
return mergeStream(js, res);
});
gulp.task('_test', function() {
return gulp.src(['app/test/*.js'], { read: false })
.pipe(mocha({
reporter: 'spec'
}));
});
gulp.task('_deployToIphone', function(cb) {
var child = spawn('tns', ['deploy', 'ios', '--device', '1'], {cwd: process.cwd() + '/' + pathToStartAppFrom});
var stdout = '';
var stderr = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
stdout += data;
console.log(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
stderr += data;
console.log(chalk.red(data));
});
child.on('close', function(code) {
console.log('Done with exit code', code);
});
cb();
});
gulp.task('_ios1x', function () {
return gulp.src(svgPath)
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/_24px$/, '');
opt.dirname = opt.dirname.replace(/svg\/production$/, '');
return opt;
}))
.pipe(colorize(iconColor))
.pipe(raster({format: 'png', scale: 1}))
.pipe(rename({extname: '.png', suffix: ''}))
.pipe(rename(function(opt) {
opt.dirname = opt.dirname + '/' + 'ios';
return opt;
}))
.pipe(gulp.dest(imageDestinationPath));
});
gulp.task('_ios2x', function () {
return gulp.src(svgPath)
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/_24px$/, '');
opt.dirname = opt.dirname.replace(/svg\/production$/, '');
return opt;
}))
.pipe(colorize(iconColor))
.pipe(raster({format: 'png', scale: 2}))
.pipe(rename({extname: '.png', suffix: '@2x'}))
.pipe(rename(function(opt) {
opt.dirname = opt.dirname + '/' + 'ios';
return opt;
}))
.pipe(gulp.dest(imageDestinationPath));
});
gulp.task('_ios3x', function () {
return gulp.src(svgPath)
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/_24px$/, '');
opt.dirname = opt.dirname.replace(/svg\/production$/, '');
return opt;
}))
.pipe(colorize(iconColor))
.pipe(raster({format: 'png', scale: 3}))
.pipe(rename({extname: '.png', suffix: '@3x'}))
.pipe(rename(function(opt) {
opt.dirname = opt.dirname + '/' + 'ios';
return opt;
}))
.pipe(gulp.dest(imageDestinationPath));
});
gulp.task('_android1x', function () {
return gulp.src(svgPath)
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/_24px$/, '');
opt.dirname = opt.dirname.replace(/svg\/production$/, '');
return opt;
}))
.pipe(colorize(iconColor))
.pipe(raster({format: 'png', scale: 1}))
.pipe(rename({extname: '.png'}))
.pipe(rename(function(opt) {
opt.dirname = opt.dirname + '/' + 'android/drawable-mdpi';
return opt;
}))
.pipe(gulp.dest(imageDestinationPath));
});
gulp.task('_android15x', function () {
return gulp.src(svgPath)
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/_24px$/, '');
opt.dirname = opt.dirname.replace(/svg\/production$/, '');
return opt;
}))
.pipe(colorize(iconColor))
.pipe(raster({format: 'png', scale: 1.5}))
.pipe(rename({extname: '.png'}))
.pipe(rename(function(opt) {
opt.dirname = opt.dirname + '/' + 'android/drawable-hdpi';
return opt;
}))
.pipe(gulp.dest(imageDestinationPath));
});
gulp.task('_android2x', function () {
return gulp.src(svgPath)
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/_24px$/, '');
opt.dirname = opt.dirname.replace(/svg\/production$/, '');
return opt;
}))
.pipe(colorize(iconColor))
.pipe(raster({format: 'png', scale: 2}))
.pipe(rename({extname: '.png'}))
.pipe(rename(function(opt) {
opt.dirname = opt.dirname + '/' + 'android/drawable-xhdpi';
return opt;
}))
.pipe(gulp.dest(imageDestinationPath));
});
gulp.task('_android3x', function () {
return gulp.src(svgPath)
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/_24px$/, '');
opt.dirname = opt.dirname.replace(/svg\/production$/, '');
return opt;
}))
.pipe(colorize(iconColor))
.pipe(raster({format: 'png', scale: 3}))
.pipe(rename({extname: '.png'}))
.pipe(rename(function(opt) {
opt.dirname = opt.dirname + '/' + 'android/drawable-xxhdpi';
return opt;
}))
.pipe(gulp.dest(imageDestinationPath));
});
gulp.task('_android4x', function () {
return gulp.src(svgPath)
.pipe(rename(function(opt) {
opt.basename = opt.basename.replace(/_24px$/, '');
opt.dirname = opt.dirname.replace(/svg\/production$/, '');
return opt;
}))
.pipe(colorize(iconColor))
.pipe(raster({format: 'png', scale: 4}))
.pipe(rename({extname: '.png'}))
.pipe(rename(function(opt) {
opt.dirname = opt.dirname + '/' + 'android/drawable-xxxhdpi';
return opt;
}))
.pipe(gulp.dest(imageDestinationPath));
});
gulp.task('_cleanImages', function() {
return gulp.src(imageDestinationPath, { read: false })
.pipe(rimraf());
});
gulp.task('_copyFixedImages', function() {
return gulp.src(fixedImagesPath)
.pipe(debug({title: 'Copying fixed image:'}))
.pipe(gulp.dest(imageDestinationPath));
});
function colorize (color) {
var sink;
return (lazypipe()
.pipe(function () {
sink = clone.sink();
return sink;
})
.pipe(cheerio, function ($) {
$('svg').attr('fill', color);
})
.pipe(function () {
return sink.tap();
})
)();
}
/**
* DEFAULT TASK
*/
gulp.task('default', function() {
console.log();
console.log(chalk.magenta(' Main tasks'));
console.log();
console.log(' ' + chalk.blue('gulp livesyncIOS') + ' - Start watching files, recompile Javascript and restart');
console.log(' emulator (set to: ' + iOSEmulator + ') when files change.');
console.log();
console.log(' ' + chalk.blue('gulp watchAndroid') + ' - Start watching files, recompile Javascript and restart');
console.log(' emulator (set to: ' + androidEmulator + ') when files change.');
console.log();
console.log(' ' + chalk.blue('gulp test') + ' - Clean, compile and run tests in /app/tests');
console.log();
console.log(' ' + chalk.blue('gulp lint') + ' - Run eslint');
console.log();
console.log(chalk.magenta(' Main tasks'));
console.log();
console.log(' ' + chalk.blue('gulp images') + ' - Clean /app/images and regenerates images from SVG');
console.log();
console.log(chalk.magenta(' Sub-tasks') + ' (primarily run by main tasks)');
console.log();
console.log(' ' + chalk.blue('gulp _clean') + ' - Clean target folders (' + generatedSources.join(', ') + ')');
console.log();
console.log(' ' + chalk.blue('gulp _compile') + ' - Compile Javascript');
console.log();
console.log(' ' + chalk.blue('gulp _test') + ' - Run tests');
console.log();
});