-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
199 lines (195 loc) · 8.17 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
module.exports = function(grunt) {
var globals = require('./tests/var/globals'),
task = grunt.cli.tasks[0] || 'build',
site = grunt.option('site') || "'No Tests Selected'",
browser = grunt.option('browser') || 0,
functionalTest = grunt.option('functionalTest') || 'all',
tests = require('./tests/testModules')(functionalTest),
testCmd = 'grunt test --site=' + site + ' --functionalTest=' + functionalTest + ' --browser=',
pkg = grunt.file.readJSON('package.json'),
environments,
execTask = {};
try {
// Try to grab the outer-level environments.json
environments = require('../tests/environments.json');
} catch (e) {
// Default to local environments.json if not found
environments = require('./tests/environments.json');
}
// Set global variable process.env.site for access within the config
if (globals.get('isGruntTask') === undefined && (task === 'test' || task === 'testAllBrowsers')) {
console.log('Globals set');
globals.set('site', site);
globals.set('isGruntTask', true);
globals.set('functionalTest', functionalTest);
console.log('Initializing test "' + functionalTest + '"');
}
if (task === 'test') {
globals.set('browser', browser);
}
// Error out if the expected unit and functional tests aren't found
if (
task !== 'build' && task !== 'installation' && !tests[site] &&
(Array.isArray(tests[site].unit) && Array.isArray(tests[site].functional)) === false
) {
throw Error('Missing tests. Check to see if ' + site + ' exists in /tests/testModules.js.');
}
// Build the exec task (used for running tests in all browsers):
for (var i in environments) {
execTask["test" + i] = {
cmd: testCmd + i
};
}
grunt.initConfig({
pkg: pkg,
intern: {
tests: {
options: {
runType: 'runner', // defaults to 'client'
config: 'tests/intern',
reporters: ['Console', 'Lcov'],
suites: tests[site] && tests[site].unit,
functionalSuites: tests[site] && tests[site].functional
}
}
},
exec: execTask,
requirejs: {
compile: {
options: {
baseUrl: "lib/js/",
name: "index",
out: "assets/js/automated-tests.min.js",
//-----------------------------------------
// We have multiple minify steps
optimize: "uglify",
uglify: {
mangle: false
},
// Include dependencies loaded with require
findNestedDependencies: true,
// Avoid inserting define() placeholder
skipModuleInsertion: true,
// Avoid breaking semicolons inserted by r.js
skipSemiColonInsertion: true,
wrap: {
start: "/**!\n * <%= pkg.name %> v<%= pkg.version %>\n * Copyright (c) " + (new Date()).getFullYear() + " Jonathan Perez.\n * Released under the MIT license\n */\n(function() {\n \"use strict\";\n",
end: "}());"
},
paths: {
/**
* This here is used so that I can still list angular as a dependency
* within my /lib/js/var/app.js file. It prevents the angular.min.js
* file from being concatenated with my custom JS.
*/
'angular': 'empty:', // <-- the colon (:) is required.
'ngRoute': 'empty:'
},
rawText: {},
/**
* This here strips out all define() wrapper stuff from my modules. I like async modules, but I
* don't *actually* want to use RequireJS. This lets me do both! Yay!
*
* I actually stole this from jQuery (they do their modules like this, too); this here's
* my attribution to them: https://github.com/jquery/jquery/blob/master/build/tasks/build.js
*/
onBuildWrite: convert
}
}
},
copy: {
main: {
files: [
// Files in /lib
{
expand: true,
cwd: 'lib/templates/',
src: ['**'],
dest: 'assets/templates/'
},
{
expand: true,
cwd: 'lib/views/',
src: ['**'],
dest: 'assets/views/'
},
{
expand: true,
cwd: 'lib/css/',
src: ['**'],
dest: 'assets/css/'
}
]
},
installation: {
files: [
{
expand: true,
cwd: 'tests/',
src: ['**'],
dest: '../../tests'
}
],
options: {
process: function(content, srcpath) {
return content
.replace(/\/\/~~REMOVE_START~~(.|\r\n)*?\/\/~~REMOVE_END~~/g, '');
}
}
}
}
});
grunt.loadNpmTasks('intern');
grunt.loadNpmTasks('grunt-exec');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask('build', ['requirejs', 'copy:main']);
grunt.registerTask('test', ['intern']);
grunt.registerTask('testAllBrowsers', ['exec']);
grunt.registerTask('installation', ['build', 'copy:installation']);
grunt.registerTask('default', ['build']);
/**
* Taken from: https://github.com/jquery/jquery/blob/master/build/tasks/build.js
*
* Strip all definitions generated by requirejs
* Convert "var" modules to var declarations
* "var module" means the module only contains a return
* statement that should be converted to a var declaration
* This is indicated by including the file in any "var" folder
* @param {String} name
* @param {String} path
* @param {String} contents The contents to be written (including their AMD wrappers)
*/
function convert(name, path, contents) {
var rdefineEnd = /\}\s*?\);[^}\w]*$/;
// Convert var modules
if (/.\/var\//.test(path)) {
contents = contents
.replace(/define\([\w\W]*?return/, " var " + (/var\/([\w-]+)/.exec(name)[1]) + " =")
.replace(rdefineEnd, "")
.replace(/\/\*\*/, " \/**")
.replace(/\s\*\s/g, " * ")
.replace(/\s\*\//g, " */");
} else {
contents = contents
.replace(/\s*return\s+[^\}]+(\}\s*?\);[^\w\}]*)$/, "$1")
// Multiple exports
.replace(/\s*exports\.\w+\s*=\s*\w+;/g, "");
// Remove define wrappers, closure ends, and empty declarations
contents = contents
.replace(/define\([^{]*?{/, "")
.replace(rdefineEnd, "");
// Remove anything wrapped with
// /* ExcludeStart */ /* ExcludeEnd */
// or a single line directly after a // BuildExclude comment
contents = contents
.replace(/\/\*\s*ExcludeStart\s*\*\/[\w\W]*?\/\*\s*ExcludeEnd\s*\*\//ig, "")
.replace(/\/\/\s*BuildExclude\n\r?[\w\W]*?\n\r?/ig, "");
// Remove empty definitions
contents = contents
.replace(/define\(\[[^\]]*\]\)[\W\n]+$/, "")
.replace(/@VERSION/, pkg.version);
}
return contents;
}
};