Skip to content

Instantly share code, notes, and snippets.

@inoperable
Created October 20, 2015 04:33
Show Gist options
  • Save inoperable/1346d0e80dbc932391d8 to your computer and use it in GitHub Desktop.
Save inoperable/1346d0e80dbc932391d8 to your computer and use it in GitHub Desktop.
gulp tasks
var fs = require('fs'),
path = require('path'),
gulp = require('gulp'),
gutil = require('gulp-util'),
es = require('event-stream'),
concat = require('gulp-concat'),
runSequence = require('gulp-run-sequence'),
templateCache = require('gulp-angular-templatecache'),
minifyHtml = require('gulp-minify-html'),
ngmin = require('gulp-ngmin'),
less = require('gulp-less'),
rimraf = require('gulp-rimraf'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
uglify = require('gulp-uglify'),
minifyCss = require('gulp-minify-css'),
gulpif = require('gulp-if'),
nodemon = require('gulp-nodemon'),
_ = require('lodash'),
moment = require('moment'),
shell = require('gulp-shell'),
minimist = require('minimist'),
rename = require('gulp-rename');
var BASE = 'src/client/app',
ASSETS_BASE = 'src/client/assets'
var appFiles = applyPrefix(BASE, ['/entry/app.js', '/**/*.js']),
templateFiles = applyPrefix(BASE, ['/**/*.html']),
lessFiles = applyPrefix(BASE, ['/entry/less/main.less', '/**/*.less']),
adminLess = applyPrefix(BASE, ['/entry/less/admin.less']),
libFiles = applyPrefix(ASSETS_BASE, ['/js/lib/jquery.min.js', '/js/lib/lodash.min.js', '/js/lib/angular.min.js', '/js/lib/**/*.js', '/js/components/**/*.js']),
assetFiles = applyPrefix(ASSETS_BASE, ['/fonts/**/*', '/img/**/*', '/translations/**/*', '/favicon.ico']),
viewsFiles = applyPrefix(BASE, ['/entry/views/*.html']);
var compile = gutil.env.compile;
gulp.task('default', function(cb) {
runSequence(
'clean',
['scripts', 'assets', 'less', 'adminLess', 'views'],
'watch',
cb
);
});
gulp.task('clean', function() {
return gulp.src('src/server/public', { read: false })
.pipe(rimraf());
});
gulp.task('scripts', function() {
gulp.src(['src/server/public/js/*main.js', 'src/server/public/js/*lib.js', 'src/server/public/js/*templates.js'], { read: false })
.pipe(rimraf());
var buildTimestamp = moment().format("MM-D-YYYY-HH:mm:ss");
fs.writeFile('src/server/js-build-timestamp.json', JSON.stringify({ buildTimestamp: buildTimestamp }), function(err) {
if (err) return console.log("Could not create js-build-timestamp file");
console.log("Created js-build-timestamp file: " + buildTimestamp);
});
// App files
gulp.src(appFiles)
.pipe(jshint({
strict: false,
laxbreak: true,
debug: true,
globals: {
angular: true,
$: true,
_: true
}
}))
.pipe(jshint.reporter(stylish))
.pipe(concat(buildTimestamp + '-main.js'))
.pipe(gulpif(compile, ngmin()))
.pipe(gulpif(compile, uglify()))
.pipe(gulp.dest('src/server/public/js'));
// Template files
gulp.src(templateFiles)
.pipe(minifyHtml({
empty: true,
spare: true,
quotes: true
}))
.pipe(templateCache({
module: 'xxxProjName',
root: '',
base: function(file) {
return 'templates/' + path.basename(file.relative);
}
}))
.pipe(concat(buildTimestamp + '-templates.js'))
.pipe(gulpif(compile, ngmin()))
.pipe(gulpif(compile, uglify()))
.pipe(gulp.dest('src/server/public/js'));
// Library files
gulp.src(libFiles)
.pipe(concat(buildTimestamp + '-lib.js'))
.pipe(gulpif(compile, uglify({
preserveComments: 'some'
})))
.pipe(gulp.dest('src/server/public/js'));
});
gulp.task('less', function() {
gulp.src(['src/server/public/css/*.css'], { read: false })
.pipe(rimraf());
var buildTimestamp = moment().format("MM-D-YYYY-HH:mm:ss");
fs.writeFile('src/server/css-build-timestamp.json', JSON.stringify({ buildTimestamp: buildTimestamp }), function(err) {
if (err) return console.log("Could not create css-build-timestamp file");
console.log("Created css-build-timestamp file: " + buildTimestamp);
});
gulp.src(lessFiles[0])
.pipe(less())
.pipe(rename(buildTimestamp + '-main.css'))
.pipe(gulpif(compile, minifyCss()))
.pipe(gulp.dest('src/server/public/css'));
});
gulp.task('adminLess', function() {
gulp.src(adminLess)
.pipe(less())
.pipe(gulpif(compile, minifyCss()))
.pipe(gulp.dest('src/server/public/css'));
});
gulp.task('assets', function() {
gulp.src(assetFiles, { base: ASSETS_BASE})
.pipe(gulp.dest('src/server/public/'));
});
gulp.task('views', function() {
gulp.src(viewsFiles)
.pipe(gulp.dest('src/server/views/'));
});
gulp.task('watch', function() {
if (compile) return;
gulp.watch([appFiles, templateFiles, libFiles], ['scripts']);
gulp.watch(['client/index.html', assetFiles], ['assets']);
gulp.watch([lessFiles], ['less', 'adminLess']);
gulp.watch([viewsFiles], ['views']);
nodemon({
script: 'src/server/server.js',
watch: 'src/server',
ext: 'json js',
ignore: ['client/*, src/server/public/*']
});
});
function applyPrefix(prefix, fileNames) {
return _.map(fileNames, function(fileName) {
return prefix + fileName;
});
}
gulp.task('bleh', function() {
gulp.src('')
.pipe(shell([
'touch test'
]))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment