This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgulpfile.babel.js
246 lines (227 loc) · 6.36 KB
/
gulpfile.babel.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
/**
* # DEPENDENCIES #
*/
var gulp = require('gulp')
try {
var browserSync = require('browser-sync')
var reload = browserSync.reload // reload shorthand
} catch (e) { }
var path = require('path')
var join = path.join // shorthand
var addsrc = require('gulp-add-src')
var del = require('del')
var run = require('run-sequence')
var _ = require('lodash')
// metalsmith building
var build = require('./build.js')
var buildIndexes = require('./build-indexes.js')
// use no-op (callback) if BUILD_PDF is false
var buildPDF = process.env.BUILD_PDF === 'false' ? (d) => d() : require('./build-pdf.js')
var buildSearchIndex = require('./build-search-index.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('browserify')
var watchify = require('watchify')
var source = require('vinyl-source-stream')
var sourcemaps = require('gulp-sourcemaps')
var buffer = require('vinyl-buffer')
// 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')
// Inform user about BUILD_PDF environment values
if (process.env.BUILD_PDF === 'false') {
console.log('BUILD_PDF environment value is false, not building PDFs')
}
/**
* # TASKS #
*/
/**
* serve build directory
*/
gulp.task('server', ['build', 'build-indexes', 'css', 'js:client', 'js:vendor', 'assets'], function () {
browserSync.init({
server: { baseDir: config.buildRoot },
ghostMode: false
})
})
/**
* 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/highlight.js/styles/idea.css',
'node_modules/intro.js/introjs.css'
]))
.pipe(autoprefixer())
.pipe(minify())
.pipe(concat('style.min.css'))
.pipe(gulp.dest(config.assetRoot))
})
/**
* copy all assets to build directory
*/
gulp.task('assets', function () {
return gulp.src([
'assets/**/*',
'node_modules/bootstrap/dist/*/glyphicons-halflings-regular.*'
]).pipe(gulp.dest(config.assetRoot))
})
/**
* browserify and uglify client-side scripts
*/
var b = browserify({
entries: './scripts/index.js',
cache: {},
packageCache: {},
plugin: [watchify],
debug: true
}).transform('babelify', { presets: ['es2015'] })
b.on('log', console.log)
gulp.task('js:client', function () {
// do not uglify in dev env
return b.bundle()
.on('error', (err) => {
console.log(err)
this.emit('end')
})
.pipe(source('script.min.js'))
.pipe(gulp.dest(config.assetRoot))
})
gulp.task('js:dist', function () {
return b.bundle()
.on('error', (err) => {
console.log(err)
this.emit('end')
})
.pipe(source('script.min.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(config.assetRoot))
})
/**
* concat and uglify vendor scripts
*/
gulp.task('js:vendor', function () {
return gulp.src([
'node_modules/jquery/dist/jquery.min.js',
'node_modules/scratchblocks/src/scratchblocks.js',
'node_modules/scratchblocks/src/translations.js',
'node_modules/bootstrap/js/modal.js',
'node_modules/bootstrap/js/tooltip.js',
'node_modules/bootstrap/js/popover.js'
])
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.pipe(concat('vendor.min.js'))
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest(config.assetRoot))
})
/**
* metalsmith building
*/
gulp.task('build', build)
gulp.task('build-force', (cb) => {
build(cb, { force: true })
})
gulp.task('build-indexes', buildIndexes)
gulp.task('build-search-index', buildSearchIndex)
gulp.task('pdf', buildPDF)
/**
* dist - build all without serving
*/
gulp.task('dist', function (cb) {
run(['assets', 'build', 'build-indexes', 'build-search-index', 'css', 'js:dist', 'js:vendor'],
'pdf',
function (err) {
// make sure process exit (b = watchify bundle)
b.close()
cb(err)
})
})
/**
* clean - remove files in build directory
*/
gulp.task('clean', function (cb) {
del([join(config.lessonRoot, 'build')], {force: true}, cb)
})
/**
* 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('*', function (event) {
if (event !== 'pull_request') {
console.log('Webhook event "' + event + '". Nothing to do.')
}
})
github.on('pull_request', build)
github.listen()
var currentlyBuilding = false
function build (repo, ref, data) {
if (!data.pull_request.merged) {
console.log('Webhook event "pull_request", not merged. Nothing to do.')
return
}
if (currentlyBuilding) {
console.log('Already building. Waiting 4 minutes...')
_.delay(build, 4 * 60 * 1000, repo, ref, data)
return
}
currentlyBuilding = true
console.log('Merged PR, building...')
exec(config.ghMergeCommand, function (err, stdout, stderr) {
if (err !== null) {
console.log(stderr)
} else {
console.log('Build successfull.')
}
currentlyBuilding = false
})
}
})
/**
* # 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([join(config.sourceRoot, '/**'), '!' + config.sourceRoot + '/**/index.md'], ['build', reload])
gulp.watch([join(__dirname, '/layouts/**'), config.sourceRoot + '/**/index.md'], ['build-indexes', reload])
// styles
gulp.watch(join(__dirname, 'styles', '**', '*'), ['css', reload])
// scripts
gulp.watch(join(__dirname, 'scripts', '**'), ['js:client', reload])
// assets
gulp.watch(join(__dirname, 'assets', '**'), ['assets', reload])
})