-
Notifications
You must be signed in to change notification settings - Fork 9
/
Gruntfile.js
166 lines (142 loc) · 4.45 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
module.exports = function(grunt) {
var databaseUrl;
var re;
var swagger;
var swaggerFile = __dirname + '/api/swagger/swagger.yaml';
var paths = {
js: ['*.js', 'api/**/*.js', 'lib/*.js', '!test/coverage/**', '!bower_components/**', '!newrelic.js']
};
if (process.env.NODE_ENV !== 'production') {
require('time-grunt')(grunt);
}
var envConfig = require('config');
// @TODO setup test heroku so we don't need this
if (envConfig.has('datasource.pgURLWercker')) {
databaseUrl = envConfig.get('datasource.pgURLWercker');
} else if (envConfig.has('datasource.pgURL')) {
databaseUrl = envConfig.get('datasource.pgURL');
} else {
databaseUrl = 'postgres://' + envConfig.get('datasource.pg.username') +
':' + envConfig.get('datasource.pg.password') +
'@' + envConfig.get('datasource.pg.host') +
':' + envConfig.get('datasource.pg.port') +
'/' + envConfig.get('datasource.pg.database');
}
// Project Configuration
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
js: {
files: paths.js,
tasks: ['jshint']
}
},
jshint: {
all: {
src: paths.js,
options: {
jshintrc: true
}
}
},
nodemon: {
dev: {
script: 'app.js',
options: {
args: [],
ignore: ['node_modules/**'],
ext: 'js,html',
nodeArgs: ['--debug'],
delayTime: 1,
cwd: __dirname
}
}
},
concurrent: {
tasks: ['nodemon', 'watch'],
options: {
logConcurrentOutput: true
}
},
mochaTest: {
test: {
options: {
reporter: 'spec',
require: [
'app.js'
]
},
src: ['test/**/*.js']
}
},
migrate: {
options: {
env: {
DATABASE_URL: databaseUrl // the databaseUrl is resolved at the beginning based on the NODE_ENV, this value injects the config in the database.json
},
'migrations-dir': 'config/schema-migrations', // defines the dir for the migration scripts
verbose: true // tell me more stuff
}
},
'swagger-js-codegen': {
options: {
apis: [
{
swagger: 'http://lc1-challenge-service.herokuapp.com/api-docs', // The location of the swagger file
moduleName: 'challenge-consumer', // The name of the file and class
className: 'Challenge'
}
],
dest: 'lib' // Where the file should be generated.
},
dist: {
}
}
});
//Load NPM tasks
require('load-grunt-tasks')(grunt);
grunt.loadNpmTasks('grunt-contrib-jshint');
//Default task(s).
grunt.registerTask('default', ['jshint', 'concurrent']);
grunt.registerTask('validate', ['mochaTest', 'jshint']);
//Test task.
grunt.registerTask('test', ['dbmigrate', 'mochaTest', 'yamlTest', 'jshint']);
// For Heroku users only.
grunt.registerTask('heroku:production', ['jshint']);
// db migrate
grunt.registerTask('dbmigrate', 'db up all the applicable scripts', function () {
grunt.task.run('migrate:up');
}); grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.registerTask('dbdown', 'db down all the applicable scripts', function () {
grunt.task.run('migrate:down');
});
// yaml tester for ./api/swagger/swagger.yaml
grunt.task.registerTask('yamlTest', 'Test Swagger spec file', function() {
// load the grunt-swagger-tools
try {
// https://www.npmjs.org/package/grunt-swagger-tools
swagger = require('grunt-swagger-tools')();
// // Setup 2.0 Swagger spec compliant using YAML format
swagger.validator.set('fileext', '.yaml');
// No logging of loaded YAML data
swagger.validator.set('log', 'true');
// Run the validator on file at swaggerFile
console.log('YAML Test for file: ' + swaggerFile + '\n');
re = swagger.validator.Validate(swaggerFile, undefined, {version: '2.0'});
} catch (e) { re = e.message; }
// If has error, result in console
console.log('YAML 2.0 RESULT: ' + re + '\n');
});
grunt.registerTask('cleandb', 'Clean db and re-apply all migrations', function () {
var fs = require('fs');
var files = fs.readdirSync('./config/schema-migrations');
if (files) {
files.forEach(function() {
grunt.task.run('migrate:down');
});
grunt.task.run('migrate:up');
}
});
grunt.registerTask('updateClient', ['swagger-js-codegen']);
};