-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathgruntfile.js
190 lines (148 loc) · 4.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
module.exports = function (grunt) {
var istanbul = require('istanbul');
// grunt plugins
require('load-grunt-tasks')(grunt);
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
mocha: {
test: {
src: 'test/index.html',
options: {
reporter: 'Spec',
run: false,
log: true
}
}
},
test: {
options: {
template: 'test/index.template.html',
runner: 'test/index.html',
files: 'test/spec/**/*'
}
},
coverage: {
// when the coverage object is received
// from grunt-mocha it will be saved here
coverage: null,
instrument: {
// files to NOT instrument eg. libs
// these files will be copied "as is"
ignore: [
'src/js/lib/**/*'
],
// files to instrument
files: [
{
src: 'dragend.js',
expand: true,
cwd: 'dist',
dest: 'test/src'
}
]
},
// task for generating reports
report: {
reports: ['html', 'text-summary'],
dest: 'test/reports'
}
},
clean: {
dist: ['dist'],
tests: [
'test/src',
'test/reports',
'test/index.html'
]
},
jshint: {
src: [
'dragend.js'
],
tests: [
'test/spec'
],
grunt: [
'gruntfile.js'
]
},
uglify: {
main: {
files: {
'dist/dragend.min.js': ['dist/dragend.js']
}
}
},
copy: {
index: {
options: {
processContent: function (content, srcpath) {
return grunt.template.process(content);
}
},
src: 'dragend.js',
dest: 'dist/dragend.js'
}
}
});
grunt.event.on('coverage', function (coverage) {
grunt.config('coverage.coverage', coverage);
});
grunt.registerMultiTask('coverage', 'Generates coverage reports for JS using Istanbul', function () {
if (this.target === 'instrument') {
var ignore = this.data.ignore || [];
var instrumenter = new istanbul.Instrumenter();
this.files.forEach(function (file) {
var src = file.src[0],
instrumented = grunt.file.read(src);
// only instrument this file if it is not in ignored list
if (!grunt.file.isMatch(ignore, src)) {
instrumented = instrumenter.instrumentSync(instrumented, src);
}
// write
grunt.file.write(file.dest, instrumented);
});
return;
}
if (this.target === 'report') {
this.requiresConfig('coverage.coverage');
var Report = istanbul.Report;
var Collector = istanbul.Collector;
var reporters = this.data.reports;
var dest = this.data.dest;
var collector = new Collector();
// fetch the coverage object we saved earlier
collector.add(grunt.config('coverage.coverage'));
reporters.forEach(function (reporter) {
Report.create(reporter, {
dir: dest + '/' + reporter
}).writeReport(collector, true);
});
return;
}
grunt.warn('Unknown target - valid targets are "instrument" and "report"');
});
grunt.registerTask('test', 'Run JS Unit tests', function () {
var options = this.options();
var tests = grunt.file.expand(options.files).map(function(file) {
return '../' + file;
});
// build the template
var template = grunt.file.read(options.template)
.replace('{{ tests }}', JSON.stringify(tests));
// write template to tests directory and run tests
grunt.file.write(options.runner, template);
grunt.task.run('coverage:instrument', 'mocha', 'coverage:report');
});
grunt.registerTask('build', 'build release versions', function () {
grunt.task.run([
'clean',
'jshint:grunt',
'jshint:src',
'copy',
'uglify',
'jshint:tests',
'test'
]);
});
};