diff --git a/Gruntfile.js b/Gruntfile.js index 3d8ad1e..3c8ee10 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,166 +1,10 @@ 'use strict'; -var fs = require('fs'); -var _ = require('lodash'); -var spawn = require('child_process').spawn; - -function loadConfig(path) { - var config = {}; - fs.readdirSync(path).forEach(function(file) { - var taskName = file.replace(/\.js$/, ''); - config[taskName] = require(path + file); - }); - - return config; -} - module.exports = function(grunt) { - // Load grunt tasks automatically - require('jit-grunt')(grunt, { - express: 'grunt-express-server', - useminPrepare: 'grunt-usemin', - ngtemplates: 'grunt-angular-templates', - protractor: 'grunt-protractor-runner', - injector: 'grunt-injector', - sass: 'grunt-sass' - }); - - // Time how long tasks take. Can help when optimizing build times - require('time-grunt')(grunt); - - var config = { - // Common settings - pkg: grunt.file.readJSON('package.json'), - app: { - dirs: { - client: 'client', - dist: 'dist' - }, + require('load-grunt-config')(grunt, { + data: { port: process.env.PORT || 5000 } - }; - - // Load grunt-tasks - _.extend( - config, - loadConfig(__dirname + '/grunt-tasks/') - ); - - grunt.initConfig(config); - - // Used for delaying livereload until after server has restarted - grunt.registerTask('wait', function() { - grunt.log.ok('Waiting for server reload...'); - - var done = this.async(); - - setTimeout(function() { - grunt.log.writeln('Done waiting!'); - done(); - }, 1500); - }); - - grunt.registerTask('express-keepalive', 'Keep grunt running', function() { - this.async(); - }); - - grunt.registerTask('bunyan', function() { - var path = './node_modules/bunyan/bin/bunyan'; - if (!fs.existsSync(path)) { - throw new Error('bundle binary not found'); - } - - var child = spawn(path, ['-oshort'], { - stdio: ['pipe', process.stdout, process.stderr] - }); - - process.stdout.write = function() { - child.stdin.write.apply(child.stdin, arguments); - }; }); - - grunt.registerTask('serve', function(target) { - if (target === 'dist') { - return grunt.task.run(['build', 'env:all', 'env:prod', 'express:prod', 'wait', 'open', 'express-keepalive']); - } - - grunt.task.run([ - 'clean:server', - 'env:all', - 'injector:sass', - 'concurrent:server', - 'injector', - 'wiredep', - 'autoprefixer', - 'express:dev', - 'wait', - 'open', - 'watch' - ]); - }); - - grunt.registerTask('test', function(target) { - if (target === 'server') { - return grunt.task.run([ - 'env:all', - 'env:test', - 'mochaTest' - ]); - } else if (target === 'client') { - return grunt.task.run([ - 'clean:server', - 'env:all', - 'injector:sass', - 'concurrent:test', - 'injector', - 'autoprefixer', - 'karma' - ]); - } else if (target === 'e2e') { - return grunt.task.run([ - 'clean:server', - 'env:all', - 'env:test', - 'injector:sass', - 'concurrent:test', - 'injector', - 'wiredep', - 'autoprefixer', - 'express:dev', - 'protractor' - ]); - } else { - grunt.task.run([ - 'test:server', - 'test:client' - ]); - } - }); - - grunt.registerTask('build', [ - 'jshint', - 'jscs', - 'clean:dist', - 'injector:sass', - 'concurrent:dist', - 'injector', - 'wiredep', - 'useminPrepare', - 'autoprefixer', - 'ngtemplates', - 'concat', - 'ngAnnotate', - 'copy:dist', - 'cssmin', - 'uglify', - 'rev', - 'usemin' - ]); - - grunt.registerTask('default', [ - 'newer:jshint', - 'jscs', - 'test', - 'build' - ]); + require('time-grunt')(grunt); }; diff --git a/grunt-tasks/jscs.js b/grunt-tasks/jscs.js deleted file mode 100644 index 02eef7e..0000000 --- a/grunt-tasks/jscs.js +++ /dev/null @@ -1,25 +0,0 @@ -module.exports = { - options: { - config: '.jscsrc' - }, - server: { - src: [ - 'server/**/*.js', - '!server/**/*.spec.js' - ] - }, - serverTest: { - src: ['server/**/*.spec.js'] - }, - all: [ - '<%= app.dirs.client %>/{app,components}/**/*.js', - '!<%= app.dirs.client %>/{app,components}/**/*.spec.js', - '!<%= app.dirs.client %>/{app,components}/**/*.mock.js' - ], - test: { - src: [ - '<%= app.dirs.client %>/{app,components}/**/*.spec.js', - '<%= app.dirs.client %>/{app,components}/**/*.mock.js' - ] - } -}; diff --git a/grunt-tasks/jshint.js b/grunt-tasks/jshint.js deleted file mode 100644 index 73aca2e..0000000 --- a/grunt-tasks/jshint.js +++ /dev/null @@ -1,31 +0,0 @@ -module.exports = { - options: { - jshintrc: '<%= app.dirs.client %>/.jshintrc' - }, - server: { - options: { - jshintrc: 'server/.jshintrc' - }, - src: [ - 'server/**/*.js', - '!server/**/*.spec.js' - ] - }, - serverTest: { - options: { - jshintrc: 'server/.jshintrc-spec' - }, - src: ['server/**/*.spec.js'] - }, - all: [ - '<%= app.dirs.client %>/{app,components}/**/*.js', - '!<%= app.dirs.client %>/{app,components}/**/*.spec.js', - '!<%= app.dirs.client %>/{app,components}/**/*.mock.js' - ], - test: { - src: [ - '<%= app.dirs.client %>/{app,components}/**/*.spec.js', - '<%= app.dirs.client %>/{app,components}/**/*.mock.js' - ] - } -}; diff --git a/grunt-tasks/open.js b/grunt-tasks/open.js deleted file mode 100644 index 92029f5..0000000 --- a/grunt-tasks/open.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = { - server: { - url: 'http://localhost:<%= app.port %>' - } -}; diff --git a/grunt-tasks/rev.js b/grunt-tasks/rev.js deleted file mode 100644 index 331b44c..0000000 --- a/grunt-tasks/rev.js +++ /dev/null @@ -1,13 +0,0 @@ -module.exports = { - dist: { - files: { - src: [ - '<%= app.dirs.dist %>/{,*/}*.js', - '<%= app.dirs.dist %>/{,*/}*.css', - '<%= app.dirs.dist %>/assets/fonts/*', - '!<%= app.dirs.dist %>/server/*', - '!<%= app.dirs.dist %>/bower_components/spin.js' - ] - } - } -}; diff --git a/grunt-tasks/useminPrepare.js b/grunt-tasks/useminPrepare.js deleted file mode 100644 index a81d4fb..0000000 --- a/grunt-tasks/useminPrepare.js +++ /dev/null @@ -1,6 +0,0 @@ -module.exports = { - html: ['<%= app.dirs.client %>/index.html'], - options: { - dest: '<%= app.dirs.dist %>' - } -}; diff --git a/grunt-tasks/watch.js b/grunt-tasks/watch.js deleted file mode 100644 index 58501ba..0000000 --- a/grunt-tasks/watch.js +++ /dev/null @@ -1,63 +0,0 @@ -module.exports = { - injectJS: { - files: [ - '<%= app.dirs.client %>/{app,components}/**/*.js', - '!<%= app.dirs.client %>/{app,components}/**/*.spec.js', - '!<%= app.dirs.client %>/{app,components}/**/*.mock.js', - '!<%= app.dirs.client %>/app/app.js'], - tasks: ['injector:scripts'] - }, - injectCss: { - files: [ - '<%= app.dirs.client %>/{app,components}/**/*.css' - ], - tasks: ['injector:css'] - }, - mochaTest: { - files: ['server/**/*.spec.js'], - tasks: ['env:test', 'mochaTest'] - }, - jsTest: { - files: [ - '<%= app.dirs.client %>/{app,components}/**/*.spec.js', - '<%= app.dirs.client %>/{app,components}/**/*.mock.js' - ], - tasks: ['newer:jshint:all', 'karma'] - }, - injectSass: { - files: [ - '<%= app.dirs.client %>/{app,components}/**/*.{scss,sass}'], - tasks: ['injector:sass'] - }, - sass: { - files: [ - '<%= app.dirs.client %>/{app,components}/**/*.{scss,sass}'], - tasks: ['sass', 'autoprefixer'] - }, - gruntfile: { - files: ['Gruntfile.js'] - }, - livereload: { - files: [ - '{.tmp,<%= app.dirs.client %>}/{app,components}/**/*.css', - '{.tmp,<%= app.dirs.client %>}/{app,components}/**/*.html', - '{.tmp,<%= app.dirs.client %>}/{app,components}/**/*.js', - '!{.tmp,<%= app.dirs.client %>}{app,components}/**/*.spec.js', - '!{.tmp,<%= app.dirs.client %>}/{app,components}/**/*.mock.js', - '<%= app.dirs.client %>/assets/images/{,*//*}*.{png,jpg,jpeg,gif,webp,svg}' - ], - options: { - livereload: true - } - }, - express: { - files: [ - 'server/**/*.{js,json}' - ], - tasks: ['express:dev', 'wait'], - options: { - livereload: true, - nospawn: true //Without this option specified express won't be reloaded - } - } -}; diff --git a/grunt/aliases.yaml b/grunt/aliases.yaml new file mode 100644 index 0000000..311372b --- /dev/null +++ b/grunt/aliases.yaml @@ -0,0 +1,48 @@ +default: + - 'newer:jshint' + - 'jscs' + - 'test' + - 'build' + +serve: + - 'clean:server' + - 'env:all' + - 'injector:sass' + - 'concurrent:server' + - 'injector' + - 'wiredep' + - 'autoprefixer' + - 'express:dev' + # - 'wait' + - 'open' + - 'watch' + +test: + - 'env:all' + - 'env:test' + - 'mochaTest' + - 'clean:server' + - 'injector:sass' + - 'concurrent:test' + - 'injector' + - 'autoprefixer' + - 'karma' + +build: + - 'jshint' + - 'jscs' + - 'clean:dist' + - 'injector:sass' + - 'concurrent:dist' + - 'injector' + - 'wiredep' + - 'useminPrepare' + - 'autoprefixer' + - 'ngtemplates' + - 'concat' + - 'ngAnnotate' + - 'copy:dist' + - 'cssmin' + - 'uglify' + - 'rev' + - 'usemin' diff --git a/grunt-tasks/autoprefixer.js b/grunt/autoprefixer.js similarity index 100% rename from grunt-tasks/autoprefixer.js rename to grunt/autoprefixer.js diff --git a/grunt-tasks/clean.js b/grunt/clean.js similarity index 79% rename from grunt-tasks/clean.js rename to grunt/clean.js index f02f386..41fa2de 100644 --- a/grunt-tasks/clean.js +++ b/grunt/clean.js @@ -4,7 +4,7 @@ module.exports = { dot: true, src: [ '.tmp', - '<%= app.dirs.dist %>/*', + 'dist/*', ] }] }, diff --git a/grunt-tasks/concurrent.js b/grunt/concurrent.js similarity index 100% rename from grunt-tasks/concurrent.js rename to grunt/concurrent.js diff --git a/grunt-tasks/copy.js b/grunt/copy.js similarity index 73% rename from grunt-tasks/copy.js rename to grunt/copy.js index 40bba6b..bdf9478 100644 --- a/grunt-tasks/copy.js +++ b/grunt/copy.js @@ -3,8 +3,8 @@ module.exports = { files: [{ expand: true, dot: true, - cwd: '<%= app.dirs.client %>', - dest: '<%= app.dirs.dist %>', + cwd: 'client', + dest: 'dist', src: [ '*.{ico,png,txt,pdf}', '.htaccess', @@ -15,13 +15,13 @@ module.exports = { }, { expand: true, cwd: '.tmp/images', - dest: '<%= app.dirs.dist %>/assets/images', + dest: 'dist/assets/images', src: ['generated/*'] }] }, styles: { expand: true, - cwd: '<%= app.dirs.client %>', + cwd: 'client', dest: '.tmp/', src: ['{app,components}/**/*.css'] } diff --git a/grunt-tasks/env.js b/grunt/env.js similarity index 100% rename from grunt-tasks/env.js rename to grunt/env.js diff --git a/grunt-tasks/express.js b/grunt/express.js similarity index 87% rename from grunt-tasks/express.js rename to grunt/express.js index a6a8f28..79f8198 100644 --- a/grunt-tasks/express.js +++ b/grunt/express.js @@ -1,6 +1,6 @@ module.exports = { options: { - port: '<%= app.port %>' + port: '<%= port %>' }, dev: { options: { diff --git a/grunt-tasks/imagemin.js b/grunt/imagemin.js similarity index 53% rename from grunt-tasks/imagemin.js rename to grunt/imagemin.js index bb34b02..f3961cb 100644 --- a/grunt-tasks/imagemin.js +++ b/grunt/imagemin.js @@ -2,9 +2,9 @@ module.exports = { dist: { files: [{ expand: true, - cwd: '<%= app.dirs.client %>/assets/images', + cwd: 'client/assets/images', src: '{,*/}*.{png,jpg,jpeg,gif}', - dest: '<%= app.dirs.dist %>/assets/images' + dest: 'dist/assets/images' }] } }; diff --git a/grunt-tasks/injector.js b/grunt/injector.js similarity index 68% rename from grunt-tasks/injector.js rename to grunt/injector.js index 9cf3a2a..91e7776 100644 --- a/grunt-tasks/injector.js +++ b/grunt/injector.js @@ -17,11 +17,11 @@ module.exports = { endtag: '' }, files: { - '<%= app.dirs.client %>/index.html': [ - ['{.tmp,<%= app.dirs.client %>}/{app,components}/**/*.js', - '!{.tmp,<%= app.dirs.client %>}/app/app.js', - '!{.tmp,<%= app.dirs.client %>}/{app,components}/**/*.spec.js', - '!{.tmp,<%= app.dirs.client %>}/{app,components}/**/*.mock.js'] + 'client/index.html': [ + ['{.tmp,client}/{app,components}/**/*.js', + '!{.tmp,client}/app/app.js', + '!{.tmp,client}/{app,components}/**/*.spec.js', + '!{.tmp,client}/{app,components}/**/*.mock.js'] ] } }, @@ -39,9 +39,9 @@ module.exports = { endtag: '// endinjector' }, files: { - '<%= app.dirs.client %>/app/app.scss': [ - '<%= app.dirs.client %>/{app,components}/**/*.{scss,sass}', - '!<%= app.dirs.client %>/app/app.{scss,sass}' + 'client/app/app.scss': [ + 'client/{app,components}/**/*.{scss,sass}', + '!client/app/app.{scss,sass}' ] } }, @@ -59,8 +59,8 @@ module.exports = { endtag: '' }, files: { - '<%= app.dirs.client %>/index.html': [ - '<%= app.dirs.client %>/{app,components}/**/*.css' + 'client/index.html': [ + 'client/{app,components}/**/*.css' ] } } diff --git a/grunt/jscs.js b/grunt/jscs.js new file mode 100644 index 0000000..42fe2ac --- /dev/null +++ b/grunt/jscs.js @@ -0,0 +1,25 @@ +module.exports = { + options: { + config: '.jscsrc' + }, + server: { + src: [ + 'server/**/*.js', + '!server/**/*.spec.js' + ] + }, + serverTest: { + src: ['server/**/*.spec.js'] + }, + all: [ + 'client/{app,components}/**/*.js', + '!client/{app,components}/**/*.spec.js', + '!client/{app,components}/**/*.mock.js' + ], + test: { + src: [ + 'client/{app,components}/**/*.spec.js', + 'client/{app,components}/**/*.mock.js' + ] + } +}; diff --git a/grunt/jshint.js b/grunt/jshint.js new file mode 100644 index 0000000..f7021e8 --- /dev/null +++ b/grunt/jshint.js @@ -0,0 +1,31 @@ +module.exports = { + options: { + jshintrc: 'client/.jshintrc' + }, + server: { + options: { + jshintrc: 'server/.jshintrc' + }, + src: [ + 'server/**/*.js', + '!server/**/*.spec.js' + ] + }, + serverTest: { + options: { + jshintrc: 'server/.jshintrc-spec' + }, + src: ['server/**/*.spec.js'] + }, + all: [ + 'client/{app,components}/**/*.js', + '!client/{app,components}/**/*.spec.js', + '!client/{app,components}/**/*.mock.js' + ], + test: { + src: [ + 'client/{app,components}/**/*.spec.js', + 'client/{app,components}/**/*.mock.js' + ] + } +}; diff --git a/grunt-tasks/karma.js b/grunt/karma.js similarity index 100% rename from grunt-tasks/karma.js rename to grunt/karma.js diff --git a/grunt-tasks/mochaTest.js b/grunt/mochaTest.js similarity index 100% rename from grunt-tasks/mochaTest.js rename to grunt/mochaTest.js diff --git a/grunt-tasks/ngAnnotate.js b/grunt/ngAnnotate.js similarity index 100% rename from grunt-tasks/ngAnnotate.js rename to grunt/ngAnnotate.js diff --git a/grunt-tasks/ngtemplates.js b/grunt/ngtemplates.js similarity index 94% rename from grunt-tasks/ngtemplates.js rename to grunt/ngtemplates.js index cbb4158..44289c5 100644 --- a/grunt-tasks/ngtemplates.js +++ b/grunt/ngtemplates.js @@ -14,7 +14,7 @@ module.exports = { usemin: 'app/app.js' }, main: { - cwd: '<%= app.dirs.client %>', + cwd: 'client', src: ['{app,components}/**/*.html'], dest: '.tmp/templates.js' }, diff --git a/grunt/open.js b/grunt/open.js new file mode 100644 index 0000000..8559eea --- /dev/null +++ b/grunt/open.js @@ -0,0 +1,5 @@ +module.exports = { + server: { + url: 'http://localhost:<%= port %>' + } +}; diff --git a/grunt-tasks/protractor.js b/grunt/protractor.js similarity index 100% rename from grunt-tasks/protractor.js rename to grunt/protractor.js diff --git a/grunt/rev.js b/grunt/rev.js new file mode 100644 index 0000000..b111061 --- /dev/null +++ b/grunt/rev.js @@ -0,0 +1,13 @@ +module.exports = { + dist: { + files: { + src: [ + 'dist/{,*/}*.js', + 'dist/{,*/}*.css', + 'dist/assets/fonts/*', + '!dist/server/*', + '!dist/bower_components/spin.js' + ] + } + } +}; diff --git a/grunt-tasks/sass.js b/grunt/sass.js similarity index 56% rename from grunt-tasks/sass.js rename to grunt/sass.js index 843df12..9219658 100644 --- a/grunt-tasks/sass.js +++ b/grunt/sass.js @@ -3,14 +3,14 @@ module.exports = { options: { sourceMap: true, includePaths: [ - '<%= app.dirs.client %>/bower_components', - '<%= app.dirs.client %>/app', - '<%= app.dirs.client %>/components' + 'client/bower_components', + 'client/app', + 'client/components' ] }, files: [{ expand: true, - cwd: '<%= app.dirs.client %>', + cwd: 'client', src: 'app/app.scss', dest: '.tmp/', ext: '.css' diff --git a/grunt-tasks/usemin.js b/grunt/usemin.js similarity index 58% rename from grunt-tasks/usemin.js rename to grunt/usemin.js index bda304e..b1759cc 100644 --- a/grunt-tasks/usemin.js +++ b/grunt/usemin.js @@ -1,12 +1,13 @@ module.exports = { - html: ['<%= app.dirs.dist %>/{,*/}*.html'], - css: ['<%= app.dirs.dist %>/{,*/}*.css'], - js: ['<%= app.dirs.dist %>/{,*/}*.js'], + html: ['dist/{,*/}*.html'], + css: ['dist/{,*/}*.css'], + js: ['dist/{,*/}*.js'], options: { assetsDirs: [ - '<%= app.dirs.dist %>', - '<%= app.dirs.dist %>/assets/images' + 'dist', + 'dist/assets/images' ], + // This is so we update image references in our ng-templates patterns: { js: [ diff --git a/grunt/useminPrepare.js b/grunt/useminPrepare.js new file mode 100644 index 0000000..67d3d29 --- /dev/null +++ b/grunt/useminPrepare.js @@ -0,0 +1,6 @@ +module.exports = { + html: ['client/index.html'], + options: { + dest: 'dist' + } +}; diff --git a/grunt/watch.js b/grunt/watch.js new file mode 100644 index 0000000..b76d254 --- /dev/null +++ b/grunt/watch.js @@ -0,0 +1,63 @@ +module.exports = { + injectJS: { + files: [ + 'client/{app,components}/**/*.js', + '!client/{app,components}/**/*.spec.js', + '!client/{app,components}/**/*.mock.js', + '!client/app/app.js'], + tasks: ['injector:scripts'] + }, + injectCss: { + files: [ + 'client/{app,components}/**/*.css' + ], + tasks: ['injector:css'] + }, + mochaTest: { + files: ['server/**/*.spec.js'], + tasks: ['env:test', 'mochaTest'] + }, + jsTest: { + files: [ + 'client/{app,components}/**/*.spec.js', + 'client/{app,components}/**/*.mock.js' + ], + tasks: ['newer:jshint:all', 'karma'] + }, + injectSass: { + files: [ + 'client/{app,components}/**/*.{scss,sass}'], + tasks: ['injector:sass'] + }, + sass: { + files: [ + 'client/{app,components}/**/*.{scss,sass}'], + tasks: ['sass', 'autoprefixer'] + }, + gruntfile: { + files: ['Gruntfile.js'] + }, + livereload: { + files: [ + '{.tmp,client}/{app,components}/**/*.css', + '{.tmp,client}/{app,components}/**/*.html', + '{.tmp,client}/{app,components}/**/*.js', + '!{.tmp,client}{app,components}/**/*.spec.js', + '!{.tmp,client}/{app,components}/**/*.mock.js', + 'client/assets/images/{,*//*}*.{png,jpg,jpeg,gif,webp,svg}' + ], + options: { + livereload: true + } + }, + express: { + files: [ + 'server/**/*.{js,json}' + ], + tasks: ['express:dev', 'wait'], + options: { + livereload: true, + nospawn: true //Without this option specified express won't be reloaded + } + } +}; diff --git a/grunt-tasks/wiredep.js b/grunt/wiredep.js similarity index 63% rename from grunt-tasks/wiredep.js rename to grunt/wiredep.js index 13777c8..db7205d 100644 --- a/grunt-tasks/wiredep.js +++ b/grunt/wiredep.js @@ -1,7 +1,7 @@ module.exports = { target: { - src: '<%= app.dirs.client %>/index.html', - ignorePath: '<%= app.dirs.client %>/', + src: 'client/index.html', + ignorePath: 'client/', exclude: [/bootstrap-sass-official/, /bootstrap.js/, '/json3/', '/es5-shim/', /bootstrap.css/, /font-awesome.css/ ] } }; diff --git a/package.json b/package.json index d93d0e8..3ce0cc6 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "karma-phantomjs-launcher": "~0.1.4", "karma-requirejs": "~0.2.1", "karma-script-launcher": "~0.1.0", + "load-grunt-config": "^0.17.2", "open": "0.0.5", "requirejs": "^2.1.17", "should": "^6.0.3",