forked from traverson/traverson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
211 lines (194 loc) · 5.95 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
'use strict';
var request = require('request')
, testServerRootUri = 'http://127.0.0.1:2808'
, testServerStatusUri = testServerRootUri + '/status'
, testServerKillUri = testServerRootUri + '/quit'
, mochaPhantomJsTestRunner = testServerRootUri +
'/static/browser/test/index.html'
, serverWasAlreadyRunning = false;
/* jshint -W106 */
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jshint: {
files: [
'**/*.js',
'Gruntfile.js',
'.jshintrc',
'!node_modules/**/*',
'!browser/dist/**/*',
'!browser/example/assets/**/*',
'!browser/lib/third-party/**/*',
'!browser/lib/shim/underscore-string-reduced.js',
'!browser/test/browserified_tests.js',
],
options: {
jshintrc: '.jshintrc'
}
},
// run the mocha tests via Node.js
mochaTest: {
test: {
options: {
reporter: 'spec',
slow: 300,
timeout: 1000
},
src: ['test/**/*.js']
}
},
// remove all previous browserified builds
clean: {
dist: ['./browser/dist/**/*.js'],
tests: ['./browser/test/browserified_tests.js']
},
// browserify everything
browserify: {
// This browserify build can be used by users of the module. It contains a
// UMD (universal module definition) and can be used via an AMD module
// loader like RequireJS or by simply placing a script tag in the page,
// which registers the module as a global var. Look at the example in
// in browser/example/index.html.
standalone: {
src: [ '<%= pkg.name %>.js' ],
dest: './browser/dist/<%= pkg.name %>.js',
options: {
browserifyOptions: {
standalone: '<%= pkg.name %>'
}
}
},
// With this browserify build, Traverson can be required by other
// browserify modules that have been created with an --external parameter.
// See browser/test/index.html for an example.
external: {
src: [ '<%= pkg.name %>.js' ],
dest: './browser/dist/<%= pkg.name %>.external.js',
options: {
require: ['./traverson.js']
}
},
// Browserify the tests
tests: {
src: [ 'test/browser_suite.js' ],
dest: './browser/test/browserified_tests.js',
options: {
// use the external bundle created above, do not bundle traverson
// into the tests
external: [ './traverson.js' ],
browserifyOptions: {
// Embed source map for tests
debug: true
},
plugin: [
'proxyquire-universal'
],
}
}
},
// Uglify browser libs
uglify: {
dist: {
files: {
'browser/dist/<%= pkg.name %>.min.js':
['<%= browserify.standalone.dest %>'],
'browser/dist/<%= pkg.name %>.external.min.js':
['<%= browserify.external.dest %>']
}
}
},
// run the mocha tests in PhantomJS
mocha: {
test: {
options: {
urls: [ mochaPhantomJsTestRunner ],
timeout: 20000,
reporter: 'spec',
}
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['dev']
},
});
// load all grunt-tasks
require('load-grunt-tasks')(grunt);
grunt.registerTask('start-test-server', 'Start the test server.',
function() {
var done = this.async();
function pingTestServer(callback) {
request.get(testServerRootUri, function(error, response) {
if (error) {
callback(error);
} else if (response.statusCode === 200) {
callback();
} else {
callback(new Error('HTTP status code was not 200 (as expected), ' +
'but ' + response.statusCode));
}
});
}
grunt.log.writeln('Starting test server from grunt.');
pingTestServer(function(error) {
// Only start a test server instance if none is running. Rationale:
// If an instance is running via supervisor while watching changed files,
// we do not need to (and can not due to port conflicts) start a second
// instance.
if (error) {
if (error.message !== 'connect ECONNREFUSED') {
grunt.log.writeln('(Message from ping was: ' + error.message + ')');
}
grunt.log.writeln('It seems the test server is currently not ' +
'running, will start a new instance to run mocha-phantomjs tests.');
require('./bin/start-test-server');
done();
} else {
serverWasAlreadyRunning = true;
grunt.log.writeln('Test server is already running.');
done();
}
});
});
grunt.registerTask('stop-test-server', 'Stops the test server.',
function() {
var done = this.async();
if (serverWasAlreadyRunning) {
grunt.log.writeln('Server was already running when Grunt build started,' +
' thus it will not be shut down now from Grunt.');
return done();
} else {
grunt.log.writeln('Stopping test server from grunt.');
}
request.get(testServerKillUri, function(error, response) {
if (error) {
if (error.message !== 'connect ECONNREFUSED') {
grunt.log.writeln('(Message from stop request was: ' + error.message +
')');
}
grunt.log.writeln('It seems the test server is not running at all, ' +
'doing nothing');
return done();
} else {
grunt.log.writeln('Shutdown request has been send to test server, ' +
'test server should have been shut down.');
grunt.log.writeln('');
return done();
}
});
});
grunt.registerTask('dev', [
'jshint',
'mochaTest',
]);
grunt.registerTask('default', [
'dev',
'clean',
'browserify',
'uglify',
'start-test-server',
'mocha',
'stop-test-server',
]);
};
/* jshint +W106 */