Skip to content

Instantly share code, notes, and snippets.

@devster
Created September 30, 2014 15:46
Show Gist options
  • Save devster/12fd6fa10ec8280abc5d to your computer and use it in GitHub Desktop.
Save devster/12fd6fa10ec8280abc5d to your computer and use it in GitHub Desktop.
var
path = require('path'),
_ = require('lodash'),
gulp = require('gulp'),
gulpUtil = require('gulp-util'),
log = gulpUtil.log,
colors = gulpUtil.colors,
glob = require('glob'),
fs = require('fs'),
gulpNotify = require('gulp-notify'),
Notification = require('node-notifier'),
notifier = new Notification(),
gulpIf = require('gulp-if'),
eventStream = require('event-stream'),
gulpChanged = require('gulp-changed'),
gulpRename = require('gulp-rename');
var options = {
projectName: 'Project Name',
assetsBuild: 'web/assets',
isRubySass: false,
isDev: gulpUtil.env.dev ? true : false,
isNotify: gulpUtil.env.notify ? true : false,
customs : [
{
src: 'bower_vendor/bootstrap-datepicker/css/datepicker.css',
dest: 'css',
minify: true
}
]
};
gulp.task('default', function () {
});
/******************************/
/******** Assets tasks ********/
/******************************/
gulp.task('assets', ['customs', 'css', 'sass', 'less', 'js', 'fonts', 'images']);
gulp.task('customs', function () {
assets.customs();
});
gulp.task('css', function () {
assets.css();
});
gulp.task('sass', function () {
assets.sass();
});
gulp.task('less', function () {
assets.less();
});
gulp.task('fonts', function () {
assets.fonts();
});
gulp.task('images', function () {
assets.images();
});
gulp.task('js', function () {
assets.js();
});
gulp.task('watch', function () {
assets.watch();
});
/*******************************/
/******* Assets business *******/
/*******************************/
var assets = {
customs: function () {
var
gulpUglify = require('gulp-uglify'),
gulpMinifyCss = require('gulp-minify-css'),
streams = [],
message = 'Customs complete';
_.forEach(options.customs, function(custom) {
streams.push(
gulp.src(custom.src)
.pipe(gulpIf(custom.rename !== undefined, gulpRename(custom.rename)))
.pipe(gulpIf(custom.uglify !== undefined && custom.uglify && !options.isDev, gulpUglify()))
.pipe(gulpIf(custom.minify !== undefined && custom.minify && !options.isDev, gulpMinifyCss({keepSpecialComments: 0})))
.pipe(gulp.dest(options.assetsBuild + '/' + (custom.dest ? custom.dest : '')))
);
});
return eventStream.readArray(streams)
.pipe(pipeNotify(message))
.on('end', function(){log(message);});
},
css: function (sources) {
var
gulpMinifyCss = require('gulp-minify-css'),
globs = this.getGlobs('css/**/*.css', sources),
message = 'CSS complete';
if (_.isEmpty(globs)) {
return false;
}
return gulp.src(globs)
.pipe(gulpIf(!options.isDev, gulpMinifyCss({
keepSpecialComments: 0
})))
.pipe(gulp.dest(options.assetsBuild + '/css'))
.pipe(pipeNotify(message))
.on('end', function(){log(message);});
},
sass: function (sources) {
var
gulpSass = options.isRubySass ? require('gulp-ruby-sass') : require('gulp-sass'),
globs = this.getGlobs('sass/**/*.scss', sources),
message = 'SASS complete';
if (_.isEmpty(globs)) {
return false;
}
return gulp.src(globs)
.pipe(gulpSass({
style: options.isDev ? 'expanded' : 'compressed'
}))
.on('error', function () {
var message = 'Error sass compilation';
notify(message);
log(message);
})
.pipe(gulp.dest(options.assetsBuild + '/css'))
.pipe(pipeNotify(message))
.on('end', function(){log(message);});
},
less: function (sources) {
var
gulpMinifyCss = require('gulp-minify-css'),
gulpLess = require('gulp-less'),
globs = this.getGlobs('less/*.less', sources),
message = 'LESS complete';
if (_.isEmpty(globs)) {
return false;
}
return gulp.src(globs)
.pipe(gulpLess())
.on('error', function () {
var message = 'Error less compilation';
notify(message);
log(message);
})
.pipe(gulpIf(!options.isDev, gulpMinifyCss({
keepSpecialComments: 0
})))
.pipe(gulp.dest(options.assetsBuild + '/css'))
.pipe(pipeNotify(message))
.on('end', function(){log(message);});
},
fonts: function (sources) {
var
globs = this.getGlobs('fonts/**/*', sources),
message = 'FONTS complete';
if (_.isEmpty(globs)) {
return false;
}
return gulp.src(globs)
.pipe(gulpChanged(options.assetsBuild + '/fonts'))
.pipe(gulp.dest(options.assetsBuild + '/fonts'))
.pipe(pipeNotify(message))
.on('end', function(){log(message);});
},
images: function(sources) {
var
gulpImagemin = require('gulp-imagemin'),
globs = this.getGlobs('images/**/*', sources),
message = 'Images ' + (sources ? '(' + sources + ') ' : '') + 'complete';
if (_.isEmpty(globs)) {
return false;
}
return gulp.src(globs)
.pipe(gulpChanged(options.assetsBuild + '/images'))
.pipe(gulpIf(!options.isDev, gulpImagemin({
optimizationLevel: 7,
progressive: true
})))
.pipe(gulp.dest(options.assetsBuild + '/images'))
.pipe(pipeNotify(message))
.on('end', function(){log(message);});
},
js: function (sources) {
var
requirejs = require('requirejs'),
globs = this.getGlobs('/js/**/build.js', sources),
builds = [],
message = "Requirejs build complete";
for (i in globs) {
builds = builds.concat(glob.sync(globs[i]));
}
for (i in builds) {
var build = builds[i];
if (fs.existsSync(build)) {
var config = require('./' + build);
var defaults = {
dir : options.assetsBuild + '/js',
optimize : options.isDev ? 'none' : 'uglify2',
preserveLicenseComments : options.isDev,
removeCombined : true,
keepBuildDir : true,
findNestedDependencies : true
};
config = _.merge(defaults, config);
config.baseUrl = path.dirname(build);
requirejs.optimize(
config,
function(buildResponse) {
fs.unlinkSync(options.assetsBuild + '/js/build.js');
fs.unlinkSync(options.assetsBuild + '/js/build.txt');
log(message);
notify(message)
},
function(error) {
message = 'Error Requirejs';
log(message, error);
notify(message);
}
);
}
}
},
watch: function () {
var
sources = this.getSources(),
glob;
for(i in sources) {
glob = sources[i]+'/css/**/*.css';
gulp.watch(glob, function (source) {
this.css(source);
}.bind(this, sources[i]));
glob = sources[i]+'/sass/**/*.scss';
gulp.watch(glob, function (source) {
this.sass(source);
}.bind(this, sources[i]));
glob = sources[i]+'/less/**/*.less';
gulp.watch(glob, function (source) {
this.less(source);
}.bind(this, sources[i]));
glob = sources[i]+'/fonts/**/*';
gulp.watch(glob, function (source) {
this.fonts(source);
}.bind(this, sources[i]));
glob = sources[i]+'/images/**/*';
gulp.watch(glob, function (source) {
this.images(source);
}.bind(this, sources[i]));
glob = sources[i]+'/js/**/*.js';
gulp.watch(glob, function (source) {
this.js(source);
}.bind(this, sources[i]));
}
},
getSources: function () {
paths = [];
// App assets
if (fs.existsSync('app/Resources/assets')) {
paths['app'] = 'app/Resources/assets';
}
// Src bundle assets
sources = glob.sync('src/**/*Bundle/Resources/assets');
for (i in sources) {
sourcePath = sources[i];
source = sourcePath
.replace('src/', '')
.replace('/Resources/assets', '')
.replace('/Bundle/', '')
.replace(/\//g, '');
paths[source] = sourcePath;
}
// Log
for (source in paths) {
log('Found', "'" + colors.cyan(source) + "'", 'assets source in', colors.magenta(paths[source]));
}
return paths;
},
getGlobs: function (glob, sources) {
if (sources && !_.isArray(sources)) {
sources = [sources];
}
var sources = sources || this.getSources();
var globs = [];
for (i in sources) {
globs.push(sources[i] + '/' + glob);
}
return globs;
}
};
/*******************************/
/************ Utils ************/
/*******************************/
function notify(message, settings, callback) {
var defaults = {
title: message,
message: options.projectName + ' gulp'
};
var settings = _.merge(defaults, settings);
if (options.isNotify) {
notifier.notify(settings, callback);
}
};
function pipeNotify(message, settings) {
var defaults = {
title: message,
message: options.projectName + ' gulp',
onLast: true
};
var settings = _.merge(defaults, settings);
return gulpIf(
options.isNotify,
gulpNotify(settings)
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment