|
| 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 openURL = require('open'); |
| 7 | +var lazypipe = require('lazypipe'); |
| 8 | +var rimraf = require('rimraf'); |
| 9 | +var wiredep = require('wiredep').stream; |
| 10 | +var runSequence = require('run-sequence'); |
| 11 | + |
| 12 | +var yeoman = { |
| 13 | + app: require('./bower.json').appPath || 'app', |
| 14 | + dist: 'dist' |
| 15 | +}; |
| 16 | + |
| 17 | +var paths = { |
| 18 | + scripts: [yeoman.app + '/scripts/**/*.<% if (coffee) { %>coffee<% } else { %>js<% } %>'], |
| 19 | + styles: [yeoman.app + '/styles/**/*.<% if (sass) { %>scss<% } else { %>css<% } %>'], |
| 20 | + test: ['test/spec/**/*.<% if (coffee) { %>coffee<% } else { %>js<% } %>'], |
| 21 | + testRequire: [ |
| 22 | + yeoman.app + '/bower_components/angular/angular.js', |
| 23 | + yeoman.app + '/bower_components/angular-mocks/angular-mocks.js', |
| 24 | + yeoman.app + '/bower_components/angular-resource/angular-resource.js', |
| 25 | + yeoman.app + '/bower_components/angular-cookies/angular-cookies.js', |
| 26 | + yeoman.app + '/bower_components/angular-sanitize/angular-sanitize.js', |
| 27 | + yeoman.app + '/bower_components/angular-route/angular-route.js',<% if (coffee) { %> |
| 28 | + 'test/mock/**/*.coffee', |
| 29 | + 'test/spec/**/*.coffee'<% } else { %> |
| 30 | + 'test/mock/**/*.js', |
| 31 | + 'test/spec/**/*.js'<% } %> |
| 32 | + ], |
| 33 | + karma: 'karma.conf.js', |
| 34 | + views: { |
| 35 | + main: yeoman.app + '/index.html', |
| 36 | + files: [yeoman.app + '/views/**/*.html'] |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +//////////////////////// |
| 41 | +// Reusable pipelines // |
| 42 | +//////////////////////// |
| 43 | + |
| 44 | +var lintScripts = lazypipe()<% if (coffee) { %> |
| 45 | + .pipe($.coffeelint) |
| 46 | + .pipe($.coffeelint.reporter);<% } else { %> |
| 47 | + .pipe($.jshint, '.jshintrc') |
| 48 | + .pipe($.jshint.reporter, 'jshint-stylish');<% } %> |
| 49 | + |
| 50 | +var styles = lazypipe()<% if (sass) { %> |
| 51 | + .pipe($.rubySass, { |
| 52 | + style: 'expanded', |
| 53 | + precision: 10 |
| 54 | + })<% } %> |
| 55 | + .pipe($.autoprefixer, 'last 1 version') |
| 56 | + .pipe(gulp.dest, '.tmp/styles'); |
| 57 | + |
| 58 | +/////////// |
| 59 | +// Tasks // |
| 60 | +/////////// |
| 61 | + |
| 62 | +gulp.task('styles', function () { |
| 63 | + return gulp.src(paths.styles) |
| 64 | + .pipe(styles()); |
| 65 | +});<% if (coffee) { %> |
| 66 | + |
| 67 | +gulp.task('coffee', function() { |
| 68 | + return gulp.src(paths.scripts) |
| 69 | + .pipe(lintScripts()) |
| 70 | + .pipe($.coffee({bare: true}).on('error', $.util.log)) |
| 71 | + .pipe(gulp.dest('.tmp/scripts')); |
| 72 | +});<% } %> |
| 73 | + |
| 74 | +gulp.task('lint:scripts', function () { |
| 75 | + return gulp.src(paths.scripts) |
| 76 | + .pipe(lintScripts()); |
| 77 | +}); |
| 78 | + |
| 79 | +gulp.task('clean:tmp', function (cb) { |
| 80 | + rimraf('./.tmp', cb); |
| 81 | +}); |
| 82 | + |
| 83 | +gulp.task('start:client', ['start:server', <% if (coffee) { %>'coffee', <% } %>'styles'], function () { |
| 84 | + openURL('http://localhost:9000'); |
| 85 | +}); |
| 86 | + |
| 87 | +gulp.task('start:server', function() { |
| 88 | + $.connect.server({ |
| 89 | + root: [yeoman.app, '.tmp'], |
| 90 | + livereload: true, |
| 91 | + // Change this to '0.0.0.0' to access the server from outside. |
| 92 | + port: 9000 |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +gulp.task('start:server:test', function() { |
| 97 | + $.connect.server({ |
| 98 | + root: ['test', yeoman.app, '.tmp'], |
| 99 | + livereload: true, |
| 100 | + port: 9001 |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +gulp.task('watch', function () { |
| 105 | + |
| 106 | + $.watch({glob: paths.styles}) |
| 107 | + .pipe($.plumber()) |
| 108 | + .pipe(styles()) |
| 109 | + .pipe($.connect.reload()); |
| 110 | + |
| 111 | + $.watch({glob: paths.views.files}) |
| 112 | + .pipe($.plumber()) |
| 113 | + .pipe($.connect.reload()); |
| 114 | + |
| 115 | + $.watch({glob: paths.scripts}) |
| 116 | + .pipe($.plumber()) |
| 117 | + .pipe(lintScripts())<% if (coffee) { %> |
| 118 | + .pipe($.coffee({bare: true}).on('error', $.util.log)) |
| 119 | + .pipe(gulp.dest('.tmp/scripts'))<% } %> |
| 120 | + .pipe($.connect.reload()); |
| 121 | + |
| 122 | + $.watch({glob: paths.test}) |
| 123 | + .pipe($.plumber()) |
| 124 | + .pipe(lintScripts()); |
| 125 | + |
| 126 | + gulp.watch('bower.json', ['bower']); |
| 127 | +}); |
| 128 | + |
| 129 | +gulp.task('serve', function (callback) { |
| 130 | + runSequence('clean:tmp', |
| 131 | + ['lint:scripts'], |
| 132 | + ['start:client'], |
| 133 | + 'watch', callback); |
| 134 | +}); |
| 135 | + |
| 136 | +gulp.task('serve:prod', function() { |
| 137 | + $.connect.server({ |
| 138 | + root: [yeoman.dist], |
| 139 | + livereload: true, |
| 140 | + port: 9000 |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +gulp.task('test', ['start:server:test'], function () { |
| 145 | + var testToFiles = paths.testRequire.concat(paths.scripts, paths.test); |
| 146 | + return gulp.src(testToFiles) |
| 147 | + .pipe($.karma({ |
| 148 | + configFile: paths.karma, |
| 149 | + action: 'watch' |
| 150 | + })); |
| 151 | +}); |
| 152 | + |
| 153 | +// inject bower components |
| 154 | +gulp.task('bower', function () { |
| 155 | + return gulp.src(paths.views.main) |
| 156 | + .pipe(wiredep({ |
| 157 | + directory: yeoman.app + '/bower_components', |
| 158 | + ignorePath: '..' |
| 159 | + })) |
| 160 | + .pipe(gulp.dest(yeoman.app + '/views')); |
| 161 | +}); |
| 162 | + |
| 163 | +/////////// |
| 164 | +// Build // |
| 165 | +/////////// |
| 166 | + |
| 167 | +gulp.task('build', function (callback) { |
| 168 | + runSequence('clean:dist', |
| 169 | + ['images', 'copy:extras', 'copy:fonts', 'client:build'], |
| 170 | + callback); |
| 171 | +}); |
| 172 | + |
| 173 | +gulp.task('clean:dist', function () { |
| 174 | + rimraf('./dist', cb); |
| 175 | +}); |
| 176 | + |
| 177 | +gulp.task('client:build', ['html', 'styles'], function () { |
| 178 | + var jsFilter = $.filter('**/*.js'); |
| 179 | + var cssFilter = $.filter('**/*.css'); |
| 180 | + |
| 181 | + return gulp.src(paths.views.main) |
| 182 | + .pipe($.useref.assets({searchPath: [yeoman.app, '.tmp']})) |
| 183 | + .pipe(jsFilter) |
| 184 | + .pipe($.ngAnnotate()) |
| 185 | + .pipe($.uglify()) |
| 186 | + .pipe(jsFilter.restore()) |
| 187 | + .pipe(cssFilter) |
| 188 | + .pipe($.minifyCss({cache: true})) |
| 189 | + .pipe(cssFilter.restore()) |
| 190 | + .pipe($.rev()) |
| 191 | + .pipe($.useref.restore()) |
| 192 | + .pipe($.revReplace()) |
| 193 | + .pipe($.useref()) |
| 194 | + .pipe(gulp.dest(yeoman.dist)); |
| 195 | +}); |
| 196 | + |
| 197 | +gulp.task('html', function () { |
| 198 | + return gulp.src(yeoman.app + '/views/**/*') |
| 199 | + .pipe(gulp.dest(yeoman.dist + '/views')); |
| 200 | +}); |
| 201 | + |
| 202 | +gulp.task('images', function () { |
| 203 | + return gulp.src(yeoman.app + '/images/**/*') |
| 204 | + .pipe($.cache($.imagemin({ |
| 205 | + optimizationLevel: 5, |
| 206 | + progressive: true, |
| 207 | + interlaced: true |
| 208 | + }))) |
| 209 | + .pipe(gulp.dest(yeoman.dist + '/images')); |
| 210 | +}); |
| 211 | + |
| 212 | +gulp.task('copy:extras', function () { |
| 213 | + return gulp.src(yeoman.app + '/*/.*', { dot: true }) |
| 214 | + .pipe(gulp.dest(yeoman.dist)); |
| 215 | +}); |
| 216 | + |
| 217 | +gulp.task('copy:fonts', function () { |
| 218 | + return gulp.src(yeoman.app + '/fonts/**/*') |
| 219 | + .pipe(gulp.dest(yeoman.dist + '/fonts')); |
| 220 | +}); |
0 commit comments