donal
2018-04-26 9f311a49e45ae2999e2cf510cd16a8abb6a738bf
commit | author | age
6ef0cc 1 // Generated on 2016-08-09 using generator-angular-fullstack 2.1.1
D 2 'use strict';
3
4 module.exports = function (grunt) {
c382ab 5   let localConfig;
6ef0cc 6   try {
D 7     localConfig = require('./server/config/local.env');
8   } catch(e) {
9     localConfig = {};
10   }
11
12   // Load grunt tasks automatically, when needed
13   require('jit-grunt')(grunt, {
14     express: 'grunt-express-server'
15   });
16
17   // Time how long tasks take. Can help when optimizing build times
18   require('time-grunt')(grunt);
19
20   grunt.task.loadTasks('./tasks');
21
22   // Define the configuration for all the tasks
23   grunt.initConfig({
24
25     // Project settings
26     pkg: grunt.file.readJSON('package.json'),
27     express: {
28       options: {
29         port: process.env.PORT || 9000
30       },
31       dev: {
32         options: {
33           script: 'server/app.js',
34           debug: true
35         }
36       },
37       prod: {
38         options: {
39           script: 'dist/server/app.js'
40         }
41       }
42     },
43
44     // Make sure code styles are up to par and there are no obvious mistakes
45     jshint: {
46       options: {
47         reporter: require('jshint-stylish')
48       },
49       server: {
50         options: {
51           jshintrc: 'server/.jshintrc'
52         },
53         src: [
54           'server/**/*.js',
55           '!server/**/*.spec.js'
56         ]
57       },
58       serverTest: {
59         options: {
60           jshintrc: 'server/.jshintrc'
61         },
62         src: ['server/**/*.spec.js']
63       },
64       ci_server: {
65         options: {
66           jshintrc: 'server/.jshintrc',
67           reporter: require('jshint-jenkins-checkstyle-reporter'),
68           reporterOutput: 'reports/server/linting/jshint-server.xml'
69         },
70         src: [
71           'server/**/*.js',
72           'server/**/*.spec.js'
73         ]
74       },
346f73 75       // bm_server: {
D 76       //   options: {
77       //     jshintrc: 'server/.jshintrc',
78       //     reporter: require('jshint-junit-reporter'),
79       //     reporterOutput: 'reports/server/linting/jshint-junit-server.xml'
80       //   },
81       //   src: [
82       //     'server/**/*.js',
83       //     'server/**/*.spec.js'
84       //   ]
85       // }
6ef0cc 86     },
D 87
88     // Empties folders to start fresh
89     clean: {
90       server: '.tmp',
91       mochareports: 'reports/server/mocha/**',
6a748c 92       lint: 'reports/{server}/linting/**',
D 93       coverage: 'reports/coverage/**'
6ef0cc 94     },
D 95
96     // Use nodemon to run server in debug mode with an initial breakpoint
97     nodemon: {
98       debug: {
99         script: 'server/app.js',
100         options: {
101           nodeArgs: ['--debug-brk'],
102           env: {
103             PORT: process.env.PORT || 9000
104           },
105           callback: function (nodemon) {
106             nodemon.on('log', function (event) {
107               console.log(event.colour);
108             });
109
110             // opens browser on initial server start
111             nodemon.on('config:update', function () {
112               setTimeout(function () {
113                 require('open')('http://localhost:8080/debug?port=5858');
114               }, 500);
115             });
116           }
117         }
118       }
119     },
120
121     // Run some tasks in parallel to speed up the build process
122     concurrent: {
123       server: [
124       ],
125       test: [
126       ],
127       debug: {
128         tasks: [
129           'nodemon',
130         ],
131         options: {
132           logConcurrentOutput: true
133         }
134       },
135       dist: [
136       ]
137     },
138
139     mochaTest: {
140       terminal: {
141         options: {
142           reporter: 'spec',
143           require: 'tasks/blanket'
144         },
145         src: ['server/**/*.spec.js']
146       },
147       junit: {
148         options: {
149           reporter: 'mocha-junit-reporter',
150           // require: require("blanket"),
151           require: 'tasks/blanket'
152         },
153         src: ['server/**/*.spec.js']
154       },
155       html: {
156         options: {
157           // reporters are  ['html-cov', 'json-cov', 'travis-cov', 'mocha-lcov-reporter', 'mocha-cobertura-reporter'],
158           reporter: 'html-cov',
159           quiet: true,
160           captureFile: 'reports/server/coverage/cobertura-report.html'
161         },
162         src: ['server/**/*.spec.js']
163       },
164       cobertura: {
165         options: {
166           reporter: 'mocha-cobertura-reporter',
167           // use the quiet flag to suppress the mocha console output
168           quiet: true,
169           // specify a destination file to capture the mocha
170           // output (the quiet option does not suppress this)
171           captureFile: 'reports/server/coverage/cobertura-report.xml'
172         },
173         src: ['server/**/*.spec.js']
174       },
175       travis: {
176         options: {
177           reporter: 'travis-cov',
178           quiet: false
179         },
180         src: ['server/**/*.spec.js']
181       }
182     },
183
184     env: {
185       test: {
186         NODE_ENV: 'test'
187       },
188       dev: {
189         NODE_ENV: 'development'
190       },
191       ci: {
192         NODE_ENV: 'ci'
193       },
194       si: {
195         NODE_ENV: 'si'
196       },
197       prod: {
198         NODE_ENV: 'production'
199       },
200       jenkins: {
201         MOCHA_FILE: 'reports/server/mocha/test-results.xml'
202       },
203       all: localConfig
204     },
205   });
206
207   // Used for delaying livereload until after server has restarted
208   grunt.registerTask('wait', function () {
209     grunt.log.ok('Waiting for server reload...');
210
c382ab 211     const done = this.async();
6ef0cc 212
D 213     setTimeout(function () {
214       grunt.log.writeln('Done waiting!');
215       done();
216     }, 1500);
217   });
218
219   grunt.registerTask('express-keepalive', 'Keep grunt running', function() {
220     this.async();
221   });
222
223   grunt.registerTask('build-image', 'Build the image', function(imageId) {
c382ab 224     const shell = require("shelljs");
6ef0cc 225     grunt.log.ok('BUILDING IMAGE');
D 226     if (!imageId) {
227       grunt.fail.warn('must supply an imageId to build');
228     }
c382ab 229     const rc = shell.exec('docker build -t todolist:' + imageId + ' -f ./dist/Dockerfile ./dist').code;
6ef0cc 230     if (rc > 0){
D 231       grunt.fail.warn("DOCKER FAILURE")
232     }
233   });
234
235   grunt.registerTask('deploy', 'deploy the node js app to a docker container and start it in the correct mode', function(target_env, build_tag) {
236     grunt.log.ok('this task must run on a host that has the Docker Daemon running on it');
c382ab 237     const ports = {
6ef0cc 238       ci: '9001',
D 239       si: '9002',
240       production: '80'
241     };
242
243     if (target_env === undefined || build_tag === undefined){
244       grunt.fail.warn('Required param not set - use grunt deploy\:\<target\>\:\<tag\>');
245     } else {
c382ab 246       const shell = require("shelljs");
6ef0cc 247       grunt.log.ok('STOPPING AND REMOVING EXISTING CONTAINERS');
D 248       shell.exec('docker stop todolist-'+ target_env + ' && docker rm todolist-'+ target_env);
249
250       grunt.log.ok('DEPLOYING ' + target_env + ' CONTAINER');
251       if (target_env === 'ci'){
c382ab 252         const rc = shell.exec('docker run -t -d --name todolist-' + target_env + ' -p ' + ports[target_env]+ ':'+ports[target_env]+' --env NODE_ENV=' + target_env + ' todolist:' + build_tag);
6ef0cc 253         if (rc > 0){
D 254           grunt.fail.warn("DOCKER FAILURE")
255         }
256       } else {
257         // ensure mongo is up
c382ab 258         const isMongo = shell.exec('docker ps | grep devops-mongo').code;
6ef0cc 259         if (isMongo > 0){
D 260           grunt.log.ok('DEPLOYING Mongodb CONTAINER FIRST');
261           shell.exec('docker run --name devops-mongo -p 27017:27017 -d mongo');
262         }
c382ab 263         const rc = shell.exec('docker run -t -d --name todolist-' + target_env + ' --link devops-mongo:mongo.server -p '
6ef0cc 264             + ports[target_env]+ ':' + ports[target_env] + ' --env NODE_ENV=' + target_env + ' todolist:' + build_tag).code;
D 265         if (rc > 0){
266           grunt.fail.warn("DOCKER FAILURE");
267         }
268       }
269     }
270
271   });
272
273   grunt.registerTask('serve', function (target) {
274     if (target === 'dist') {
c382ab 275       return grunt.task.run(['build', 'env:all', 'env:prod', 'express:prod', 'wait', 'express-keepalive']);
6ef0cc 276     }
D 277
278     if (target === 'debug') {
279       return grunt.task.run([
280         'clean:server',
281         'env:all',
282         'concurrent:server',
283         'concurrent:debug'
284       ]);
285     }
286
287     grunt.task.run([
288       'clean:server',
289       'env:all',
290       'env:' + (target || 'dev'),
291       'express:dev',
292       'wait',
c382ab 293       'express-keepalive'
6ef0cc 294     ]);
D 295   });
296
297   grunt.registerTask('test', function(target, environ) {
298     environ = environ !== undefined ? environ : 'test';
c382ab 299     const reporter = 'terminal';
D 300     const coverage = 'travis';
6ef0cc 301     if (target === 'server-jenkins') {
D 302       target = 'server';
303       reporter = 'junit';
304       coverage = 'cobertura';
305       grunt.task.run(['env:jenkins']);
306     }
307     if (target === 'server') {
308       return grunt.task.run([
309         'clean:mochareports',
310         'clean:coverage',
311         'env:all',
312         'env:'+environ,
313         'mochaTest:' + reporter,
314         'mochaTest:' + 'html',
315         'mochaTest:' + coverage
316       ]);
317     }
318
319     else grunt.task.run([
320       'test:server'
321     ]);
322   });
323
324   grunt.registerTask('build', [
6a748c 325     'clean',
6ef0cc 326     'concurrent:dist',
6a748c 327     'newer:jshint',
D 328     'test',
6ef0cc 329   ]);
D 330
331   grunt.registerTask('default', [
332     'newer:jshint',
333     'test',
334     'build'
335   ]);
336 };