Last active
August 29, 2015 14:07
-
-
Save josephfinlayson/846a45b14513be02fafc to your computer and use it in GitHub Desktop.
Sample gulpfile
This file contains hidden or 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
'use strict'; | |
//gulp plugins | |
var gulp = require('gulp'), | |
inject = require('gulp-inject'), | |
sourcemaps = require('gulp-sourcemaps'), | |
coffee = require('gulp-coffee'), | |
sass = require('gulp-sass'), | |
prefix = require('gulp-autoprefixer'), | |
wiredep = require('wiredep').stream, | |
argv = require('yargs').argv, | |
angularFilesort = require('gulp-angular-filesort'), | |
livereload = require('gulp-livereload'), | |
ngAnnotate = require('gulp-ng-annotate'), | |
concat = require('gulp-concat'), | |
uglify = require('gulp-uglify'), | |
gulpRev = require('gulp-rev'), | |
flatten = require('gulp-flatten'), | |
del = require('del'), | |
sequence = require('run-sequence'), | |
gulpFilter = require('gulp-filter'), | |
watch = require('gulp-watch'), | |
nodemon = require('gulp-nodemon'), | |
plumber = require('gulp-plumber'), | |
spec = require('tap-spec'), | |
spawn = require('child_process').spawn, | |
tapeRunner = require.resolve('tape/bin/tape'), | |
googlecdn = require('gulp-google-cdn'); | |
// stripDebug = require('gulp-strip-debug'); | |
//initial config | |
var serverApp = 'api/v2/app.js', | |
buildPath = 'angular', // this will be changed to 'dist' in the case of a deployment build | |
build = false; | |
// get the gulp command, set env vars | |
if (argv._[0] === 'build') { | |
buildPath = 'dist', // this will be changed to 'dist' in the case of a deployment build | |
build = true; | |
} | |
var debug = require('gulp-debug'); | |
//don't watch temporary files | |
var tmpFiles = '!angular/tmp/**'; | |
//vars for various folders for watching | |
var paths = { | |
scripts: ['angular/**/*.js', tmpFiles], | |
coffeescripts: ['angular/**/*.coffee', tmpFiles], | |
css: ['angular/**/*.css', tmpFiles], | |
sass: ['angular/**/*.scss', tmpFiles], | |
tmp: 'angular/tmp/**', | |
php: ['./inc/built_js.php', './inc/built_css.php', './inc/footer.php', './inc/header.php'], | |
html: ['angular/**/*.html'], | |
server: ['api/v2/**/.js'], | |
serverTests: ['api/v2/test/**/*.js'] | |
}; | |
//pre rqeuisites are NPM | |
// This block sets up your local machine (or the remote server for angular/node development) | |
gulp.task('bowerInstallGlobal', function(done) { | |
require('child_process').spawn('npm', ['install', 'bower', '-g'], { | |
stdio: 'inherit' | |
}) | |
.on('close', done); | |
}); | |
gulp.task('knexInstallGlobal', function(done) { | |
require('child_process').spawn('npm', ['install', 'knex', '-g'], { | |
stdio: 'inherit' | |
}) | |
.on('close', done); | |
}); | |
gulp.task('npmInstall', function(done) { | |
require('child_process').spawn('npm', ['install'], { | |
stdio: 'inherit' | |
}) | |
.on('close', done); | |
}); | |
gulp.task('bowerInstall', ['bowerInstallGlobal'], function(done) { | |
require('child_process').spawn('bower', ['install'], { | |
stdio: 'inherit' | |
}) | |
.on('close', done); | |
}); | |
gulp.task('knexMigrate', ['npmInstall', 'knexInstallGlobal'], function(done) { | |
require('child_process').spawn('knex', ['--knexfile', 'migrations/knexfile.js', 'migrate:latest'], { | |
stdio: 'inherit' | |
}) | |
.on('close', done); | |
}); | |
//this is the main task, that combines other subtasks | |
gulp.task('installDeps', ['npmInstall', 'bowerInstall', 'knexMigrate']); | |
// end block | |
/* | |
* Getting all JS into the right place, and compiling coffee | |
* | |
*/ | |
//concat and minify js | |
gulp.task('concatAndMinifyJs', ['cleanDist', 'compileCoffee'], | |
function() { | |
return gulp.src(['angular/**/*.js', '!angular/bower_components/**', '!angular/**/*.spec*']) | |
.pipe(angularFilesort()) | |
.pipe(ngAnnotate()) | |
// .pipe(debug()) | |
.pipe(concat('app.js')) | |
.pipe(uglify({ | |
mangle: false | |
})) | |
.pipe(gulpRev()) | |
.pipe(gulp.dest(buildPath + '/public')); | |
} | |
); | |
gulp.task('concatAndMinifyCss', ['cleanDist', 'compileAndMinifySass'], | |
function() { | |
gulp.src(['angular/**/*.css', '!angular/bower_components/**']) | |
// .pipe(debug()) | |
.pipe(concat('app.css')) | |
.pipe(gulpRev()) | |
.pipe(gulp.dest(buildPath + '/public')); | |
} | |
); | |
gulp.task('compileCoffee', ['cleanDist'], | |
function() { | |
return gulp.src(['angular/**/*.coffee', '!angular/bower_components/**', '!angular/tmp/**']) | |
.pipe(plumber()) | |
.pipe(sourcemaps.init()) | |
.pipe(coffee()) | |
.pipe(sourcemaps.write()) | |
.pipe(gulp.dest('angular/tmp')) | |
} | |
); | |
// gulp.task('scripts', ['moveJs', 'compileCoffee']); | |
/* | |
* Getting all CSS into the right place, and compiling Sass | |
* | |
*/ | |
gulp.task('compileAndMinifySass', ['cleanDist'], | |
function() { | |
return gulp.src( | |
[ | |
'angular' + '/**/*.scss', | |
'!' + 'angular' + '/bower_components/**/*' | |
] | |
) | |
// .pipe(debug()) | |
.pipe(sass({ | |
errLogToConsole: true, | |
sourceComments: 'map', | |
// sourceMap: 'sass', | |
":cache_path": 'angular/tmp', | |
"cache_path": 'angular/tmp', | |
"cache_location": 'angular/tmp', | |
includePaths: ['angular/bower_components/'] | |
})) | |
.pipe(prefix()) | |
.pipe(gulp.dest('angular/tmp')); | |
} | |
); | |
gulp.task('moveBower', ['cleanDist'], | |
function() { | |
var distFonts = '/public/angular/bower_components'; | |
var watchFonts = '/tmp/app/angular/bower_components'; | |
return gulp.src(['angular/bower_components/**/*.{ttf,woff,svg,eot}']) | |
.pipe(gulp.dest(buildPath + (build ? distFonts : watchFonts))); | |
}); | |
/* | |
* | |
* Inject files into the right places | |
* | |
**/ | |
// injects bower dependencies | |
gulp.task('injectBower', ['cleanDist'], function() { | |
gulp.src(['angular/bower_components/**/*.map']) | |
.pipe(flatten()) | |
.pipe(gulp.dest(buildPath + '/bower_components')) | |
return gulp.src(paths.php) | |
.pipe(wiredep({ | |
ignorePath: '..' | |
})) | |
.pipe(gulp.dest('./inc')); | |
}); | |
//this places script and css tags in the php | |
// shouldn't need two tasks for this. The problem isn't due to the variable paths, | |
// but rather because of the different dependencies involved | |
gulp.task('injectStaticFiles', function(cb) { | |
var sources; | |
var targets = gulp.src(paths.php); | |
if (build) { | |
sources = gulp.src( | |
[ | |
buildPath + '/**/*vendor*.js', | |
buildPath + '/**/*app*.js', | |
buildPath + '/**/*.js', | |
buildPath + '/**/*.css', | |
'!' + buildPath + '/bower_components/**/*', | |
'!' + buildPath + '/**/*spec*', //exclude tests | |
]) | |
} else { | |
sources = gulp.src( | |
[ | |
buildPath + '/**/*.css', | |
buildPath + '/**/*.js', | |
'!' + buildPath + '/bower_components/**/*', //exclude bower components | |
'!' + buildPath + '/bower_components', //exclude bower components | |
'!' + buildPath + '/**/*spec*', //exclude tests | |
]); | |
} | |
var jsFilter = gulpFilter('**/*.js') | |
//pass JS only through the angular filesort | |
sources = sources.pipe(jsFilter) | |
.pipe(plumber()) | |
.pipe(angularFilesort()) | |
.pipe(jsFilter.restore()); | |
return targets | |
.pipe(inject(sources, { | |
// relative: true | |
})) | |
.pipe(gulp.dest('./inc')); | |
}); | |
gulp.task('injectStaticFilesDev', ['compileCoffee', 'compileAndMinifySass'], function(cb) { | |
sequence('injectStaticFiles', cb) | |
}) | |
gulp.task('injectStaticFilesDist', ['concatAndMinifyCss', 'concatAndMinifyJs', 'injectBower', 'compileCoffee', 'compileAndMinifySass'], function(cb) { | |
sequence('injectStaticFiles', cb) | |
}) | |
/* | |
* Clean files | |
* | |
*/ | |
gulp.task('cleanTmp', function(cb) { | |
del(['angular/tmp', 'angular/app/angular'], cb); | |
}); | |
gulp.task('cleanDist', function(cb) { | |
del('dist', cb); | |
}); | |
/****** | |
* tasks actually exposed | |
******/ | |
gulp.task('build', ['buildStart']); | |
gulp.task('buildStart', function(cb) { | |
sequence([ | |
'cleanDist', | |
'cleanTmp' | |
], ['injectBower', | |
'compileCoffee', | |
'concatAndMinifyJs', | |
'concatAndMinifyCss', | |
'startNode', | |
'injectStaticFilesDist', | |
'moveBower', | |
'cdnify' | |
], cb); | |
}); | |
gulp.task('cdnify', ['injectBower'], function() { | |
return gulp.src(paths.php) | |
.pipe(googlecdn(require('./bower.json'), { | |
componentsPath: './angular/bower_components' | |
})) | |
.pipe(gulp.dest('inc')); | |
}); | |
gulp.task('clean', ['cleanTmp', 'cleanDist']); | |
gulp.task('watch', ['watchstart'], function() { | |
livereload.listen(); | |
// watch(paths.coffeescripts, { | |
// interval: 500 | |
// }, function(files, cb) { | |
// gulp.start(['injectStaticFilesDev'], cb); | |
// }) | |
watch(paths.scripts, { | |
interval: 500 | |
}, function(files, cb) { | |
gulp.start('injectStaticFilesDev', cb) | |
}).pipe(livereload()) | |
watch(paths.sass, { | |
interval: 500 | |
}, function(files, cb) { | |
gulp.start(['compileAndMinifySass', 'injectStaticFilesDev'], cb) | |
}) | |
watch(paths.css, { | |
interval: 500 | |
}, function(files, cb) { | |
gulp.start('injectStaticFilesDev', cb); | |
}) | |
// .pipe(livereload()); | |
watch(paths.tmp, { | |
interval: 500 | |
}).pipe(livereload()) | |
watch(paths.html, { | |
interval: 500 | |
}).pipe(livereload()) | |
watch(paths.server, { | |
interval: 200 | |
}).pipe(livereload()) | |
}) | |
gulp.task('watchstart', function(cb) { | |
sequence('cleanTmp', ['compileCoffee', 'injectBower', 'injectStaticFilesDev', 'startNode', 'moveBower', 'compileAndMinifySass'], cb) | |
}); | |
/***** | |
* Restarts node | |
*****/ | |
gulp.task('stopNode', [], function(done) { | |
// if | |
require('child_process').spawn('forever', ['stop', serverApp], { | |
stdio: 'inherit' | |
}) | |
.on('close', function() { | |
done(); | |
}); | |
}); | |
//starts it (restart) | |
gulp.task('startNode', ['stopNode'], function(done) { | |
var started = false; | |
var productionEnv = process.env; | |
if (build) { | |
productionEnv.NODE_ENV = 'production'; | |
require('child_process').spawn('forever', ['start', '-o', './api/v2/console.logs', serverApp], { | |
env: productionEnv, | |
stdio: "inherit" | |
}) | |
.on('close', done); | |
} else { | |
return nodemon({ | |
script: serverApp, | |
ext: 'html js coffee', | |
nodeArgs: ['--debug'], | |
watch: './api/v2/**', | |
ignore: '**/node_modules**', | |
}).on('start', function() { | |
if (started !== true) { | |
done() | |
} | |
started = true | |
}) | |
} | |
}); | |
gulp.task('test', function(cb) { | |
var runner = spawn(tapeRunner, paths.serverTests) | |
.on('close', function errorHandler(error) { | |
console.log(error.toString()); | |
cb() | |
this.emit('end'); | |
}) | |
return runner.stdout | |
.pipe(plumber()) | |
.pipe(spec()) | |
.pipe(process.stdout) | |
}); | |
// gulp.task('removeLogs', function() { | |
// return gulp.src([paths.coffeescripts, paths.server]) | |
// .pipe(stripDebug()) | |
// //.pipe(gulp.dest('dist')); | |
// }); | |
// gulp.task('convertToCoffee', function() { | |
// return gulp.src(paths.coffeescripts) | |
// .pipe(coffee({ | |
// bare: true | |
// })) | |
// .pipe(gulp.dest('angular')) | |
// }); | |
// gulp.task('deleteCoffee', function(cb) { | |
// del(paths.coffeescripts, cb) | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment