|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var gulp = require('gulp'), |
| 4 | + gutil = require('gulp-util'), |
| 5 | + plumber = require('gulp-plumber'), |
| 6 | + pug = require('gulp-pug'), |
| 7 | + stylus = require('gulp-stylus'), |
| 8 | + rename = require('gulp-rename'), |
| 9 | + remark = require('gulp-remark'), |
| 10 | + frontMatter = require('gulp-gray-matter'), |
| 11 | + remarkHtml = require('remark-html'), |
| 12 | + attachToTemplate = require('gulp-attach-to-template'), |
| 13 | + filterDrafts = require('stratic-filter-drafts'), |
| 14 | + dateInPath = require('stratic-date-in-path'), |
| 15 | + postsToIndex = require('stratic-posts-to-index'), |
| 16 | + paginateIndexes = require('stratic-paginate-indexes'), |
| 17 | + indexesToRss = require('stratic-indexes-to-rss'), |
| 18 | + addsrc = require('gulp-add-src'), |
| 19 | + ecstatic = require('ecstatic'), |
| 20 | + ghpages = require('gh-pages'), |
| 21 | + path = require('path'), |
| 22 | + http = require('http'), |
| 23 | + through2 = require('through2'), |
| 24 | + isDist = process.argv.indexOf('serve') === -1; |
| 25 | + |
| 26 | +gulp.task('build', ['build:html', 'build:css', 'build:js', 'build:blog']); |
| 27 | +gulp.task('build:blog', ['build:blog:posts', 'build:blog:index', 'build:blog:rss']); |
| 28 | + |
| 29 | +gulp.task('build:html', function() { |
| 30 | + return gulp.src('src/*.pug') |
| 31 | + .pipe(isDist ? through2.obj() : plumber()) |
| 32 | + .pipe(pug({ pretty: true, basedir: __dirname })) |
| 33 | + .pipe(rename({ extname: '.html' })) |
| 34 | + .pipe(gulp.dest('dist')); |
| 35 | +}); |
| 36 | + |
| 37 | +gulp.task('build:blog:posts', function() { |
| 38 | + return gulp.src('src/blog/*.md') |
| 39 | + .pipe(isDist ? through2.obj() : plumber()) |
| 40 | + .pipe(frontMatter()) |
| 41 | + .pipe(filterDrafts()) |
| 42 | + .pipe(remark({quiet: true}).use(remarkHtml)) |
| 43 | + .pipe(dateInPath()) |
| 44 | + .pipe(addsrc('src/blog/post.pug')) |
| 45 | + .pipe(attachToTemplate('post.pug')) |
| 46 | + .pipe(pug({ pretty: true, basedir: __dirname })) |
| 47 | + .pipe(rename({ extname: '.html' })) |
| 48 | + .pipe(gulp.dest('dist/blog')); |
| 49 | +}); |
| 50 | + |
| 51 | +gulp.task('build:blog:index', function() { |
| 52 | + return gulp.src('src/blog/*.md') |
| 53 | + .pipe(isDist ? through2.obj() : plumber()) |
| 54 | + .pipe(frontMatter()) |
| 55 | + .pipe(filterDrafts()) |
| 56 | + .pipe(remark({quiet: true}).use(remarkHtml)) |
| 57 | + .pipe(dateInPath()) |
| 58 | + .pipe(addsrc('src/blog/index.pug')) |
| 59 | + .pipe(postsToIndex('index.pug')) |
| 60 | + .pipe(paginateIndexes()) |
| 61 | + .pipe(pug({ pretty: true, basedir: __dirname })) |
| 62 | + .pipe(rename({ extname: '.html' })) |
| 63 | + .pipe(gulp.dest('dist/blog')); |
| 64 | +}); |
| 65 | + |
| 66 | +gulp.task('build:blog:rss', function() { |
| 67 | + return gulp.src('src/blog/*.md') |
| 68 | + .pipe(frontMatter()) |
| 69 | + .pipe(filterDrafts()) |
| 70 | + .pipe(remark({quiet: true}).use(remarkHtml)) |
| 71 | + .pipe(dateInPath()) |
| 72 | + .pipe(addsrc('src/blog/index.pug')) |
| 73 | + .pipe(postsToIndex('index.pug')) |
| 74 | + .pipe(indexesToRss({ |
| 75 | + title: 'My personal blog' |
| 76 | + }, 'http://stratic.js.org/blog/')) |
| 77 | + .pipe(rename({ extname: '.rss' })) |
| 78 | + .pipe(gulp.dest('dist/blog')); |
| 79 | +}); |
| 80 | + |
| 81 | +gulp.task('build:css', function() { |
| 82 | + return gulp.src('src/styles/*.styl') |
| 83 | + .pipe(isDist ? through2.obj() : plumber()) |
| 84 | + .pipe(stylus()) |
| 85 | + .pipe(rename({ extname: '.css' })) |
| 86 | + .pipe(gulp.dest('dist/css')); |
| 87 | +}); |
| 88 | + |
| 89 | +gulp.task('build:js', function() { |
| 90 | + return gulp.src('src/scripts/*.js') |
| 91 | + .pipe(gulp.dest('dist/js')); |
| 92 | + |
| 93 | +}); |
| 94 | + |
| 95 | +gulp.task('build:images', function() { |
| 96 | + return gulp.src('src/img/*') |
| 97 | + .pipe(gulp.dest('dist/img')); |
| 98 | + |
| 99 | +}); |
| 100 | + |
| 101 | +gulp.task('deploy', ['build'], function(done) { |
| 102 | + ghpages.publish(path.join(__dirname, 'dist'), { logger: gutil.log, branch: 'master' }, done); |
| 103 | +}); |
| 104 | + |
| 105 | +gulp.task('serve', ['watch'], function() { |
| 106 | + http.createServer( |
| 107 | + ecstatic({ root: __dirname + '/dist' }) |
| 108 | + ).listen(process.env.PORT || 8080); |
| 109 | +}); |
| 110 | + |
| 111 | +gulp.task('watch', ['build'], function() { |
| 112 | + gulp.watch(['src/*.pug', 'src/includes/*.pug'], ['build:html', 'build:blog:posts', 'build:blog:index']); |
| 113 | + gulp.watch('src/styles/*.styl', ['build:css']); |
| 114 | + gulp.watch('src/scripts/*.js', ['build:js']); |
| 115 | + gulp.watch('src/blog/*.md', ['build:blog']); |
| 116 | + gulp.watch('src/blog/*.pug', ['build:blog:posts', 'build:blog:index']); |
| 117 | +}); |
0 commit comments