|
| 1 | +// Generated on <%= (new Date).toISOString().split('T')[0] %> using <%= pkg.name %> <%= pkg.version %> |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +var gulp = require('gulp'); |
| 5 | +var $ = require('gulp-load-plugins')(); |
| 6 | +var http = require('http'); |
| 7 | +var openURL = require('open'); |
| 8 | +var lazypipe = require('lazypipe'); |
| 9 | +var wiredep = require('wiredep').stream; |
| 10 | +var nodemon = require('nodemon'); |
| 11 | +var runSequence = require('run-sequence'); |
| 12 | +var path = require('path');<% if (stylus) { %> |
| 13 | +var nib = require('nib');<% } %> |
| 14 | +var config; |
| 15 | + |
| 16 | +var yeoman = { |
| 17 | + app: require('./bower.json').appPath || 'app', |
| 18 | + dist: 'dist' |
| 19 | +}; |
| 20 | + |
| 21 | +var paths = { |
| 22 | + client: { |
| 23 | + scripts: [yeoman.app + '/scripts/**/*.<% if (coffee) { %>coffee<% } else { %>js<% } %>'], |
| 24 | + styles: [yeoman.app + '/styles/**/*.<% if (stylus) { %>styl<% } else if (sass) { %>scss<% } else { %>css<% } %>'], |
| 25 | + test: ['test/client/**/*.<% if (coffee) { %>coffee<% } else { %>js<% } %>'], |
| 26 | + testRequire: [ |
| 27 | + yeoman.app + '/bower_components/angular/angular.js', |
| 28 | + yeoman.app + '/bower_components/angular-mocks/angular-mocks.js', |
| 29 | + yeoman.app + '/bower_components/angular-resource/angular-resource.js', |
| 30 | + yeoman.app + '/bower_components/angular-cookies/angular-cookies.js', |
| 31 | + yeoman.app + '/bower_components/angular-sanitize/angular-sanitize.js', |
| 32 | + yeoman.app + '/bower_components/angular-route/angular-route.js',<% if (coffee) { %> |
| 33 | + 'test/mock/**/*.coffee', |
| 34 | + 'test/spec/**/*.coffee'<% } else { %> |
| 35 | + 'test/mock/**/*.js', |
| 36 | + 'test/spec/**/*.js'<% } %> |
| 37 | + ] |
| 38 | + }, |
| 39 | + server: {<% if (coffee) { %> |
| 40 | + scripts: ['lib/**/*.coffee'], |
| 41 | + test: ['test/server/**/*.coffee'],<% } else { %> |
| 42 | + scripts: ['lib/**/*.js'], |
| 43 | + test: ['test/server/**/*.js'],<% } %> |
| 44 | + |
| 45 | + }, |
| 46 | + views: {<% if (jade) { %> |
| 47 | + main: yeoman.app + '/views/index.jade', |
| 48 | + files: [yeoman.app + '/views/**/*.jade']<% } else {%> |
| 49 | + main: yeoman.app + '/views/index.html', |
| 50 | + files: [yeoman.app + '/views/**/*.html']<% } %> |
| 51 | + }, |
| 52 | + karma: 'karma.conf.js' |
| 53 | +}; |
| 54 | + |
| 55 | +////////////////////// |
| 56 | +// Helper functions // |
| 57 | +////////////////////// |
| 58 | + |
| 59 | +function onServerLog(log) { |
| 60 | + console.log($.util.colors.white('[') + $.util.colors.yellow('nodemon') + $.util.colors.white('] ') + log.message); |
| 61 | +} |
| 62 | + |
| 63 | +function checkAppReady(cb) { |
| 64 | + var options = { |
| 65 | + host: 'localhost', |
| 66 | + port: config.port, |
| 67 | + }; |
| 68 | + http.get(options, function() { |
| 69 | + cb(true); |
| 70 | + }).on('error', function() { |
| 71 | + cb(false); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +// Call page until first success |
| 76 | +function whenServerReady (cb) { |
| 77 | + var serverReady = false; |
| 78 | + var appReadyInterval = setInterval(function () { |
| 79 | + checkAppReady(function(ready){ |
| 80 | + if (!ready || serverReady) { return; } |
| 81 | + clearInterval(appReadyInterval); |
| 82 | + serverReady = true; |
| 83 | + cb(); |
| 84 | + }); |
| 85 | + }, 100); |
| 86 | +} |
| 87 | + |
| 88 | +//////////////////////// |
| 89 | +// Reusable pipelines // |
| 90 | +//////////////////////// |
| 91 | + |
| 92 | +var lintScripts = lazypipe()<% if (coffee) { %> |
| 93 | + .pipe($.coffeelint) |
| 94 | + .pipe($.coffeelint.reporter);<% } else { %> |
| 95 | + .pipe($.jshint, '.jshintrc') |
| 96 | + .pipe($.jshint.reporter, 'jshint-stylish');<% } %> |
| 97 | + |
| 98 | +var styles = lazypipe()<% if (stylus) { %> |
| 99 | + .pipe($.stylus, { |
| 100 | + use: [nib()], |
| 101 | + errors: true |
| 102 | + })<% } %><% if (sass) { %> |
| 103 | + .pipe($.rubySass, { |
| 104 | + style: 'expanded', |
| 105 | + precision: 10 |
| 106 | + })<% } %> |
| 107 | + .pipe($.autoprefixer, 'last 1 version') |
| 108 | + .pipe(gulp.dest, '.tmp/styles'); |
| 109 | + |
| 110 | +/////////// |
| 111 | +// Tasks // |
| 112 | +/////////// |
| 113 | + |
| 114 | +gulp.task('styles', function () { |
| 115 | + return gulp.src(paths.client.styles) |
| 116 | + .pipe(styles()); |
| 117 | +});<% if (coffee) { %> |
| 118 | + |
| 119 | +gulp.task('coffee', function() { |
| 120 | + return gulp.src(paths.client.scripts) |
| 121 | + .pipe(lintScripts()) |
| 122 | + .pipe($.coffee({bare: true}).on('error', $.util.log)) |
| 123 | + .pipe(gulp.dest('.tmp/scripts')); |
| 124 | +});<% } %> |
| 125 | + |
| 126 | +gulp.task('lint:scripts', function () { |
| 127 | + var scripts = paths.client.scripts.concat(paths.server.scripts); |
| 128 | + return gulp.src(scripts).pipe(lintScripts()); |
| 129 | +}); |
| 130 | + |
| 131 | +gulp.task('clean:tmp', function () { |
| 132 | + return gulp.src('.tmp', {read: false}).pipe($.clean()); |
| 133 | +}); |
| 134 | + |
| 135 | +gulp.task('start:client', [<% if (coffee) { %>'coffee', <% } %>'styles'], function (callback) { |
| 136 | + whenServerReady(function () { |
| 137 | + openURL('http://localhost:' + config.port); |
| 138 | + callback(); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +gulp.task('start:server', function () { |
| 143 | + process.env.NODE_ENV = process.env.NODE_ENV || 'development'; |
| 144 | + config = require('./lib/config/config'); |
| 145 | + nodemon('-w lib server.js') |
| 146 | + .on('log', onServerLog); |
| 147 | +}); |
| 148 | + |
| 149 | +gulp.task('watch', function () { |
| 150 | + var testFiles = paths.client.test.concat(paths.server.test); |
| 151 | + |
| 152 | + $.watch({glob: paths.client.styles}) |
| 153 | + .pipe($.plumber()) |
| 154 | + .pipe(styles()) |
| 155 | + .pipe($.livereload()); |
| 156 | + |
| 157 | + $.watch({glob: paths.views.files}) |
| 158 | + .pipe($.plumber()) |
| 159 | + .pipe($.livereload()); |
| 160 | + |
| 161 | + $.watch({glob: paths.client.scripts}) |
| 162 | + .pipe($.plumber()) |
| 163 | + .pipe(lintScripts())<% if (coffee) { %> |
| 164 | + .pipe($.coffee({bare: true}).on('error', $.util.log)) |
| 165 | + .pipe(gulp.dest('.tmp/scripts'))<% } %> |
| 166 | + .pipe($.livereload()); |
| 167 | + |
| 168 | + $.watch({glob: paths.server.scripts.concat(testFiles)}) |
| 169 | + .pipe($.plumber()) |
| 170 | + .pipe(lintScripts()); |
| 171 | + |
| 172 | + gulp.watch('bower.json', ['bower']); |
| 173 | +}); |
| 174 | + |
| 175 | +gulp.task('serve', function (callback) { |
| 176 | + runSequence('clean:tmp', |
| 177 | + ['lint:scripts'], |
| 178 | + ['start:server', 'start:client'], |
| 179 | + 'watch', callback); |
| 180 | +}); |
| 181 | + |
| 182 | +gulp.task('test:server', function () { |
| 183 | + process.env.NODE_ENV = 'test'; |
| 184 | + return gulp.src(paths.server.test) |
| 185 | + .pipe($.mocha({reporter: 'spec'})); |
| 186 | +}); |
| 187 | + |
| 188 | +gulp.task('test:client', function () { |
| 189 | + var testFiles = paths.client.testRequire.concat(paths.client.test) |
| 190 | + gulp.src(testFiles) |
| 191 | + .pipe($.karma({ |
| 192 | + configFile: paths.karma, |
| 193 | + action: 'watch' |
| 194 | + })); |
| 195 | +}); |
| 196 | + |
| 197 | +// inject bower components |
| 198 | +gulp.task('bower', function () { |
| 199 | + return gulp.src(paths.views.main) |
| 200 | + .pipe(wiredep({ |
| 201 | + directory: yeoman.app + '/bower_components', |
| 202 | + ignorePath: '..' |
| 203 | + })) |
| 204 | + .pipe(gulp.dest(yeoman.app + '/views/')); |
| 205 | +}); |
| 206 | + |
| 207 | +/////////// |
| 208 | +// Build // |
| 209 | +/////////// |
| 210 | + |
| 211 | +gulp.task('build', function (callback) { |
| 212 | + runSequence('clean:dist', |
| 213 | + ['images', 'copy:extras', 'copy:fonts', 'copy:server', 'client:build'], |
| 214 | + callback); |
| 215 | +}); |
| 216 | + |
| 217 | +gulp.task('clean:dist', function () { |
| 218 | + return gulp.src('dist', {read: false}).pipe($.clean()); |
| 219 | +}); |
| 220 | + |
| 221 | +gulp.task('client:build', ['html'], function () { |
| 222 | + var jsFilter = $.filter('**/*.js'); |
| 223 | + var cssFilter = $.filter('**/*.css');<% if (jade) { %> |
| 224 | + var assets = $.filter('**/*.{js,css}');<% } %> |
| 225 | + |
| 226 | + return gulp.src(paths.views.main)<% if (jade) { %> |
| 227 | + .pipe($.jade({pretty: true}))<% } %> |
| 228 | + .pipe($.useref.assets({searchPath: [yeoman.app, '.tmp']})) |
| 229 | + .pipe(jsFilter) |
| 230 | + .pipe($.ngmin()) |
| 231 | + .pipe($.uglify()) |
| 232 | + .pipe(jsFilter.restore()) |
| 233 | + .pipe(cssFilter) |
| 234 | + .pipe($.minifyCss({cache: true})) |
| 235 | + .pipe(cssFilter.restore()) |
| 236 | + .pipe($.rev()) |
| 237 | + .pipe($.useref.restore()) |
| 238 | + .pipe($.revReplace()) |
| 239 | + .pipe($.useref())<% if (jade) { %> |
| 240 | + .pipe(assets)<% } %> |
| 241 | + .pipe(gulp.dest(yeoman.dist + '/public')); |
| 242 | +}); |
| 243 | + |
| 244 | +gulp.task('html', function () { |
| 245 | + return gulp.src(yeoman.app + '/views/**/*') |
| 246 | + .pipe(gulp.dest(yeoman.dist + '/public/views')); |
| 247 | +}); |
| 248 | + |
| 249 | +gulp.task('images', function () { |
| 250 | + return gulp.src(yeoman.app + '/images/**/*') |
| 251 | + .pipe($.cache($.imagemin({ |
| 252 | + optimizationLevel: 5, |
| 253 | + progressive: true, |
| 254 | + interlaced: true |
| 255 | + }))) |
| 256 | + .pipe(gulp.dest(yeoman.dist + '/public/images')); |
| 257 | +}); |
| 258 | + |
| 259 | +gulp.task('copy:extras', function () { |
| 260 | + return gulp.src(yeoman.app + '/*.*', { dot: true }) |
| 261 | + .pipe(gulp.dest(yeoman.dist + '/public')); |
| 262 | +}); |
| 263 | + |
| 264 | +gulp.task('copy:fonts', function () { |
| 265 | + return gulp.src(yeoman.app + '/fonts/**/*') |
| 266 | + .pipe(gulp.dest(yeoman.dist + '/fonts')); |
| 267 | +}); |
| 268 | + |
| 269 | +gulp.task('copy:server', function(){ |
| 270 | + return gulp.src([ |
| 271 | + 'package.json', |
| 272 | + 'server.js', |
| 273 | + 'lib/**/*' |
| 274 | + ], {cwdbase: true}).pipe(gulp.dest(yeoman.dist)); |
| 275 | +}); |
0 commit comments