forked from Leafpub/leafpub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
455 lines (403 loc) · 16.2 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
'use strict';
var gulp = require('gulp-help')(require('gulp')),
autoprefixer = require('gulp-autoprefixer'),
cleanCSS = require('gulp-clean-css'),
del = require('del'),
fs = require('fs-extra'),
imagemin = require('gulp-imagemin'),
jshint = require('gulp-jshint'),
notify = require('gulp-notify'),
path = require('path'),
preprocess = require('gulp-preprocess'),
rename = require('gulp-rename'),
sass = require('gulp-sass'),
uglify = require('gulp-uglify'),
watch = require('gulp-watch');
////////////////////////////////////////////////////////////////////////////////////////////////////
// Config
////////////////////////////////////////////////////////////////////////////////////////////////////
var assets = 'app/source/assets',
fonts = {
base : 'node_modules/font-awesome/fonts',
source: 'node_modules/font-awesome/fonts/**/*.+(eot|svg|ttf|woff|woff2|otf)',
target: assets + '/font'
},
images = {
source: 'app/source/images/**/*.+(gif|jpg|jpeg|png|svg)',
target: assets + '/img'
},
scripts = {
source: [
'app/source/scripts/**/*.js',
'!app/source/scripts/modules/**/*.js',
'node_modules/jquery/dist/jquery.js'
],
modules: 'app/source/scripts/modules/**/*.js',
target: assets + '/js'
},
styles = {
source: 'app/source/styles/**/*.scss',
target: assets + '/css'
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// Build functions
////////////////////////////////////////////////////////////////////////////////////////////////////
// Copies font files to target
function buildFonts(source, target, base) {
return gulp.src(source, { base: base })
.pipe(gulp.dest(target))
.pipe(notify({
title: 'Leafpub',
icon: path.join(__dirname, 'app/source/images/logo-color.png'),
message: 'Build fonts task complete.',
onLast: true
}));
}
// Optimizes images in source and outputs them in target
function buildImages(source, target) {
return gulp.src(source)
.pipe(imagemin())
.pipe(gulp.dest(target))
.pipe(notify({
title: 'Leafpub',
icon: path.join(__dirname, 'app/source/images/logo-color.png'),
message: 'Build images task complete.',
onLast: true
}));
}
// Minifies scripts in source and outputs them in target
function buildScripts(source, target) {
return gulp.src(source)
.pipe(preprocess({
includeBase: __dirname
}))
.on('error', function(err) {
notify({
title: 'Leafpub',
icon: path.join(__dirname, 'app/source/images/ladybug.png'),
message: 'Error including scripts: ' + err
}).write(err);
this.emit('end');
})
.pipe(uglify({
preserveComments: 'license'
}))
.on('error', function(err) {
notify({
title: 'Leafpub',
icon: path.join(__dirname, 'app/source/images/ladybug.png'),
message: 'Error minifying scripts: ' + err
}).write(err);
this.emit('end');
})
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(target))
.pipe(notify({
title: 'Leafpub',
icon: path.join(__dirname, 'app/source/images/logo-color.png'),
message: 'Build scripts task complete.',
onLast: true
}));
}
// Compiles styles in source and outputs them in target
function buildStyles(source, target) {
return gulp.src(source)
.pipe(sass({
includePaths: [
'node_modules'
],
precision: 8,
outputStyle: 'compressed'
}))
.on('error', function(err) {
notify({
title: 'Leafpub',
icon: path.join(__dirname, 'app/source/images/ladybug.png'),
message: 'Error compiling styles: ' + err
}).write(err);
this.emit('end');
})
.pipe(autoprefixer({
browsers: ['last 2 versions']
}))
.pipe(cleanCSS({
keepBreaks: true,
keepSpecialComments: 1
}))
.pipe(gulp.dest(target))
.pipe(notify({
title: 'Leafpub',
icon: path.join(__dirname, 'app/source/images/logo-color.png'),
message: 'Build styles task complete.',
onLast: true
}));
}
// Runs source through JSHint
function lintScripts(source) {
return gulp.src(source)
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('jshint-stylish'))
.on('error', function(err) {
notify({
title: 'Leafpub',
icon: path.join(__dirname, 'app/source/images/ladybug.png'),
message: 'Error linting scripts: ' + err
}).write(err);
this.emit('end');
});
}
// Removes unused files from app/source/vendor
function pruneVendor() {
//
// Beware: the glob pattern ** matches all children AND the parent! You must explicitly ignore
// ALL the parent directories as well as the files you wish to keep.
//
return del.sync([
// abeautifulsite/simpleimage
'app/source/vendor/abeautifulsite/simpleimage/example',
'app/source/vendor/abeautifulsite/simpleimage/.gitignore',
'app/source/vendor/abeautifulsite/simpleimage/composer.json',
// container-interop/container-interop
'app/source/vendor/container-interop/container-interop/docs',
'app/source/vendor/container-interop/container-interop/.gitignore',
'app/source/vendor/container-interop/container-interop/composer.json',
'app/source/vendor/container-interop/container-interop/README.md',
// erusev/parsedown
'app/source/vendor/erusev/parsedown/test',
'app/source/vendor/erusev/parsedown/.travis.yml',
'app/source/vendor/erusev/parsedown/composer.json',
'app/source/vendor/erusev/parsedown/phpunit.xml.dist',
'app/source/vendor/erusev/parsedown/README.md',
// firebase/php-jwt
'app/source/vendor/firebase/php-jwt/tests',
'app/source/vendor/firebase/php-jwt/.gitignore',
'app/source/vendor/firebase/php-jwt/.travis.yml',
'app/source/vendor/firebase/php-jwt/composer.json',
'app/source/vendor/firebase/php-jwt/composer.lock',
'app/source/vendor/firebase/php-jwt/package.xml',
'app/source/vendor/firebase/php-jwt/README.md',
'app/source/vendor/firebase/php-jwt/run-tests.sh',
'app/source/vendor/firebase/php-jwt/phpunit.xml.dist',
// nikic/fast-route
'app/source/vendor/nikic/fast-route/test',
'app/source/vendor/nikic/fast-route/.hhconfig',
'app/source/vendor/nikic/fast-route/.travis.yml',
'app/source/vendor/nikic/fast-route/composer.json',
'app/source/vendor/nikic/fast-route/FastRoute.hhi',
'app/source/vendor/nikic/fast-route/phpunit.xml',
'app/source/vendor/nikic/fast-route/README.md',
// pimple/pimple
'app/source/vendor/pimple/pimple/ext/tests',
'app/source/vendor/pimple/pimple/src/Pimple/Tests',
'app/source/vendor/pimple/pimple/.gitignore',
'app/source/vendor/pimple/pimple/.travis.yml',
'app/source/vendor/pimple/pimple/CHANGELOG',
'app/source/vendor/pimple/pimple/composer.json',
'app/source/vendor/pimple/pimple/phpunit.xml.dist',
'app/source/vendor/pimple/pimple/README.rst',
// slim/slim
'app/source/vendor/slim/slim/example',
'app/source/vendor/slim/slim/composer.json',
'app/source/vendor/slim/slim/CONTRIBUTING.md',
'app/source/vendor/slim/slim/README.md',
// tinymce/tinymce
'app/source/vendor/tinymce/tinymce/**',
'!app/source/vendor/tinymce/tinymce',
'!app/source/vendor/tinymce/tinymce/tinymce.min.js',
'!app/source/vendor/tinymce/tinymce/license.txt',
'!app/source/vendor/tinymce/tinymce/plugins',
'!app/source/vendor/tinymce/tinymce/plugins/lists',
'!app/source/vendor/tinymce/tinymce/plugins/lists/plugin.min.js',
'!app/source/vendor/tinymce/tinymce/plugins/paste',
'!app/source/vendor/tinymce/tinymce/plugins/paste/plugin.min.js',
'!app/source/vendor/tinymce/tinymce/plugins/table',
'!app/source/vendor/tinymce/tinymce/plugins/table/plugin.min.js',
'!app/source/vendor/tinymce/tinymce/plugins/textpattern',
'!app/source/vendor/tinymce/tinymce/plugins/textpattern/plugin.min.js',
'!app/source/vendor/tinymce/tinymce/themes',
'!app/source/vendor/tinymce/tinymce/themes/modern',
'!app/source/vendor/tinymce/tinymce/themes/modern/theme.min.js',
// zordius/lightncandy
'app/source/vendor/zordius/lightncandy/.github',
'app/source/vendor/zordius/lightncandy/build',
'app/source/vendor/zordius/lightncandy/specs',
'app/source/vendor/zordius/lightncandy/tests',
'app/source/vendor/zordius/lightncandy/.gitignore',
'app/source/vendor/zordius/lightncandy/.gitmodules',
'app/source/vendor/zordius/lightncandy/.scrutinizer.yml',
'app/source/vendor/zordius/lightncandy/.travis.yml',
'app/source/vendor/zordius/lightncandy/HISTORY.md',
'app/source/vendor/zordius/lightncandy/README.md',
'app/source/vendor/zordius/lightncandy/UPGRADE.md',
'app/source/vendor/zordius/lightncandy/composer.json',
'app/source/vendor/zordius/lightncandy/example_debug.png',
'app/source/vendor/zordius/lightncandy/phpunit.xml'
]);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Build tasks
////////////////////////////////////////////////////////////////////////////////////////////////////
// Build fonts
gulp.task('build:fonts', 'Build font assets.', ['clean:fonts'], function() {
buildFonts(fonts.source, fonts.target, fonts.base);
});
// Build images
gulp.task('build:images', 'Optimize images.', ['clean:images'], function() {
buildImages(images.source, images.target);
});
// Prune vendor packages
gulp.task('build:prune', 'Prune unused files from vendor packages.', function() {
pruneVendor();
});
// Build scripts
gulp.task('build:scripts', 'Build scripts.', ['jshint', 'clean:scripts'], function() {
buildScripts(scripts.source, scripts.target);
});
// Build styles
gulp.task('build:styles', 'Build styles.', ['clean:styles'], function() {
buildStyles(styles.source, styles.target);
});
// Build all
gulp.task('build', 'Run all build tasks.', [
'build:fonts',
'build:images',
'build:prune',
'build:scripts',
'build:styles'
]);
////////////////////////////////////////////////////////////////////////////////////////////////////
// Clean tasks
////////////////////////////////////////////////////////////////////////////////////////////////////
// Clean fonts
gulp.task('clean:fonts', 'Delete generated fonts.', function() {
return del(fonts.target);
});
// Clean images
gulp.task('clean:images', 'Delete generated images.', function() {
return del(images.target);
});
// Clean scripts
gulp.task('clean:scripts', 'Delete generated scripts.', function() {
return del(scripts.target);
});
// Clean styles
gulp.task('clean:styles', 'Delete generated styles.', function() {
return del(styles.target);
});
// Clean all
gulp.task('clean', 'Clean up generated files.', [
'clean:fonts',
'clean:images',
'clean:scripts',
'clean:styles'
], function() {
return del(assets);
});
////////////////////////////////////////////////////////////////////////////////////////////////////
// Release tasks
////////////////////////////////////////////////////////////////////////////////////////////////////
// Generate a release
gulp.task('release:make', 'Generate a release.', function() {
var config = require(path.join(__dirname, 'package.json')),
dist = path.join(__dirname, 'dist'),
target = path.join(dist, 'leafpub-' + config.version);
// Delete the target directory if it exists
del.sync(target);
// Create dist directory
fs.mkdirsSync(dist);
// Copy app/ to dist/leafpub-<version>/
fs.copySync(path.join(__dirname, 'app'), target);
// Copy license and installation instructions
fs.copySync(path.join(__dirname, 'LICENSE.md'), path.join(target, 'LICENSE.md'));
fs.copySync(path.join(__dirname, 'INSTALL.md'), path.join(target, 'INSTALL.md'));
// Inject version number into runtime.php
try {
fs.writeFileSync(
path.join(target, 'source/runtime.php'),
fs.readFileSync(path.join(target, 'source/runtime.php'))
.toString()
.replace('{{version}}', config.version)
);
} catch(err) {
return console.error(err);
}
// Delete .htaccess, database.php
del.sync(path.join(target, '.htaccess'));
del.sync(path.join(target, 'database.php'));
// Empty backups, content/cache, content/themes, content/uploads
del.sync(path.join(target, 'backups/*'));
del.sync(path.join(target, 'content/cache/*'));
del.sync([
path.join(target, 'content/themes/*'),
'!' + path.join(target, 'content/themes/range')
]);
del.sync([
path.join(target, 'content/uploads/*'),
'!' + path.join(target, 'content/uploads/2016')
]);
del.sync(path.join(target, 'content/plugins/*'));
// Prune source/images, source/scripts, and source/styles
del.sync(path.join(target, 'source/images/**'));
del.sync(path.join(target, 'source/scripts/**'));
del.sync(path.join(target, 'source/styles/**'));
// delete log folder
del.sync(path.join(target, 'log/*'));
// Little message to celebrate
console.log(
'\nLeafpub ' + config.version + ' has been released! 🎉\n\n' +
'Location: ' + target + '\n'
);
});
// Clean releases
gulp.task('release:clean', 'Delete all generated releases.', function() {
return del(path.join(__dirname, 'dist'));
});
////////////////////////////////////////////////////////////////////////////////////////////////////
// Other tasks
////////////////////////////////////////////////////////////////////////////////////////////////////
// JSHint
gulp.task('jshint', 'Lint source scripts with JSHint.', function() {
lintScripts(scripts.source);
});
// Watch for changes
gulp.task('watch', 'Watch for script and style changes.', function() {
// Watch fonts
gulp.src(fonts.source)
.pipe(watch(fonts.source))
.on('add', (file) => buildFonts(file, fonts.target))
.on('change', (file) => buildFonts(file, fonts.target));
// Watch images
gulp.src(images.source)
.pipe(watch(images.source))
.on('add', (file) => buildImages(file, images.target))
.on('change', (file) => buildImages(file, images.target));
// Watch scripts
gulp.src(scripts.source)
.pipe(watch(scripts.source))
.on('add', (file) => {
lintScripts(file);
buildScripts(file, scripts.target);
})
.on('change', (file) => {
lintScripts(file);
buildScripts(file, scripts.target)
});
// Watch script modules (compile into lib.min.js)
gulp.src(scripts.modules)
.pipe(watch(scripts.modules))
.on('add', (file) => {
buildScripts(scripts.source, scripts.target);
})
.on('change', (file) => {
buildScripts(scripts.source, scripts.target);
});
// Watch styles
gulp.src(styles.source)
.pipe(watch(styles.source))
// Recompile all styles since changes to _partials.scss won't
// compile on their own.
.on('add', (file) => buildStyles(styles.source, styles.target))
.on('change', (file) => buildStyles(styles.source, styles.target));
});
// Default
gulp.task('default', 'Run the default task.', ['watch']);