-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
388 lines (367 loc) · 10.4 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*global module:false*/
'use strict';
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
var config = {
appName: 'efp-seq-browser',
app: 'app',
dist: 'dist'
};
// Project configuration.
grunt.initConfig({
// Task configuration.
config: config,
watch: {
bower: {
files: ['bower.json'],
tasks: ['wiredep']
},
gruntfile: {
files: 'Gruntfile.js',
tasks: ['jshint']
},
js: {
files: ['app/scripts/{,*/}*.js'],
tasks: ['jshint'],
options: {
livereload: {
host: '0.0.0.0',
port: 8080
}
}
},
jstest: {
files: ['test/spec/{,*/}*.js'],
tasks: ['test:watch']
},
livereload: {
files: [
'index.html',
'lib/*.*',
'app/{,*/}*.html',
'app/images/{,*/}*',
'app/styles/{,*/}*.css',
'.tmp/styles/{,*/}*.css'
],
tasks: ['includes'],
options: {
livereload: {
host: '0.0.0.0',
port: 8081
}
}
}
},
// The actual grunt server settings
connect: {
cloud9: {
options: {
port: 8080,
open: true,
hostname: '0.0.0.0',
livereload: 8081,
middleware: function(connect) {
return [
connect.static('.tmp'),
connect().use('/lib', connect.static('lib')),
connect().use('/bower_components', connect.static('./bower_components')),
connect().use('/app', connect.static(config.app)),
connect().use('/assets', connect.static('./assets')),
connect().use('/', connect.static('./'))
];
}
}
},
dist: {
options: {
port: 9000,
open: true,
// Change this to '0.0.0.0' to access the server from outside
hostname: '0.0.0.0',
livereload: false,
middleware: function(connect) {
return [
connect.static('.tmp'),
connect().use('/lib', connect.static('lib')),
connect().use('/bower_components', connect.static('./bower_components')),
connect().use('/app', connect.static(config.app)),
connect().use('/assets', connect.static('./assets')),
connect().use('/', connect.static('./'))
];
}
}
},
livereload: {
options: {
port: 9000,
open: true,
hostname: 'localhost',
livereload: 8081,
middleware: function(connect) {
return [
connect.static('.tmp'),
connect().use('/lib', connect.static('lib')),
connect().use('/bower_components', connect.static('./bower_components')),
connect().use('/app', connect.static(config.app)),
connect().use('/assets', connect.static('./assets')),
connect().use('/', connect.static('./'))
];
}
}
},
},
// Empties folders to start fresh
clean: {
server: '.tmp',
dist: {
files: [{
dot: true,
src: ['.tmp', 'dist/*', '!dist/.git*']
}]
}
},
// Make sure code styles are up to par and there are no obvious mistakes
jshint: {
options: {
jshintrc: '.jshintrc',
reporter: require('jshint-stylish'),
reporterOutput: ''
},
all: [
'Gruntfile.js',
'lib/*.js',
'app/scripts/{,*/}*.js',
'!app/scripts/vendor/*',
'test/spec/{,*/}*.js'
]
},
includes: {
files: {
src: ['index.html', 'app/*.html'], // Source files
dest: '.tmp', // Destination directory
flatten: true,
cwd: '.',
options: {
silent: true,
}
}
},
// Add vendor prefixed styles
autoprefixer: {
options: {
browsers: ['last 1 version']
},
dist: {
files: [{
expand: true,
cwd: '.tmp/styles/',
src: '{,*/}*.css',
dest: '.tmp/styles/'
}]
}
},
// Automatically inject Bower components into the HTML file
// don't include any transient jQuery dependencies
wiredep: {
target: {
src: [
'index.html'
],
options: {
exclude: [ /jquery\.js$/, /jquery\.min\.js$/ ]
}
}
},
copy: {
libraries: {
expand: true,
cwd: 'app/vendor',
src: '**',
dest: '.tmp/sites/all/libraries/efp-seq-browser/'
}
},
inline: {
dist: {
src: ['app/main.html'],
dest: [ 'dist/' ],
options:{
cssmin: true,
uglify: true,
tag: ''
},
}
}
});
/*
* This task reads araport-app.json file and injects the necessary references
* in the specified files. Its functionality is based on wiredep's functionality.
* There could probably be a better solution out there, or maybe modify grunt-wiredep?
*/
/*jshint unused:false */
grunt.registerTask('araport-wiredep', '', function(){
console.log('Wiring dependencies from araport-app.json');
var $ = {
_: require('lodash'),
fs: require('fs'),
path: require('path'),
};
//TODO relative path of assets, scripts and styles should only be app/ without the {styles, assets, scripts}
var config = {
'src': 'index.html',
'appFile': 'araport-app.json',
'fileTypes':{
'js':{
'depprop': 'scripts',
'depcwd': 'app/',
'replaceStr': '<script src="%filePath%"></script>'
},
'css':{
'depprop': 'styles',
'depcwd': 'app/',
'replaceStr': '<link rel="stylesheet" href="%filePath%" />'
},
'html':{
'depprop': 'html',
'depcwd': '/app/',
'replaceStr': 'include "%filePath%"'
},
},
'block': /(([ \t]*)<!--\s*araport_dep:*(\S*)\s*-->)(\n|\r|.)*?(<!--\s*endaraport_dep\s*-->)/gi
};
var error;
//Check for the araport=app.json file and load it.
if(!$.fs.existsSync(config.appFile)){
error = new Error('Cannot find araport-app.json.');
error.code = 'ARAPORT_COMPONENTS_MISSING';
grunt.fail.warn(error);
}
var ajson = JSON.parse($.fs.readFileSync(config.appFile));
//Check for the index.html file and load it.
if(!$.fs.existsSync(config.src)){
error = new Error('Cannot find src file');
error.code = 'ARAPORT_COMPONENTS_MISSING';
grunt.fail.warn(error);
}
var shtml = String($.fs.readFileSync(config.src));
var filePath = config.src;
//var fileExt = $.path.extname(filePath).substr(1);
//Get the file types that are on the config json.
var fileTypes = [];
$._.forEach(config.fileTypes, function(n, key){
fileTypes.push(key);
});
//function to get the array of file names to wire.
var _getFileNames = function(fileType, config, ajson){
var depprop = config.fileTypes[fileType].depprop;
var ret = [];
var tmpRet = ajson[depprop];
if(tmpRet.constructor !== Array){
ret.push(tmpRet);
}else{
ret = tmpRet;
}
return ret;
};
//function to append the relative path within the app to the files to wire.
var _getRelPaths = function(fileType, config, ajson, fileNames){
var depcwd = config.fileTypes[fileType].depcwd;
var relPaths = [];
$._(fileNames).forEach(function(n){
relPaths.push(depcwd + n);
}).value();
return relPaths;
};
//Callback function when we find a block
var _constructDeps = function(relPaths, config, fileType){
var paths = relPaths;
var cfg = config;
var ft = fileType;
return function(match, startBlock, spacing, blockType, oldScripts, endBlock, offset, string){
if(blockType !== fileType){
return match;
}
var deps = '';
$._(paths).forEach(function(relPath){
deps += cfg.fileTypes[ft].replaceStr.replace('%filePath%', relPath) + spacing;
}).value();
return startBlock + '\n' + spacing + deps + '\n' + spacing + endBlock;
};
};
var newCont = shtml;
$._(fileTypes).forEach(function(n){
console.log('for filetype: ' + n);
var fileNames = _getFileNames(n, config, ajson);
var relPaths = _getRelPaths(n, config, ajson, fileNames);
newCont = newCont.replace(config.block, _constructDeps(relPaths, config, n));
}).value();
if (newCont !== shtml){
$.fs.writeFileSync(filePath, newCont);
}
return true;
});
/*
* Make sure that the bower_components dependency path exists
* even if we have no external dependencies, otherwise
* wiredep gets mad
*/
grunt.registerTask('checkdeps', function() {
var fs = require('fs');
if (! fs.existsSync('bower_components')) {
fs.mkdirSync('bower_components');
}
});
grunt.registerTask('serve', function (target) {
if (target === 'dist') {
return grunt.task.run([
'clean:server',
'jshint',
'checkdeps',
'araport-wiredep',
'wiredep',
'includes',
'copy',
'connect:dist:keepalive'
]);
}
else if (target === 'cloud9') {
return grunt.task.run([
'clean:server',
'jshint',
'checkdeps',
'araport-wiredep',
'wiredep',
'includes',
'copy',
'connect:cloud9',
'watch'
]);
}
grunt.task.run([
'clean:server',
'jshint',
'checkdeps',
'araport-wiredep',
'wiredep',
'includes',
'copy',
'connect:livereload',
'watch'
]);
});
grunt.registerTask('server', function (target) {
grunt.log.warn('The `server` task has been deprecated. Use `grunt serve` to start a server.');
grunt.task.run([target ? ('serve:' + target) : 'serve']);
});
grunt.registerTask('default', 'serve');
grunt.registerTask('test', [
'jshint'
]);
grunt.registerTask('dist', [
'clean:dist',
'jshint',
'checkdeps',
'araport-wiredep',
'wiredep',
'inline'
]);
};