-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
107 lines (93 loc) · 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
var gulp = require('gulp');
var less = require('gulp-less');
var concat = require('gulp-concat');
var del = require('del');
var runSequence = require('run-sequence');
var sourcemaps = require('gulp-sourcemaps');
var angularInjector = require('gulp-angular-injector');
var ngAnnotate = require('gulp-ng-annotate');
var rename = require('gulp-rename');
var through2 = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var yaml = require('gulp-yaml');
var uglify = require('gulp-uglify');
var paths = {
scripts: 'src/*.js',
resources: ['src/*.html', 'src/*.png'],
less: 'src/*.less',
questions: 'src/quiz.yml',
questionsOut: 'quiz.txt',
build: 'dist'
}
// ---------------------------------------------------------------------------
// Tasks
// ---------------------------------------------------------------------------
gulp.task('default', function() {
runSequence('clean', ['scripts', 'resources', 'css', 'encode-questions']);
});
gulp.task('encode-questions', function() {
gulp.src(paths.questions)
.pipe(yaml({ space: 2 }))
.pipe(encodeQuestions())
.pipe(rename(paths.questionsOut))
.pipe(gulp.dest(paths.build));
});
gulp.task('scripts', function() {
gulp.src(paths.scripts)
.pipe(sourcemaps.init())
// Convert from ES6 to ES5
.pipe(ngAnnotate())
.pipe(gulp.dest(paths.build))
.pipe(uglify())
// Minify the file, rename it, write the source map and save the
// minified file.
.pipe(sourcemaps.write('.'))
.pipe(rename("quiz.min.js"))
.pipe(gulp.dest(paths.build));
});
gulp.task('resources', function() {
gulp.src(paths.resources)
.pipe(gulp.dest(paths.build));
});
gulp.task('css', function() {
gulp.src(paths.less)
.pipe(less())
.pipe(gulp.dest(paths.build));
});
gulp.task('clean', function() {
del(paths.build);
});
gulp.task('watch', function() {
gulp.watch(paths.scripts, ['scripts']);
gulp.watch(paths.questions, ['encode-questions']);
gulp.watch(paths.resources, ['resources']);
gulp.watch(paths.less, ['css']);
});
// ---------------------------------------------------------------------------
// Local Plugins
// ---------------------------------------------------------------------------
function encodeQuestions(opts) {
return through2.obj(function(file, enc, cb) {
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) {
this.emit('error', new PluginError({
plugin: 'encodeQuestions',
message: 'streams are not supported'
}));
return cb();
}
if (file.isBuffer()) {
var contents = file.contents;
var encoded = contents.toString('base64');
var questions = JSON.parse(contents.toString());
gutil.log("Encoded " + questions.questions.length + " questions.");
file.contents = new Buffer(encoded);
this.push(file);
return cb();
}
});
}