donal
2018-03-26 6ef0cc963d40c64fb56ba0545c05788535a45895
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) {
5   var localConfig;
6   try {
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       },
75       bm_client: {
76         options: {
77           jshintrc: 'client/.jshintrc',
78           reporter: require('jshint-junit-reporter'),
79           reporterOutput: 'reports/client/linting/jshint-junit-client.xml'
80         },
81         src: [
82           '<%= yeoman.client %>/app/**/*.js',
83           '<%= yeoman.client %>/app/**/*.spec.js',
84           '<%= yeoman.client %>/app/**/*.mock.js'
85         ]
86       },
87       bm_server: {
88         options: {
89           jshintrc: 'server/.jshintrc',
90           reporter: require('jshint-junit-reporter'),
91           reporterOutput: 'reports/server/linting/jshint-junit-server.xml'
92         },
93         src: [
94           'server/**/*.js',
95           'server/**/*.spec.js'
96         ]
97       }
98     },
99
100     // Empties folders to start fresh
101     clean: {
102       server: '.tmp',
103       karmareports: 'reports/client/karma/**',
104       mochareports: 'reports/server/mocha/**',
105       lint: 'reports/{server,client}/jshint/**',
106       coverage: 'reports/{server,client}/coverage/**'
107     },
108
109     // Debugging with node inspector
110     'node-inspector': {
111       custom: {
112         options: {
113           'web-host': 'localhost'
114         }
115       }
116     },
117
118     // Use nodemon to run server in debug mode with an initial breakpoint
119     nodemon: {
120       debug: {
121         script: 'server/app.js',
122         options: {
123           nodeArgs: ['--debug-brk'],
124           env: {
125             PORT: process.env.PORT || 9000
126           },
127           callback: function (nodemon) {
128             nodemon.on('log', function (event) {
129               console.log(event.colour);
130             });
131
132             // opens browser on initial server start
133             nodemon.on('config:update', function () {
134               setTimeout(function () {
135                 require('open')('http://localhost:8080/debug?port=5858');
136               }, 500);
137             });
138           }
139         }
140       }
141     },
142
143     // Run some tasks in parallel to speed up the build process
144     concurrent: {
145       server: [
146       ],
147       test: [
148       ],
149       debug: {
150         tasks: [
151           'nodemon',
152           'node-inspector'
153         ],
154         options: {
155           logConcurrentOutput: true
156         }
157       },
158       dist: [
159       ]
160     },
161
162     mochaTest: {
163       terminal: {
164         options: {
165           reporter: 'spec',
166           require: 'tasks/blanket'
167         },
168         src: ['server/**/*.spec.js']
169       },
170       junit: {
171         options: {
172           reporter: 'mocha-junit-reporter',
173           // require: require("blanket"),
174           require: 'tasks/blanket'
175         },
176         src: ['server/**/*.spec.js']
177       },
178       html: {
179         options: {
180           // reporters are  ['html-cov', 'json-cov', 'travis-cov', 'mocha-lcov-reporter', 'mocha-cobertura-reporter'],
181           reporter: 'html-cov',
182           quiet: true,
183           captureFile: 'reports/server/coverage/cobertura-report.html'
184         },
185         src: ['server/**/*.spec.js']
186       },
187       cobertura: {
188         options: {
189           reporter: 'mocha-cobertura-reporter',
190           // use the quiet flag to suppress the mocha console output
191           quiet: true,
192           // specify a destination file to capture the mocha
193           // output (the quiet option does not suppress this)
194           captureFile: 'reports/server/coverage/cobertura-report.xml'
195         },
196         src: ['server/**/*.spec.js']
197       },
198       travis: {
199         options: {
200           reporter: 'travis-cov',
201           quiet: false
202         },
203         src: ['server/**/*.spec.js']
204       }
205     },
206
207     env: {
208       test: {
209         NODE_ENV: 'test'
210       },
211       dev: {
212         NODE_ENV: 'development'
213       },
214       ci: {
215         NODE_ENV: 'ci'
216       },
217       si: {
218         NODE_ENV: 'si'
219       },
220       prod: {
221         NODE_ENV: 'production'
222       },
223       jenkins: {
224         MOCHA_FILE: 'reports/server/mocha/test-results.xml'
225       },
226       all: localConfig
227     },
228   });
229
230   // Used for delaying livereload until after server has restarted
231   grunt.registerTask('wait', function () {
232     grunt.log.ok('Waiting for server reload...');
233
234     var done = this.async();
235
236     setTimeout(function () {
237       grunt.log.writeln('Done waiting!');
238       done();
239     }, 1500);
240   });
241
242   grunt.registerTask('express-keepalive', 'Keep grunt running', function() {
243     this.async();
244   });
245
246   grunt.registerTask('build-image', 'Build the image', function(imageId) {
247     var shell = require("shelljs");
248     grunt.log.ok('BUILDING IMAGE');
249     if (!imageId) {
250       grunt.fail.warn('must supply an imageId to build');
251     }
252     var rc = shell.exec('docker build -t todolist:' + imageId + ' -f ./dist/Dockerfile ./dist').code;
253     if (rc > 0){
254       grunt.fail.warn("DOCKER FAILURE")
255     }
256   });
257
258   grunt.registerTask('deploy', 'deploy the node js app to a docker container and start it in the correct mode', function(target_env, build_tag) {
259     grunt.log.ok('this task must run on a host that has the Docker Daemon running on it');
260     var ports = {
261       ci: '9001',
262       si: '9002',
263       production: '80'
264     };
265
266     if (target_env === undefined || build_tag === undefined){
267       grunt.fail.warn('Required param not set - use grunt deploy\:\<target\>\:\<tag\>');
268     } else {
269       var shell = require("shelljs");
270       grunt.log.ok('STOPPING AND REMOVING EXISTING CONTAINERS');
271       shell.exec('docker stop todolist-'+ target_env + ' && docker rm todolist-'+ target_env);
272
273       grunt.log.ok('DEPLOYING ' + target_env + ' CONTAINER');
274       if (target_env === 'ci'){
275         var 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);
276         if (rc > 0){
277           grunt.fail.warn("DOCKER FAILURE")
278         }
279       } else {
280         // ensure mongo is up
281         var isMongo = shell.exec('docker ps | grep devops-mongo').code;
282         if (isMongo > 0){
283           grunt.log.ok('DEPLOYING Mongodb CONTAINER FIRST');
284           shell.exec('docker run --name devops-mongo -p 27017:27017 -d mongo');
285         }
286         var rc = shell.exec('docker run -t -d --name todolist-' + target_env + ' --link devops-mongo:mongo.server -p '
287             + ports[target_env]+ ':' + ports[target_env] + ' --env NODE_ENV=' + target_env + ' todolist:' + build_tag).code;
288         if (rc > 0){
289           grunt.fail.warn("DOCKER FAILURE");
290         }
291       }
292     }
293
294   });
295
296   grunt.registerTask('serve', function (target) {
297     if (target === 'dist') {
298       return grunt.task.run(['build', 'env:all', 'env:prod', 'express:prod', 'wait', 'open', 'express-keepalive']);
299     }
300
301     if (target === 'debug') {
302       return grunt.task.run([
303         'clean:server',
304         'env:all',
305         'concurrent:server',
306         'concurrent:debug'
307       ]);
308     }
309
310     grunt.task.run([
311       'clean:server',
312       'env:all',
313       'env:' + (target || 'dev'),
314       'express:dev',
315       'wait',
316       'open',
317       'watch'
318     ]);
319   });
320
321   grunt.registerTask('test', function(target, environ) {
322     environ = environ !== undefined ? environ : 'test';
323     var usePhantom = false;
324     if (environ === 'phantom') {
325       environ = 'test';
326       usePhantom = true;
327     }
328     var reporter = 'terminal';
329     var coverage = 'travis';
330     if (target === 'server-jenkins') {
331       target = 'server';
332       reporter = 'junit';
333       coverage = 'cobertura';
334       grunt.task.run(['env:jenkins']);
335     }
336     if (target === 'server') {
337       return grunt.task.run([
338         'clean:mochareports',
339         'clean:coverage',
340         'env:all',
341         'env:'+environ,
342         'mochaTest:' + reporter,
343         'mochaTest:' + 'html',
344         'mochaTest:' + coverage
345       ]);
346     }
347
348     else grunt.task.run([
349       'test:server'
350     ]);
351   });
352
353   grunt.registerTask('build', [
354     'clean:dist',
355     'concurrent:dist',
356   ]);
357
358   grunt.registerTask('default', [
359     'newer:jshint',
360     'test',
361     'build'
362   ]);
363 };