Skip to content

Instantly share code, notes, and snippets.

@smcelhinney
Created March 18, 2015 00:50
Show Gist options
  • Save smcelhinney/57013f687705e695d32f to your computer and use it in GitHub Desktop.
Save smcelhinney/57013f687705e695d32f to your computer and use it in GitHub Desktop.
var
gulp = require('gulp'),
$$ = require('gulp-load-plugins')(),
del = require('del'),
fs = require('fs'),
runSequence = require('run-sequence'),
server = require('./server/index'),
merge = require('event-stream').merge,
stylish = require('jshint-stylish'),
LessPluginCleanCSS = require('less-plugin-clean-css'),
LessPluginAutoPrefix = require('less-plugin-autoprefix'),
lessPluginGlob = require('less-plugin-glob'),
cleancss = new LessPluginCleanCSS({
advanced: true
}),
autoprefix = new LessPluginAutoPrefix({
browsers: [
'last 2 versions', 'ie 9'
]
}),
appDir = 'client/src/',
lrPort = null,
bowerDir = 'client/bower_components/',
handleError;
handleError = function(message) {
$$.util.log($$.util.colors.red(message));
this.end();
};
// Cleaning
gulp.task('clean', function(cb) {
del([
'client/public/'
], cb);
});
gulp.task('serve', function() {
server.app.listen(server.prodPort);
});
gulp.task('serve:dev', function() {
server.app.listen(server.devPort);
});
gulp.task('setup', function() {
return gulp.src([
'./bower.json',
'./package.json'
])
.pipe($$.install())
.on('error', handleError);
});
gulp.task('copy:resources', function() {
return gulp.src('resources/**/*.*')
.pipe(gulp.dest('client/public/resources'));
});
gulp.task('copy:vendor-assets', function() {
return gulp.src(bowerDir + '**/*.*')
// TODO minify
.pipe(gulp.dest('client/public/vendor'));
});
gulp.task('bundle-styles', function() {
return gulp.src([
'client/src/*.less'
])
.pipe($$.plumber())
.pipe($$.sourcemaps.init())
.pipe($$.less({
plugins: [autoprefix, cleancss, lessPluginGlob]
}))
.pipe($$.sourcemaps.write())
.pipe(gulp.dest('client/public/css/'));
});
gulp.task('bundle-common', function() {
return merge(
gulp.src([
appDir + '/common/**/*.html'
])
.pipe($$.angularTemplatecache({
root: 'common'
})),
gulp.src([
appDir + '/common/**/*.js',
'!' + appDir + '/common/**/*.test.js',
appDir + '/app.js'
])
.pipe($$.jscs())
.on('error', function(message) {
$$.util.log($$.util.colors.white(message));
})
.pipe($$.babel())
.pipe($$.jshint('./.jshintrc'))
.pipe($$.jshint.reporter(stylish))
)
.pipe($$.concat('common.js'))
.pipe(gulp.dest('client/public/js/'));
});
gulp.task('bundle-entities', function() {
return merge(
gulp.src([
appDir + '/**/*.html',
'!' + appDir + '/common/**/*.html',
'!' + appDir + '/modules/**/*.html'
])
.pipe($$.angularTemplatecache({
// root: folder.replace('modules/', '')
})),
gulp.src([
appDir + '/**/*.js',
'!' + appDir + '/**/*.test.js',
'!' + appDir + '/common/**/*.js',
'!' + appDir + '/modules/**/*.js'
])
.pipe($$.jscs())
.on('error', function(message) {
$$.util.log($$.util.colors.white(message));
})
.pipe($$.babel())
.pipe($$.jshint('./.jshintrc'))
.pipe($$.jshint.reporter(stylish))
)
.pipe($$.concat('app.js'))
.pipe(gulp.dest('client/public/js/'));
});
gulp.task('check-style', function() {
return gulp.src([
appDir + '/**/*.js'
])
.pipe($$.jscs())
.pipe($$.jshint('./.jshintrc'))
.pipe($$.jshint.reporter(stylish))
.pipe($$.jshint.reporter('fail'));
});
gulp.task('jshint', function() {
return gulp.src([
appDir + '/**/*.js'
])
.pipe($$.jscs())
.pipe($$.jshint('./.jshintrc'))
.pipe($$.jshint.reporter(stylish))
.pipe($$.jshint.reporter('fail'));
});
gulp.task('unit-tests', function() {
return gulp.src('not/a/real/path/*.js')
.pipe($$.karma({
configFile: 'karma.conf.js',
action: 'start'
}))
.on('error', function(err) {
throw err;
});
});
// This will stay here for historical importance.
gulp.task('kill-the-jade', function() {
gulp.src([
'**/*.jade'
])
.pipe($$.plumber())
.pipe($$.jade({
pretty: true
}))
.pipe(gulp.dest('.'));
});
// -----------------------------------------------
gulp.task('reload', function() {
return gulp.src([
'public/js/common.js',
'public/js/app.js'
])
.pipe($$.livereload(lrPort));
});
gulp.task('jsdoc', ['clean:docs'], function() {
return gulp.src(appDir + '/**/*.js')
.pipe($$.jsdoc('public/docs'));
});
gulp.task('watch', function() {
$$.livereload.listen();
gulp.watch([
appDir + '/**/*.js',
appDir + '/**/*.html'
], function() {
runSequence(
['bundle-entities', 'bundle-common'],
'reload'
);
});
gulp.watch([
'resources/**/*.*'
], ['copy:resources']);
gulp.watch([
appDir + '/**/*.less'
], function() {
runSequence('bundle-styles', 'reload');
});
});
gulp.task('test', function(cb) {
runSequence(
'setup',
'check-style',
'jshint',
'unit-tests',
cb
);
});
gulp.task('production', function(cb) {
runSequence(
[
'bundle-common',
'bundle-entities',
'bundle-styles',
'copy:resources',
'copy:vendor-assets'
],
cb
);
});
gulp.task('default', function(cb) {
runSequence(
'clean',
'setup', [
'bundle-common',
'bundle-entities',
'bundle-styles',
'copy:resources',
'copy:vendor-assets'
],
'serve:dev',
'watch',
'reload',
cb
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment