Created
May 29, 2015 12:00
-
-
Save stianwestvig/1efcec391e07be4523ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var gutil = require('gulp-util'); | |
var gulpif = require('gulp-if'); | |
var notify = require('gulp-notify'); | |
var argv = require('yargs').argv; | |
var merge = require('merge-stream'); | |
var rename = require("gulp-rename"); | |
var rimraf = require('gulp-rimraf'); | |
var jade = require('gulp-jade'); | |
var sass = require('gulp-sass'); | |
var sourcemaps = require('gulp-sourcemaps'); | |
var minifycss = require('gulp-minify-css'); | |
var jshint = require('gulp-jshint'); | |
var stylish = require('jshint-stylish'); | |
var uglify = require('gulp-uglify'); | |
var concat = require('gulp-concat'); | |
// -------------------------- | |
// CMD ARGUMENTS | |
// gulp build --production | |
// -------------------------- | |
var production = !!argv.production; | |
var build = argv._.length ? argv._[0] === 'build' : false; | |
var watch = argv._.length ? argv._[0] === 'watch' : true; | |
// ---------------------------- | |
// ERROR NOTIFICATION | |
// ---------------------------- | |
var handleError = function(task) { | |
return function(err) { | |
notify.onError({ | |
message: task + ' failed, check the logs..', | |
sound: false | |
})(err); | |
gutil.log(gutil.colors.bgRed(task + ' error:'), gutil.colors.red(err)); | |
}; | |
}; | |
// -------------------------- | |
// PATHS AND GLOBS | |
// -------------------------- | |
var src_root = './src'; | |
var build_root = './build'; | |
var proto_root = './prototype'; | |
var dist_root = './dist/content/Static'; | |
var paths = { | |
src: { | |
jade: src_root + '/jade/*.jade', | |
sass: src_root + '/scss/*.scss', | |
node: src_root + '/*.js', | |
js: src_root + '/js/**', | |
jsRecurse: src_root + '/js/**/*.js', | |
img: src_root + '/img/*.*', | |
fonts: './bower_components/fontface-source-sans-pro/fonts/woff/otf/*.woff' | |
}, | |
build: { | |
clean: build_root + '/', | |
html: build_root + '/html', | |
css: build_root + '/css', | |
fonts: build_root + '/fonts', | |
img: build_root + '/img', | |
js: build_root + '/js' | |
}, | |
proto: { | |
clean: proto_root + '/public/', | |
cleanGlob: proto_root + '/**/*', | |
cleanExcept: '!' + proto_root + '/resources/', | |
public: proto_root + '/public', | |
node: proto_root + '/*.js', | |
index: proto_root + '/public/html/index.*', | |
css: proto_root + '/public/css', | |
html: proto_root + '/public/html', | |
js: proto_root + '/public/js' | |
}, | |
dist: { | |
clean: dist_root + '/' | |
} | |
}; | |
// -------------------------- | |
// TASK FUNCTIONS | |
// -------------------------- | |
var tasks = { | |
clean: function () { | |
return gulp.src([ | |
paths.build.clean, | |
paths.dist.clean, | |
paths.proto.cleanGlob, | |
paths.proto.cleanExcept | |
], {read: false, allowEmpty: true} | |
) | |
.pipe(rimraf()); | |
}, | |
jade: function () { | |
return gulp.src(paths.src.jade) | |
.pipe(jade({pretty: true})) | |
.on('error', handleError('JADE')) | |
.pipe(gulp.dest(paths.build.html)) | |
}, | |
indexMove: function () { | |
return gulp.src(paths.proto.index) | |
.pipe(rename({ dirname: '' })) | |
.pipe(gulp.dest(paths.proto.public)); | |
}, | |
indexDelete: function () { | |
return gulp.src(paths.proto.index, {read: false, allowEmpty: true}) | |
.pipe(rimraf()) | |
}, | |
sass: function () { | |
return gulp.src(paths.src.sass) | |
.pipe(gulpif(!production, sourcemaps.init())) | |
.pipe(sass({ | |
sourceComments: !production, | |
outputStyle: production ? 'compressed' : 'nested' | |
})) | |
.on('error', handleError('SASS')) | |
.pipe(minifycss()) | |
.pipe(gulpif(!production, sourcemaps.write({ | |
'includeContent': false, | |
'sourceRoot': '.' | |
}))) | |
.pipe(gulpif(!production, sourcemaps.init({ | |
'loadMaps': true | |
}))) | |
.pipe(sourcemaps.write({ | |
'includeContent': true | |
})) | |
.pipe(rename('bundle.css')) | |
.pipe(gulp.dest(paths.build.css)) | |
}, | |
serverCopy: function () { | |
return gulp.src(paths.src.node) | |
.pipe(gulp.dest(build_root)) | |
}, | |
lint: function () { | |
return gulp.src([ | |
paths.src.node, | |
paths.src.js | |
]).pipe(jshint()) | |
.pipe(jshint.reporter(stylish)) | |
.on('error', handleError('lint')); | |
}, | |
js: function () { | |
return gulp.src([paths.src.js]) | |
.pipe(concat('bundle.js')) | |
.pipe(uglify()) | |
.pipe(gulp.dest(paths.build.js)) | |
}, | |
fonts: function () { | |
return gulp.src(paths.src.fonts) | |
.pipe(gulp.dest(paths.build.fonts)) | |
}, | |
img: function () { | |
return gulp.src(paths.src.img) | |
.pipe(gulp.dest(paths.build.img)) | |
}, | |
protoCopy: function () { | |
var copyFiles = gulp.src([build_root + '/**/*.*', '!' + build_root + '/*.js'], { base: './build/' }) | |
.pipe(gulp.dest(paths.proto.public)); | |
var copyNode = gulp.src(build_root + '/deployd.js') | |
.pipe(rename('server.js')) | |
.pipe(gulp.dest(proto_root)); | |
return merge(copyFiles, copyNode); | |
}, | |
distCopy: function () { | |
return gulp.src([build_root + '/**/*.*', '!' + build_root + '/*.js'], { base: './build/' }) | |
.pipe(gulp.dest(dist_root)); | |
}, | |
watch: function () { | |
gulp.watch(paths.src.sass, ['sass', 'protoCopyCss']); | |
gulp.watch(paths.src.jade, ['jade', 'indexDelete']); | |
gulp.watch(paths.src.js, ['js', 'protoCopyJs']); | |
}, | |
protoCopyCss: function () { | |
return gulp.src(paths.build.css + '/bundle.css') | |
.pipe(gulp.dest(paths.proto.css)); | |
}, | |
protoCopyHtml: function () { | |
// todo: handle index.html in root too | |
return gulp.src(paths.build.html + '/*.html') | |
.pipe(gulp.dest(paths.proto.html)); | |
}, | |
protoCopyJs: function () { | |
return gulp.src(paths.build.js + '/**/*.js') | |
.pipe(gulp.dest(paths.proto.js)); | |
}, | |
test: function () { | |
console.log('running tests'); | |
console.log('karma start karma.conf.js'); | |
console.log('success'); | |
} | |
}; | |
// -------------------------- | |
// TASK DEFINITIONS | |
// -------------------------- | |
gulp.task('default', ['watch']); | |
gulp.task('watch', ['proto'], tasks.watch); // watches and triggers build of html, css, js and copies that asset type from ./build to ./prototype | |
gulp.task('proto', ['build'], tasks.protoCopy); // gets from ./build and puts files in ./prototype | |
gulp.task('dist', ['build'], tasks.distCopy); // gets from ./build and puts files in ./dist/contents/Public | |
gulp.task('runtests', tasks.test); | |
// build chain builds all to ./build | |
gulp.task('build', [ | |
'jade', | |
'sass', | |
'lint', | |
'serverCopy', | |
'js', | |
'fonts', | |
'img' | |
] | |
); | |
gulp.task('clean', tasks.clean); | |
gulp.task('jade', tasks.jade); | |
gulp.task('indexMove', ['jade', 'protoCopyHtml'], tasks.indexMove); // task so that index.html can live in root and not in /html folder (copy it) | |
gulp.task('indexDelete', ['indexMove'], tasks.indexDelete); // task so that index.html can live in root and not in /html folder (delete original) | |
gulp.task('sass', tasks.sass); | |
gulp.task('serverCopy', tasks.serverCopy); // put express (build) or deployd (prototype) and rename to server.js | |
gulp.task('lint', tasks.lint); | |
gulp.task('js', ['lint'], tasks.js); | |
gulp.task('fonts', tasks.fonts); | |
gulp.task('img', tasks.img); | |
gulp.task('protoCopyCss', ['sass'], tasks.protoCopyCss); // for use in watch, to copy only one asset type | |
gulp.task('protoCopyHtml', ['jade'], tasks.protoCopyHtml); // for use in watch, to copy only one asset type | |
gulp.task('protoCopyJs', ['js'], tasks.protoCopyJs); // for use in watch, to copy only one asset type | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment