kwand-stack
Version:
Koa + WebSockets + Angular + Node + Dokku
226 lines (189 loc) • 6.53 kB
JavaScript
// THIS GULP FILE MUST BE RUN FROM `gulp --harmony` or equivalent
// Edit `which gulp` to call this correctly
var c = require('./config');
process.env.LIVERELOAD = true;
var htmld = c.clientd,
stylesd = c.clientd + '/styles',
scriptsd = c.clientd + '/scripts',
imagesd = c.clientd + '/images',
assetsd = c.clientd + '/assets',
gulp = require("gulp"),
glob = require("glob"),
co = require("co"),
fs = require("fs"),
p = require("gulp-load-plugins")();
var paths = {
stylus: [
// broken in gulp-stylus and reported
//c.srcClient + '/**/*.styl'
c.srcClient + '/styles/main.styl'
],
stylusSrc: [
//c.srcClient + '/styles/**/*.styl',
c.srcClient + '/styles/main.styl',
'!' + c.srcClient + '/styles/**/*~'
],
stylesAsis: [
c.srcClient + '/styles/**/*',
'!' + c.srcClient + '/styles/**/*{~,.styl}'
],
locals: __dirname + '/locals.js',
clean: [
c.clientd + '/*.html',
c.clientd + '/*.ico',
c.clientd + '/images',
c.clientd + '/scripts',
c.clientd + '/styles',
c.clientd + '/assets',
c.serverd
]
};
gulp.task('clean',function(){
return gulp.src(paths.clean, {read: false})
.pipe(p.clean());
});
//------------------------------- archive ---------------------------------
// git archive creates a tarbomb by nature, beware
gulp.task('archive', function(){
var exec = require("child_process").exec;
var archive = exec('git archive --format zip --output ' + __dirname + '/kwand-stack.zip master');
});
//------------------------------- scripts ---------------------------------
gulp.task('scripts-pkgs', function(){
var pkgDirs = glob.sync(c.srcClient + '/scripts/pkgs/*/');
pkgDirs.forEach(function(d){
var pkgName = d.match('/.+\/(.+)\/$')[1];
gulp.src(d + '*.js')
.pipe(p.jshint({"esnext": true}))
.pipe(p.traceur())
.pipe(p.uglify())
.pipe(p.concat(pkgName + '.pkg.min.js'))
.pipe(gulp.dest(scriptsd))
});
});
gulp.task('scripts-asis', function(){
return gulp.src(c.srcClient + '/scripts/*.min.js')
.pipe(p.traceur())
.pipe(gulp.dest(scriptsd));
});
gulp.task('scripts-plain', function(){
return gulp.src([c.srcClient + '/scripts/*.js',
'!' + c.srcClient + '/scripts/*.min.js'])
.pipe(p.traceur())
.pipe(p.jshint())
.pipe(p.uglify())
.pipe(gulp.dest(scriptsd));
});
gulp.task('scripts-before', function(){
return gulp.src([c.srcClient + '/scripts/before/*.js'])
.pipe(p.jshint())
.pipe(p.traceur())
.pipe(p.uglify())
.pipe(p.concat('before.min.js'))
.pipe(gulp.dest(scriptsd));
});
gulp.task('scripts-after', function(){
return gulp.src([c.srcClient + '/scripts/after/*.js'])
.pipe(p.jshint())
.pipe(p.traceur())
.pipe(p.uglify())
.pipe(p.concat('after.min.js'))
.pipe(gulp.dest(scriptsd));
});
gulp.task('scripts',['scripts-plain','scripts-asis','scripts-pkgs',
'scripts-before', 'scripts-after']);
//------------------------------- styles ---------------------------------
gulp.task('styles-stylus', function(){
return gulp.src(paths.stylusSrc)
.pipe(p.stylus({path: paths.stylus, errors: true}))
//.pipe(p.autoprefixer('last 2 versions'))
.pipe(gulp.dest(stylesd))
.pipe(p.minifyCss({keepSpecialComments: 0}))
.pipe(p.rename({extname: ".min.css"}))
.pipe(gulp.dest(stylesd));
});
gulp.task('styles-asis', function(){
return gulp.src(paths.stylesAsis)
.pipe(gulp.dest(stylesd));
});
gulp.task('styles',['styles-stylus','styles-asis']);
//------------------------------- images ---------------------------------
gulp.task('favicon',function(){
return gulp.src(c.srcClient + '/images/favicon.ico')
.pipe(gulp.dest(htmld));
});
gulp.task('raster',function(){
return gulp.src(c.srcClient + '/images/**/*.{jpg,jpeg,png,gif}')
.pipe(p.imagemin())
.pipe(gulp.dest(imagesd));
});
gulp.task('svg',function(){
return gulp.src(c.srcClient + '/images/**/*.{svg}')
.pipe(p.svgmin())
.pipe(gulp.dest(imagesd));
});
gulp.task('images', ['favicon', 'raster', 'svg']);
//------------------------------- html -----------------------------------
gulp.task('html-plain', function(){
return gulp.src([
c.srcClient + '/pages/**/*.html',
'!' + c.srcClient + '/pages/**/_*/**'
])
.pipe(p.minifyHtml())
.pipe(gulp.dest(htmld));
});
// TODO: refactor and co this
gulp.task('html-jade', function(){
if (fs.existsSync(paths.locals)) {
delete require.cache[require.resolve(paths.locals)];
locals = require(paths.locals);
return locals(function (err, data) {
return gulp.src([
c.srcClient + '/pages/**/*.jade',
'!' + c.srcClient + '/pages/**/_*/**'
])
.pipe(p.jade({ locals: data}))
.pipe(p.highlight())
.pipe(p.minifyHtml())
.pipe(gulp.dest(htmld));
});
} else {
return gulp.src([
c.srcClient + '/pages/**/*.jade',
'!' + c.srcClient + '/pages/**/_*/**'
])
.pipe(p.jade())
.pipe(p.highlight())
.pipe(p.minifyHtml())
}
});
gulp.task('html', ['html-plain', 'html-jade']);
//------------------------------- assets ---------------------------------
gulp.task('assets', function(){
return gulp.src(c.srcClient + '/assets/**/*')
.pipe(gulp.dest(assetsd));
});
//------------------------------- server ---------------------------------
gulp.task('server', function(){
return gulp.src(c.srcServer + '/**/*')
.pipe(gulp.dest(c.serverd));
});
//------------------------------- watch ----------------------------------
gulp.task('watch', function(){
p.nodemon({
script: c.serverd + '/server.js',
execMap: {js: "node --harmony"}
});
var lr = p.livereload();
var u = function(file) {lr.changed(file.path);};
gulp.watch(c.srcClient + '/styles/**/*', ['styles']).on('change',u);
gulp.watch(c.srcClient + '/scripts/**/*', ['scripts']).on('change',u);
//gulp.watch(c.srcClient + '/images/**/*', ['images']).on('change',u);
gulp.watch([c.srcClient + '/pages/**/*', 'locals.js'], ['html']).on('change',u);
gulp.watch(c.srcClient + '/assets/**/*', ['assets']).on('change',u);
gulp.watch(c.serverd + '/**/*', ['server']).on('change',u);
});
//------------------------------- default --------------------------------
//gulp.task('once',['scripts','styles','images','html','assets','server']);
gulp.task('once',['scripts','styles','html','assets','server','archive']);
gulp.task('default',['once','watch']);