forked from ractive-foundation/ractive-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
642 lines (547 loc) · 16.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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
var gulp = require('gulp'),
del = require('del'),
glob = require('simple-glob'),
exec = require('child_process').exec,
gutil = require('gulp-util'),
jscs = require('gulp-jscs'),
runSequence = require('run-sequence'),
mergeStream = require('merge-stream'),
fs = require('fs'),
nodePath = require('path'),
path = require('path'),
cordovaCreate = require('gulp-cordova-create'),
cordovaDescription = require('gulp-cordova-description'),
cordovaAuthor = require('gulp-cordova-author'),
cordovaVersion = require('gulp-cordova-version'),
cordovaAndroid = require('gulp-cordova-build-android'),
cordovaIos = require('gulp-cordova-build-ios'),
plugins = require('gulp-load-plugins')(),
applyVersions = require('./tasks/applyVersions'),
rebaseDist = require('./tasks/rebaseDist'),
seleniumServer = require('./tasks/seleniumServer'),
rfCucumber = require('./tasks/rfCucumber'),
ractiveParse = require('./tasks/ractiveParse'),
renderDocumentation = require('./tasks/renderDocumentation'),
concatManifests = require('./tasks/concatManifests'),
gulpWing = require('./tasks/gulpWing'),
jshintFailReporter = require('./tasks/jshintFailReporter'),
rfA11y = require('./tasks/rfA11y'),
pkg = require('./package.json');
var lowerCaseOnly = new RegExp('^[a-z-]+$', 'g');
var args = require('node-getopt-long').options([
['name|n=s', {
description: 'The name of the component to create',
paramName: 'ux-...',
test: function (value) {
// Check for ux-*, including nothing at all to error out on.
if (value.length < 4) {
throw new gutil.PluginError('gulp-wing', 'wing needs --name param to work');
}
// Names of components MUST be lowercase, and start with 'ux-'.
if (!lowerCaseOnly.test(value) || 'ux-' !== value.substr(0, 3)) {
throw new gutil.PluginError('gulp-wing', 'Component name must be all lowercase alphanumeric, ' +
'and begin with "ux-". Example: ux-' + value.toLowerCase());
}
return value;
}
}],
['component|c=s', 'Specify a component to run tests for'],
['plugin|p=s', 'Specify a plugin to run tests for'],
['android|a', 'Build android cordover components'],
['ios|i', 'Build iOS cordover components'],
], {
name: 'gulp (task) [option]',
helpSuffix: ' Gulp tasks:\n' +
' a11y Run a11y tests\n' +
' a11y-connect Connect to a11y server\n' +
' a11y-only \n' +
' build Build components, plugins and documentation\n' +
' build-documentation-components\n' +
' Build component documentation\n' +
' build-documentation-plugins\n' +
' Build plugin documentation\n' +
' build-sass Compile SASS to CSS\n' +
' clean Clean the public directory\n' +
' clean-dist Clean the dist directory\n' +
' concat-app ??\n' +
' concat-app-umd ??\n' +
' connect ??\n' +
' copy-use-cases ??\n' +
' copy-vendors Copy vendor files\n' +
' cordova-build Run a cordover build\n' +
' cordova-clean Clean cordover build files\n' +
' cordova-create Create a new cordover build\n' +
' cordova-run Run RF as a cordover app\n' +
' dist Build dist files\n' +
' html Generate html\n' +
' lint Run lint task on files\n' +
' ractive-build-components\n' +
' Build components\n' +
' ractive-build-plugins\n' +
' Build plugins\n' +
' ractive-build-templates\n' +
' Build templates\n' +
' ractive-build-test-templates\n' +
' Build test templates\n' +
' test Run tests\n' +
' test-connect ??\n' +
' test-only ??\n' +
' version-check Check minimum node version\n' +
' watch Run watch task\n' +
' wing Create a new component or plugin\n'
});
const DEV_SERVER_PORT = 9080,
TEST_SERVER_PORT = 8088,
A11Y_SERVER_PORT = 8089;
gulp.task('connect', function () {
plugins.connect.server({
root: 'public',
livereload: true,
port: DEV_SERVER_PORT
});
});
gulp.task('test-connect', function () {
plugins.connect.server({
root: 'public',
port: TEST_SERVER_PORT
});
});
gulp.task('a11y-connect', function (callback) {
plugins.connect.server({
root: 'public',
port: A11Y_SERVER_PORT
});
callback();
});
gulp.task('html', function () {
return gulp.src('./public/*.html')
.pipe(plugins.connect.reload());
});
gulp.task('copy-vendors', function () {
return mergeStream(
gulp.src([
'./node_modules/ractive/ractive.js',
'./node_modules/ractive/ractive.min.js',
'./node_modules/ractive/ractive.min.js.map',
'./node_modules/hammerjs/hammer.min.js',
'./node_modules/ractive-touch/index.js',
'./node_modules/ractive-events-tap/dist/ractive-events-tap.umd.js',
'./node_modules/jquery/dist/jquery.min.js',
'./node_modules/jquery/dist/jquery.min.map',
'./node_modules/lodash/lodash.min.js',
'./node_modules/superagent/superagent.js',
'./node_modules/page/page.js',
'./node_modules/foundation-sites/js/vendor/modernizr.js',
'./node_modules/lodash-compat/index.js',
'./node_modules/hljs-cdn-release/build/highlight.min.js'
])
.pipe(plugins.copy('./public/js', { prefix: 1 })),
gulp.src([
'./node_modules/hljs-cdn-release/build/styles/github.min.css'
])
.pipe(plugins.copy('./public/css', { prefix: 1 })),
// Our own project files.
gulp.src('./src/route.js')
.pipe(gulp.dest('./public/js')),
// Some reference images, taken from Zurb for demo'ing, but not part of the real source.
gulp.src([
'./src/assets/images/**/*'
])
.pipe(gulp.dest('public/images/'))
);
});
gulp.task('copy-use-cases', function () {
return gulp.src([
'./src/components/**/use-cases/*.json',
'./src/plugins/**/use-cases/*.json',
])
.pipe(plugins.rename(function (path) {
// Get rid of the extra "use-cases" folder for the destination.
path.dirname = path.dirname.split(nodePath.sep)[0];
}))
.pipe(gulp.dest('public/use-cases/'));
});
gulp.task('clean', function (callback) {
return del([
'public/**/*'
], callback);
});
gulp.task('build-sass', function () {
return mergeStream(
gulp.src('./src/**/*.scss')
.pipe(plugins.sass())
.pipe(plugins.concat('components.css'))
.pipe(gulp.dest('./public/css')),
gulp.src('./node_modules/foundation-sites/scss/*.scss')
.pipe(plugins.sass())
.pipe(gulp.dest('./public/css/foundation'))
);
});
gulp.task('ractive-build-templates', function () {
return gulp.src([
'./src/components/**/*.hbs',
'!./src/components/**/use-cases/*.hbs',
'!./src/plugins/**/use-cases/*.hbs'
])
.pipe(ractiveParse({
template: true,
prefix: 'Ractive.defaults.templates'
}))
.pipe(plugins.concat('templates.js'))
.pipe(gulp.dest('./public/js/'));
});
gulp.task('ractive-build-test-templates', function () {
return gulp.src([
'./src/components/**/use-cases/*.hbs',
'./src/plugins/**/use-cases/*.hbs'
])
.pipe(ractiveParse({
objectName: function(file) {
var parts = file.history[0].split(path.sep).slice(-3);
return parts[0] + '-' + parts[2].replace(/[.]hbs$/, '');
},
template: true,
prefix: 'Ractive.defaults.templates'
}))
.pipe(plugins.concat('templates-tests.js'))
.pipe(gulp.dest('./public/js/'));
});
gulp.task('ractive-build-components', function () {
return gulp.src([
'./src/components/**/*.js',
'!./src/components/**/*.steps.js'
])
.pipe(ractiveParse({
'prefix': 'Ractive.components'
}))
.pipe(plugins.concat('components.js'))
.pipe(gulp.dest('./public/js/'));
});
gulp.task('ractive-build-plugins', function () {
return gulp.src([
'./src/plugins/**/*.js',
'!./src/plugins/**/*.steps.js'
])
.pipe(ractiveParse({
'prefix': 'Ractive'
}))
.pipe(plugins.concat('plugins.js'))
.pipe(gulp.dest('./public/js/'));
});
gulp.task('build-documentation-components', function () {
var headerHtml = fs.readFileSync('./src/header.html'),
footerHtml = fs.readFileSync('./src/footer.html');
return mergeStream(
// Component docs page.
gulp.src('./src/components/**/manifest.json')
.pipe(concatManifests('manifest-rf.json'))
.pipe(gulp.dest('./public/'))
// Create one doc file per component, using single manifest-rf.json file data.
.pipe(renderDocumentation({
type: 'components',
componentsDir: './src/components/',
docSrcPath: './src/component-page.html',
indexSrcPath: './src/components.html',
partials: [
'./src/component.html',
'./src/component-use-case.html'
]
}))
.pipe(plugins.header(headerHtml, { pkg: pkg }))
.pipe(plugins.footer(footerHtml))
.pipe(gulp.dest('./public/')),
// Documentation pages.
gulp.src([ './src/pages/*.html' ])
.pipe(plugins.header(headerHtml, { pkg: pkg }))
.pipe(plugins.footer(footerHtml))
.pipe(gulp.dest('./public/')),
// Blank pages
gulp.src([ './src/blank-pages/*.html' ])
.pipe(gulp.dest('./public/')),
// Test runner while we're at it.
gulp.src('./src/testRunner.html')
.pipe(gulp.dest('./public/'))
);
});
gulp.task('build-documentation-plugins', function () {
var headerHtml = fs.readFileSync('./src/header.html'),
footerHtml = fs.readFileSync('./src/footer.html');
return mergeStream(
// Plugins docs page.
gulp.src('./src/plugins/**/manifest.json')
.pipe(concatManifests('manifest-rf.json'))
.pipe(gulp.dest('./public/'))
// Create one doc file per plugin, using single manifest-rf.json file data.
.pipe(renderDocumentation({
type: 'plugins',
componentsDir: './src/plugins/',
docSrcPath: './src/plugin-page.html',
indexSrcPath: './src/plugins.html',
partials: [
'./src/plugin.html',
'./src/plugin-use-case.html'
]
}))
.pipe(plugins.header(headerHtml, { pkg: pkg }))
.pipe(plugins.footer(footerHtml))
.pipe(gulp.dest('./public/'))
);
});
gulp.task('concat-app', function () {
var files = [
'./src/ractivef.base.js',
'./public/js/templates.js',
'./public/js/plugins.js',
'./public/js/components.js'
];
return gulp.src(files)
.pipe(plugins.concat('ractivef-base.js'))
.pipe(gulp.dest('./public/js/'))
.pipe(plugins.footer(fs.readFileSync('./src/ractivef.initializer.js')))
.pipe(plugins.concat('ractivef.js'))
.pipe(gulp.dest('./public/js/'));
});
gulp.task('concat-app-umd', function () {
return gulp.src('./public/js/ractivef-base.js')
.pipe(plugins.wrap({ src: './src/ractivef-umd.js'}))
.pipe(plugins.concat('ractivef-umd.js'))
.pipe(gulp.dest('./public/js/'))
.pipe(applyVersions());
});
gulp.task('wing', function (callback) {
gulpWing(args);
callback();
});
gulp.task('build', ['clean', 'lint'], function (callback) {
runSequence([
'build-sass',
'ractive-build-templates',
'ractive-build-test-templates',
'ractive-build-plugins',
'ractive-build-components',
'build-documentation-plugins',
'build-documentation-components'
], [
'copy-vendors',
'copy-use-cases',
'concat-app'
], [
'concat-app-umd'
], callback);
});
gulp.task('clean-dist', function (callback) {
return del([
'dist/**/*'
], callback);
});
gulp.task('dist', ['clean-dist', 'build'], function () {
return mergeStream(
gulp.src([
'./public/js/ractivef-umd.js'
])
.pipe(plugins.rename(function (path) {
// Rename the dist file to 'ractivef'
path.dirname = '';
path.basename = 'ractivef';
}))
.pipe(gulp.dest('dist')),
gulp.src([
'./public/manifest-rf.json',
'./public/js/lodash-compat/*',
'./public/js/hammerjs/hammer.min.js',
'./public/js/ractive-touch/*',
'./public/js/ractive-events-tap/dist/*'
], { base: process.cwd() })
.pipe(rebaseDist())
.pipe(gulp.dest('dist')),
gulp.src([
'public/css/**/*.css'
]).pipe(gulp.dest('dist/css'))
);
});
gulp.task('version-check', function (callback) {
exec('node ./scripts/versionCheck.js', function (err, stdout) {
if (stdout) {
gutil.log(gutil.colors.red(stdout));
}
if (err) {
gutil.log(gutil.colors.red('Exiting...'));
process.exit(1);
}
callback(err);
});
});
gulp.task('watch', function () {
var self = this;
plugins.watch([
'src/*.html',
'src/pages/*.html',
'src/blank-pages/*.html',
'src/**/*.json',
'src/**/*.hbs',
'src/**/*.md',
'src/**/*.js',
'src/**/*.scss'
], function () {
runSequence('build', 'html', function (err) {
self.emit('end');
});
});
});
gulp.task('a11y-only', [ 'a11y-connect' ], function (callback) {
rfA11y.auditComponents({ port: A11Y_SERVER_PORT }, args)
.then(function () {
callback();
process.exit(0);
})
.catch(function (error) {
callback(new Error(error));
process.exit(1);
});
});
// Run the test suite alone, without re-building the project. Useful for rapid test debugging.
// See 'test' for the full build and test task.
gulp.task('test-only', [ 'test-connect' ], function (callback) {
var selServer = seleniumServer(),
globFeature = [],
globStep = [],
componentName = args.component || '',
paths = [];
if (args.component) {
paths = [
'./src/components/%s/*.feature'.replace('%s', componentName)
];
globFeature = glob(paths);
if (!globFeature.length) {
gutil.log(gutil.colors.red.bold('Couldn\'t find requested component/widget, running whole suite'));
}
}
if (args.plugin) {
var pluginName = args.plugin || '';
var paths = [
'./src/plugins/%s/*.feature'.replace('%s', pluginName)
];
globFeature = glob(paths);
if (!globFeature.length) {
gutil.log(gutil.colors.red.bold('Couldn\'t find requested component/widget, running whole suite'));
}
}
if (!globFeature.length) {
paths = [
'./src/components/**/*.feature',
'./src/plugins/**/*.feature'
];
globFeature = glob(paths);
}
globStep = [
'./src/components/**/*.steps.js',
'./src/plugins/**/*.steps.js'
];
selServer.init().then(function () {
var stream = gulp.src(globFeature)
.pipe(rfCucumber(
{ steps: globStep }
));
stream.on('end', function () {
selServer.killServer().then(function () {
callback();
process.exit(0);
}).catch(function () {
callback();
process.exit(0);
});
});
stream.on('error', function (err) {
var errorCode = err ? 1 : 0;
selServer.killServer().then(function () {
callback(err);
process.exit(errorCode);
}).catch(function () {
callback(err);
process.exit(errorCode);
});
});
}).catch(gutil.log);
});
gulp.task('cordova-clean', function (callback) {
return del([
'.cordova/**/*'
], callback);
});
gulp.task('cordova-create', ['cordova-clean'], function () {
var options = {
dir: '.cordova',
id: 'com.ractiveFoundationDemo.sample',
name: 'Ractive Foundation Demo'
};
return gulp.src('public')
.pipe(cordovaCreate(options))
.pipe(cordovaAuthor('Ractive Foundation Team', ''))
.pipe(cordovaDescription('Ractive Foundation Demo'))
.pipe(cordovaVersion(pkg.version));
});
gulp.task('cordova-build', ['cordova-create'], function (callback) {
if (args.ios) {
return gulp.src('.cordova')
.pipe(cordovaIos(true));
} else if (args.android) {
return gulp.src('.cordova')
.pipe(cordovaAndroid(true))
.pipe(gulp.dest('apk'));
} else {
return gulp.src('.cordova')
.pipe(cordovaIos(true))
.pipe(cordovaAndroid(true))
.pipe(gulp.dest('apk'));
}
});
gulp.task('cordova-run', function (callback) {
var platform = args.ios ? 'ios' : 'android';
exec('(cd ./.cordova/ && cordova run ' + platform + ')', function (err, stdout) {
if (stdout) {
gutil.log(gutil.colors.red(stdout));
}
if (err) {
gutil.log(gutil.colors.red('Exiting...'));
process.exit(1);
}
callback(err);
});
});
// Build and test the project. Default choice. Used by npm test.
gulp.task('test', function (callback) {
runSequence([ 'version-check', 'build' ], 'test-only', callback);
});
// Currently a11y not part of standard build/test process.
gulp.task('a11y', function (callback) {
runSequence([ 'version-check', 'build' ], 'a11y-only', callback);
});
gulp.task('lint', ['lint-indentation'], function (callback) {
return gulp.src('./src/**/*.js')
.pipe(plugins.jshint('./.jshintrc'))
.pipe(plugins.jshint.reporter('jshint-stylish'))
.pipe(jscs())
.pipe(jscs.reporter())
.pipe(jscs.reporter('fail'))
.pipe(jshintFailReporter());
});
gulp.task('lint-indentation', function (callback) {
return gulp.src([
'src/**',
'tasks/**',
'scripts/**',
'gulpfile.js'
])
.pipe(plugins.indentChecker({
warn: false,
throwAtEnd: true,
jsdoc: true,
type: 'tab'
}));
});
gulp.task('default', function () {
var self = this;
runSequence('version-check', 'lint', 'build', 'connect', 'watch', function (err) {
self.emit('end');
});
});