forked from arve0/codeclub_lesson_builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
235 lines (212 loc) · 5.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
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
/*
* # DEPENDENCIES #
*/
var gulp = require('gulp');
var browserSync = require('browser-sync');
var reload = browserSync.reload; // reload shorthand
var path = require('path');
var addsrc = require('gulp-add-src');
var del = require('del');
var run = require('run-sequence');
var merge = require('merge-stream');
var _ = require('lodash');
// html building
var build = require('./build.js');
// styles and scripts
var less = require('gulp-less');
var concat = require('gulp-concat');
var autoprefixer = require('gulp-autoprefixer');
var minify = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var browserify = require('gulp-browserify');
// archive
var zip = require('gulp-zip');
var fs = require('fs');
// pdf generation
var pdf = require('./pdf.js');
// link-checking
var checkLinks = require('./check-links');
// get configuration variables
var config = require('./config.js');
// github hooks
var exec = require('child_process').exec;
var githubhook = require('githubhook');
/*
* # VARIABLES #
*/
var assetRoot = config.assetRoot;
var buildRoot = config.buildRoot;
var lessonRoot = config.lessonRoot;
var sourceFolder = config.sourceFolder;
/*
* # TASKS #
*/
/*
* Create archive files for each subdir of buildRoot
* Each archive includes all assets.
*/
gulp.task('archive', function() {
var src_dirs = fs.readdirSync(buildRoot).filter(function(file) {
return fs.statSync(path.join(buildRoot, file)).isDirectory();
});
var streams = _.map(src_dirs, function (dirname){
if (dirname == 'assets') {
return;
}
return gulp.src([
buildRoot + '/index.html',
buildRoot + '/{assets,assets/**}',
buildRoot + '/{'+dirname+','+dirname+'/**}',
])
.pipe(zip(dirname + '.zip'))
.pipe(gulp.dest(buildRoot));
});
streams = _.compact(streams);
return merge(streams);
});
/*
* serve build directory
*/
gulp.task('server', ['force-build', 'css', 'js', 'assets'], function () {
browserSync.init({
server: { baseDir: buildRoot }
});
});
/*
* build less files to css, prefix and minify
*/
gulp.task('css', function(cb) {
return gulp.src('styles/main.less')
.pipe(less())
.on('error', cb)
.pipe(addsrc([
'node_modules/scratchblocks2/build/scratchblocks2.css',
'node_modules/metalsmith-code-highlight/node_modules/highlight.js/styles/idea.css'
]))
.pipe(autoprefixer())
.pipe(minify())
.pipe(concat('style.min.css'))
.pipe(gulp.dest(assetRoot));
});
/*
* copy all assets to build directory
*/
gulp.task('assets', function(){
return gulp.src([
'assets/**/*',
'node_modules/scratchblocks2/build/*/*.png',
'node_modules/bootstrap/dist/*/glyphicons-halflings-regular.*',
])
.pipe(gulp.dest(assetRoot));
});
/*
* browserify, concat and uglify scripts
*/
gulp.task('browserify', function() {
return gulp.src('scripts/index.js')
.pipe(browserify({
insertGlobals: true,
debug: true
}))
.pipe(uglify())
.pipe(concat('script.min.js'))
.pipe(gulp.dest(assetRoot));
});
/*
* concat and uglify vendor scripts
*/
gulp.task('js', ['browserify'], function(){
return gulp.src([
'node_modules/scratchblocks2/build/scratchblocks2.js',
'node_modules/scratchblocks2/src/translations.js',
'node_modules/bootstrap/js/tooltip.js'
])
.pipe(uglify())
.pipe(addsrc.prepend([
'node_modules/jquery/dist/jquery.min.js'
]))
.pipe(concat('vendor.min.js'))
.pipe(gulp.dest(assetRoot));
});
/*
* metalsmith building
*/
gulp.task('build', build);
gulp.task('force-build', function(done){
build(function(err){
done(err);
}, { // build options
force: true
});
});
/*
* dist - build all without serving
*/
gulp.task('dist', function(cb){
// preferred way to this will change in gulp 4
// see https://github.com/gulpjs/gulp/issues/96
run('clean',
['assets', 'build', 'css', 'js'],
'archive',
'pdf', // do not include pdf in zip files
cb);
});
/*
* clean - remove files in build directory
*/
gulp.task('clean', function(cb){
del([path.join(lessonRoot, 'build')], {force: true}, cb);
});
/*
* pdf - generate pdfs of all htmls
*/
gulp.task('pdf', pdf);
/*
* links - check for broken links
*/
gulp.task('links', checkLinks('http://localhost:3000/'));
gulp.task('prodlinks', checkLinks(config.productionCrawlStart));
/*
* github webhook for automatic building
*/
gulp.task('github', function(cb){
var github = githubhook({
host: config.ghHost,
port: config.ghPort,
path: config.ghPath,
secret: config.ghSecret
});
github.on('pull_request', function(repo, ref, data) {
if (data.pull_request.merged) {
deployProc = exec(config.ghPushCommand, function(err, stdout, stderr) {
if(err!==null) {
console.log(stderr);
}
});
}
});
github.listen();
});
/*
* # DEFAULT TASK #
* do metalsmith build
* build, concat and minify styles
* concat and uglify scripts
* copy assets
* serve build directory with livereload
* watch files -> build and reload upon changes
*/
gulp.task('default', ['server'], function(){
/*
* ## WATCHES ##
*/
// files which are built with metalsmith
gulp.watch([config.sourceRoot + '/**', '!' + config.sourceRoot + '/**/index.md'], ['build', reload]);
gulp.watch([__dirname + '/templates/**', config.sourceRoot + '/**/index.md'], ['force-build', reload]);
// styles
gulp.watch(path.join(__dirname, 'styles', '**', '*'), ['css', reload]);
// scripts
gulp.watch(path.join(__dirname, 'scripts', '**'), ['js', reload]);
// assets
gulp.watch(path.join(__dirname, 'assets', '**'), ['assets', reload]);
});