-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGruntfile.js
341 lines (310 loc) · 9.08 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
'use strict';
/* Gruntfile for api-tools-admin-ui
*
* Primary Targets:
* - test: run karma tests
* - build: build the distribution version
* - serve: runs a static HTTP server; watches for changes in JS, tests, HTML,
* and this file, and runs jshint, runs tests, builds HTML template file, and
* reloads the server. Accepts the following options:
* - `--port` to specify a port to run the HTTP server on; if none is
* specified, 3000 is used.
* - `--api` to specify the URI to the API; uses
* http://localhost:9000/api-tools/api if none is specified.
* - `--docs` to specify the URI to documentation (referenced in the
* documentation navigation item); uses
* http://localhost:9000/api-tools/documentation if none is specified.
* - `--host` to specify the hostname to use; defaults to "localhost".
* - default: runs jshint, tests, and build
*/
module.exports = function(grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
// Time how long tasks take. Can help when optimizing build times
require('time-grunt')(grunt);
var fs = require('fs');
var urlparser = require('url').parse;
// Define the configuration for all the tasks
grunt.initConfig({
// Project settings
api_tools: {
dist: 'dist',
ui: 'src'
},
// Connect - static server - settings
connect: {
options: {
port: grunt.option('port') || 3000,
livereload: 35729,
hostname: '0.0.0.0',
},
livereload: {
options: {
base: '<%= api_tools.ui %>',
middleware: function (connect, options, middlewares) {
middlewares = middlewares || [];
var host = grunt.option('host') || 'localhost';
var base = 'http://' + host + ':' + options.port + '/';
var api = grunt.option('api') || 'http://' + host + ':9000/api';
var docs = grunt.option('docs') || 'http://' + host + ':9000/api-tools/documentation';
var index = fs.readFileSync(options.base + '/index.html', { encoding: 'utf-8' });
index = index.replace(/%BASE_HREF%/g, base);
index = index.replace(/%API_BASE%/g, api);
index = index.replace(/%DOC_URI%/g, docs);
middlewares.unshift(function (req, res, next) {
var path = urlparser(req.url).pathname;
if (path !== '/' && path !== '/index.html') {
return next();
}
if (req.method !== 'GET') {
res.statusCode = 405;
res.setHeader('Allow', 'GET');
res.end();
return;
}
return res.end(index);
});
return middlewares;
}
}
}
},
// Watches files for changes and runs tasks based on the changed files
watch: {
js: {
files: ['<%= api_tools.ui %>/api-tools-ui/{,**/}*.js'],
tasks: ['newer:jshint:all', 'karma']
},
jsTest: {
files: ['test/{,**/}*.js'],
tasks: ['newer:jshint:test', 'karma']
},
/*
less: {
files: ['<%= api_tools.app %>/less/{,**<delete brackets and this message>/}*.less'],
tasks: ['less:server']
},
*/
gruntfile: {
files: ['Gruntfile.js']
},
livereload: {
options: {
livereload: '<%= connect.options.livereload %>'
},
files: [
'<%= api_tools.ui %>/{,**/}*.html',
'<%= api_tools.ui %>/api-tools-ui/css/{,**/}*.css',
'<%= api_tools.ui %>/api-tools-ui/img/{,**/}*',
'<%= api_tools.ui %>/api-tools-ui/{,**/}*.js'
]
},
html2js: {
files: ['<%= api_tools.ui %>/api-tools-ui/{,**/}*.html'],
tasks: ['html2js']
}
},
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish'),
ignores: ['<%= api_tools.ui %>/api-tools-ui/templates.js'],
},
all: [
'Gruntfile.js',
'<%= api_tools.ui %>/api-tools-ui/{,**/}*.js'
],
test: {
options: {
},
src: [
'test/unit/{,**/}*.js'
]
}
},
// Empties folders to start fresh
clean: {
dist: {
files: [{
dot: true,
src: [
'.tmp',
'<%= api_tools.dist %>/*',
'!<%= api_tools.dist %>/.git*'
]
}]
},
server: '.tmp'
},
// Compiles Less to CSS
/*
less: {
options: {
// Adds additional paths for import (so can import bower_components as well)
paths: [
'<%= api_tools.app %>/less',
'<%= api_tools.app %>/vendor'
]
},
server: {
files: {
'<%= api_tools.app %>/css/main.css': '<%= api_tools.app %>/less/main.less',
'<%= api_tools.app %>/css/vendor.css': '<%= api_tools.app %>/less/vendor.less'
},
options: {
cleancss: false
}
}
// Dist is handled by usemin/cssmin of the above 'server' config
},
*/
// Reads HTML for usemin blocks to enable smart builds that automatically
// concat, minify and revision files. Creates configurations in memory so
// additional tasks can operate on them
useminPrepare: {
html: '<%= api_tools.dist %>/index.html',
options: {
dest: '<%= api_tools.dist %>',
root: '<%= api_tools.ui %>'
}
},
// Performs rewrites based on rev and the useminPrepare configuration
usemin: {
html: ['<%= api_tools.dist %>/{,**/}*.html'],
css: ['<%= api_tools.dist %>/{,**/}*.css'],
options: {
assetsDirs: ['<%= api_tools.dist %>']
}
},
htmlmin: {
dist: {
options: {
// Optional configurations that you can uncomment to use
// removeCommentsFromCDATA: true,
// collapseBooleanAttributes: true,
// removeAttributeQuotes: true,
// removeRedundantAttributes: true,
// useShortDoctype: true,
// removeEmptyAttributes: true,
// removeOptionalTags: true*/
},
files: [{
expand: true,
cwd: '<%= api_tools.ui %>',
src: ['*.html', 'html/**/*.html'],
dest: '<%= api_tools.dist %>'
}]
}
},
// Copies remaining files to places other tasks can use
copy: {
dist: {
files: [
{
expand: true,
dot: true,
cwd: '<%= api_tools.ui %>',
dest: '<%= api_tools.dist %>',
src: [
'index.html',
'*.{ico,png,txt}',
'api-tools-ui/img/**'
]
},
{
expand: true,
dot: false,
flatten: true,
cwd: '<%= api_tools.ui %>',
dest: '<%= api_tools.dist %>/api-tools-ui/fonts',
src: [
'vendor/bootstrap/dist/fonts/*.{eot,woff,woff2,ttf,svg}',
'vendor/sass-bootstrap-glyphicons/fonts/*.{eot,woff,woff2,ttf,svg}'
]
}
]
}
},
// Run some tasks in parallel to speed up the build process
concurrent: {
server: [
/* 'less:server' */
],
test: [
/* 'less:server' */
],
dist: [
/* 'less:server', */
'htmlmin'
]
},
// Test settings
karma: {
unit: {
configFile: 'test/karma.conf.js',
singleRun: true,
autoWatch: false
}
},
//Collect all html views into single template
html2js: {
options: {
base: '<%= api_tools.ui %>'
},
main: {
src: ['<%= api_tools.ui %>/api-tools-ui/**/*.html'],
dest: '<%= api_tools.ui %>/api-tools-ui/templates.js'
},
},
rev: {
files: [
'<%= api_tools.dist %>/api-tools-ui/{,**/}*.css',
'<%= api_tools.dist %>/api-tools-ui/{,**/}*.js'
]
}
});
grunt.registerTask('monkeyPatches', function () {
// monkeypatch FileProcessor to include utf-8
var FileProcessor = require('grunt-usemin/lib/fileprocessor');
FileProcessor.prototype.replaceWithOld = FileProcessor.prototype.replaceWith;
FileProcessor.prototype.replaceWith = function replaceWith(block) {
var script = FileProcessor.prototype.replaceWithOld(block);
if (script.match(/<script src/)) {
script = script.replace('></script>', ' charset="utf-8"></script>');
}
return script;
};
});
grunt.registerTask('serve', function(target) {
grunt.task.run([
'clean:server',
// 'concurrent:server',
'connect:livereload',
'watch'
]);
});
grunt.registerTask('test', [
'clean:server',
// 'concurrent:test',
'karma'
]);
grunt.registerTask('build', [
'clean:dist',
'html2js',
'copy:dist',
'useminPrepare',
'concurrent:dist',
'concat',
'uglify',
'cssmin',
'rev',
'monkeyPatches',
'usemin'
]);
grunt.registerTask('default', [
'newer:jshint',
'test',
'build'
]);
};