Skip to content

Instantly share code, notes, and snippets.

@halcarleton
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halcarleton/997e446155ebfb932a47 to your computer and use it in GitHub Desktop.
Save halcarleton/997e446155ebfb932a47 to your computer and use it in GitHub Desktop.
Jekyll Gulpfile [WIP]
var gulp = require('gulp');
var fs = require('fs');
var spawn = require('child_process').spawn;
var through = require('through2');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
var gutil = require('gulp-util');
var $ = require('gulp-load-plugins')();
var jshintStylish = require('jshint-stylish');
var mainScriptFileName = 'main.js';
var ignore = [
'!karma.conf.js',
'!test/**/*',
'!.gitignore',
'!node_modules/**/*',
'!.sass-cache/**/*',
'!_site/**/*',
'!.jshintrc'
];
// TODO: Test task
// TODO: Build task
var compareAgainstFile = function (stream, cb, sourceFile, destPath, destFileName) {
'use strict';
var destPathTo = destPath.match(/^(\\|\/)?(.+(\\|\/))*/)[0];
var modDestPath = destPathTo+destFileName;
fs.stat(modDestPath, function (err, targetStat) {
if (err) {
if (err.code !== 'ENOENT') {
stream.emit('error', new gutil.PluginError('gulp-changed', err, {
fileName: sourceFile.path
}));
}
stream.push(sourceFile);
} else if (sourceFile.stat.mtime > targetStat.mtime) {
stream.push(sourceFile);
}
cb();
});
};
var jekyllBuild = function (cb) {
'use strict';
var cp = spawn('jekyll', ['build']),
stdout = '',
stderr = '';
cp.stdout.setEncoding('utf8');
cp.stdout.on('data', function (data) {
stdout += data;
gutil.log(data);
});
cp.stderr.setEncoding('utf8');
cp.stderr.on('data', function (data) {
stderr += data;
gutil.log(gutil.colors.red(data));
});
cp.on('error', function (err) {
gutil.log(gutil.colors.red('gulp-jekyll', err));
});
cp.on('close', function (code) {
gutil.log('Done with exit code ', code);
gutil.log('Jekyll has completed the build process.');
if (cb) {
cb();
}
});
};
/* Javascript */
gulp.task('jshint', function () {
'use strict';
return gulp.src('_scripts/**/*.js')
.pipe($.changed('scripts/', {hasChanged: function (stream, cb, sourceFile, destPath) {
var destFileName = mainScriptFileName;
compareAgainstFile(stream, cb, sourceFile, destPath, destFileName);
}}))
.pipe($.jshint('.jshintrc'))
.pipe($.jshint.reporter(jshintStylish));
});
gulp.task('concat', ['jshint'], function () {
'use strict';
return gulp.src('_scripts/**/*.js')
.pipe($.concat(mainScriptFileName))
.pipe(gulp.dest('scripts/'));
});
gulp.task('uglify', ['concat'], function () {
'use strict';
return gulp.src('scripts/'+mainScriptFileName)
.pipe($.uglify())
.pipe(gulp.dest('scripts/'));
});
/* CSS */
gulp.task('sass', function () {
'use strict';
return $.rubySass('_sass/', {style: 'nested', require: 'susy'})
.on('error', gutil.log)
.pipe($.autoprefixer('last 3 versions', '> 1%', 'ie 8'))
.pipe(gulp.dest('css/'))
.pipe(gulp.dest('_site/css/'))
.pipe(reload({stream: true}));
});
gulp.task('cssMinify', ['sass'], function () {
'use strict';
return gulp.src('css/**/*.css')
.pipe($.csso())
.pipe(gulp.dest('css/'));
});
/* Serve */
gulp.task('browser-sync', function () {
'use strict';
browserSync({
server: {
baseDir: './_site'
}
});
});
/* Test */
gulp.task('unitTest');
// TODO: gulp.task('e2e');
gulp.task('test', ['unitTest']);
/* Main Processes */
gulp.task('watch', ['concat', 'sass', 'browser-sync'], function () {
'use strict';
var watcherReporter = function (e) {
gutil.log(gutil.colors.blue('File changed (' + e.path + ')'));
};
var scriptWatcher = gulp.watch(['_scripts/**/*.js'].concat(ignore),['concat']);
var sassWatcher = gulp.watch(['_sass/**/*.scss'].concat(ignore), ['sass'])
var markupWatcher = gulp.watch(['**/*.html', '**/*.md', '**/*.markdown'].concat(ignore))
var siteWatcher = gulp.watch(['_site/index.html']); // Only watching one file to prevent multiple calls with every jekyllBuild call. jekyllBuild changes multiple files.
scriptWatcher.on('change', function (e) {
watcherReporter(e);
jekyllBuild();
});
sassWatcher.on('change', function (e) {
watcherReporter(e);
// Does not call jekyllBuild in order to allow browserSync style injection
});
markupWatcher.on('change', function (e) {
watcherReporter(e);
jekyllBuild();
});
siteWatcher.on('change', function (e) {
watcherReporter(e);
reload();
});
jekyllBuild();
});
gulp.task('build', ['uglify', 'cssMinify', 'test'], function () {
'use strict';
/**
** TODO: Don't build if javascript has errors(jsHint and through2)
** TODO: Don't build if tests fail
**/
jekyllBuild();
});
gulp.task('default', function () {
'use strict';
gutil.log(gutil.colors.red('There is currently nothing in this task. (default)'));
});
$.npmScriptSync(gulp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment