-
Notifications
You must be signed in to change notification settings - Fork 10
/
Gruntfile.js
344 lines (322 loc) · 10.8 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
342
343
344
// Generated on 2013-06-26 using generator-webapp 0.2.4
'use strict';
var child_process = require('child_process');
var fs = require('fs');
var path = require('path');
var proxy = require('proxy-middleware');
var LIVERELOAD_PORT = 35729;
var lrSnippet = require('connect-livereload')({port: LIVERELOAD_PORT});
var mountFolder = function (connect, dir) {
return connect.static(require('path').resolve(dir));
};
var addHeaders = function(headers) {
return function(req, res, next) {
for (var key in headers) {
if (req.url == "/relay.html" && key == "X-Frame-Options")
continue;
res.setHeader(key, headers[key]);
}
next();
};
}
var proxyPath = function(route, port) {
return proxy({
route: route,
protocol: 'http:',
hostname: '127.0.0.1',
port: port,
pathname: '/'
});
};
var KDC_PROXY_PORT = 5001;
// # Globbing
// for performance reasons we're only matching one level down:
// 'test/spec/{,*/}*.js'
// use this if you want to recursively match all subfolders:
// 'test/spec/**/*.js'
module.exports = function (grunt) {
// load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
// configurable paths
var yeomanConfig = {
app: 'app',
dist: 'out'
};
/*
var appConfig = {
realm: 'ATHENA.MIT.EDU',
server: 'https://roost-api.mit.edu',
serverPrincipal: 'HTTP/roost-api.mit.edu',
webathena: 'https://webathena.mit.edu'
};
if (grunt.option('realm'))
appConfig.server = grunt.option('realm');
if (grunt.option('server'))
appConfig.server = grunt.option('server');
if (grunt.option('server-principal'))
appConfig.serverPrincipal = grunt.option('server-principal');
if (grunt.option('webathena'))
appConfig.webathena = grunt.option('webathena');
*/
// Declare non-HSTS headers here, so they can be emitted both to
// .htaccess and in the dev server.
var csp = "default-src 'self'; " +
"style-src 'self' https://fonts.googleapis.com; " +
"font-src https://themes.googleusercontent.com; " +
"object-src 'none'";
var headers = {
// Standard header; Chrome 25+
'Content-Security-Policy': csp,
// Firefox and IE.
'X-Content-Security-Policy': csp,
// Safari 6+ and Chrome < 25
'X-WebKit-CSP': csp,
// XSS filters can sometimes be abused to selectively disable
// script tags. With inline script disabled, it's probably
// fine, but it's configure them to hard-fail anyway.
'X-XSS-Protection': '1; mode=block',
// Disallow iframes to do a bit against click-jacking.
'X-Frame-Options': 'deny',
// Disable content sniffing, per Tangled Web. Though it's not
// a huge deal as we're completely static.
'X-Content-Options': 'nosniff'
};
grunt.initConfig({
// app: appConfig,
yeoman: yeomanConfig,
watch: {
options: {
nospawn: true
},
livereload: {
options: {
livereload: LIVERELOAD_PORT
},
files: [
'<%= yeoman.app %>/*.html',
'{.tmp,<%= yeoman.app %>}/styles/{,*/}*.css',
'{.tmp,<%= yeoman.app %>}/scripts/{,*/}*.js',
'<%= yeoman.app %>/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
]
}
},
connect: {
options: {
port: 5000,
// change this to '0.0.0.0' to access the server from outside
hostname: 'localhost'
},
livereload: {
options: {
middleware: function (connect) {
return [
// livereload and CSP don't play well.
// lrSnippet,
addHeaders(headers),
proxyPath('/kdc', KDC_PROXY_PORT),
mountFolder(connect, '.tmp'),
mountFolder(connect, yeomanConfig.app + '/dev-overlay'),
mountFolder(connect, yeomanConfig.app)
];
}
}
},
test: {
options: {
middleware: function (connect) {
return [
mountFolder(connect, '.tmp'),
mountFolder(connect, 'test'),
mountFolder(connect, yeomanConfig.app + '/dev-overlay'),
mountFolder(connect, yeomanConfig.app)
];
}
}
},
dist: {
options: {
middleware: function (connect) {
return [
addHeaders(headers),
proxyPath('/kdc', KDC_PROXY_PORT),
mountFolder(connect, yeomanConfig.dist)
];
}
}
}
},
open: {
server: {
path: 'http://localhost:<%= connect.options.port %>'
}
},
clean: {
dist: {
files: [{
dot: true,
src: [
'.tmp',
'<%= yeoman.dist %>/*',
'!<%= yeoman.dist %>/.git*'
]
}]
},
server: [
'.tmp',
'<%= yeoman.app %>/scripts-src/config.js'
]
},
mocha: {
all: {
options: {
run: true,
urls: ['http://localhost:<%= connect.options.port %>/index.html']
}
}
},
// not used since Uglify task does concat,
// but still available if needed
/*concat: {
dist: {}
},*/
// not enabled since usemin task does concat and uglify
// check index.html to edit your build targets
// enable this task if you prefer defining your build targets here
uglify: {
options: { preserveComments: 'some' }
},
rev: {
dist: {
files: {
src: [
'<%= yeoman.dist %>/scripts/{,*/}*.js',
'<%= yeoman.dist %>/styles/{,*/}*.css',
'<%= yeoman.dist %>/images/{,*/}*.{png,jpg,jpeg,gif,webp}',
'<%= yeoman.dist %>/styles/fonts/*'
]
}
}
},
useminPrepare: {
options: {
dest: '<%= yeoman.dist %>'
},
html: '<%= yeoman.app %>/*.html'
},
usemin: {
options: {
dirs: ['<%= yeoman.dist %>']
},
html: ['<%= yeoman.dist %>/{,*/}*.html'],
css: ['<%= yeoman.dist %>/styles/{,*/}*.css']
},
// Put files not handled in other tasks here
copy: {
dist: {
files: [{
expand: true,
dot: true,
cwd: '<%= yeoman.app %>',
dest: '<%= yeoman.dist %>',
src: [
'*.{ico,png,txt}',
'.htaccess',
'images/{,*/}*.{webp,gif}',
'images/{,*/}*.{png,jpg,jpeg}',
'images/{,*/}*.svg',
'styles/{,*/}*.css',
// Anything to be compiled goes in scripts-src/. This
// directory is things that are already minified.
'scripts/{,*/}*.js',
'*.html',
'kdc.fcgi'
]
}, {
expand: true,
cwd: '.tmp/images',
dest: '<%= yeoman.dist %>/images',
src: [
'generated/*'
]
}]
}
}
});
grunt.registerTask('config', function() {
/*
grunt.file.write(yeomanConfig.app + '/scripts-src/config.js',
'"use strict"\n' +
'var CONFIG = ' + JSON.stringify(appConfig) + ';');
*/
var htaccess = grunt.file.read(yeomanConfig.app + '/htaccess-header');
htaccess += '\n';
for (var key in headers) {
htaccess += 'Header add ' + key + ' "' +
headers[key].replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
if (key == 'X-WebKit-CSP')
htaccess += ' env=!broken_safari';
htaccess += '\n';
}
htaccess += grunt.file.read(yeomanConfig.app + '/htaccess-footer');
grunt.file.write(yeomanConfig.app + '/.htaccess', htaccess);
});
grunt.registerTask('kdc-proxy', function() {
grunt.log.writeln('Spawning KDC proxy')
var kdc = child_process.spawn(
path.join(yeomanConfig.app, '../kdc/kdc.py'),
['127.0.0.1:' + String(KDC_PROXY_PORT)],
{env: process.env, stdio: 'inherit'});
kdc.on('exit', function(code) {
grunt.log.writeln('KDC proxy exitted with code ' + code);
});
process.on('exit', function() {
grunt.log.writeln('Killing KDC proxy')
kdc.kill();
});
// Sleep for 100ms for the proxy to be ready.
setTimeout(this.async(), 100);
});
grunt.registerTask('server', function (target) {
if (target === 'dist') {
return grunt.task.run(['build', 'open', 'kdc-proxy', 'connect:dist:keepalive']);
}
grunt.task.run([
'clean:server',
'config',
'kdc-proxy',
'connect:livereload',
'open',
'watch'
]);
});
grunt.registerTask('test', [
'clean:server',
'config',
'connect:test',
'mocha'
]);
grunt.registerTask('fix-perms', function() {
// Because grunt is lame.
fs.chmodSync(path.join(yeomanConfig.dist, 'kdc.fcgi'), '755');
});
grunt.registerTask('export-webathena', function() {
grunt.file.copy(path.join(yeomanConfig.dist, 'scripts/webathena.js'),
'dist/webathena.js');
});
grunt.registerTask('build', [
'clean:dist',
'config',
'useminPrepare',
'concat',
'uglify',
'copy:dist',
'fix-perms',
'export-webathena',
'rev',
'usemin'
]);
grunt.registerTask('default', [
'test',
'build'
]);
};