This repository has been archived by the owner on May 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
114 lines (92 loc) · 2.39 KB
/
index.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
/*jshint node:true */
'use strict';
var gutil = require('gulp-util');
var c = gutil.colors;
var es = require('event-stream');
var extend = require('xtend');
var path = require('path');
var spawn = require('child_process').spawn;
var server = require('karma').server;
var karmaPlugin = function(options) {
var child;
var stream;
var files = [];
options = extend({
action: 'run'
}, options);
var action = options.action;
// Remove option in case Karma uses it in the future
delete options.action;
if (action === 'watch') {
// Never set singleRun in background mode
options.singleRun = false;
// Enable watching
options.autoWatch = true;
}
else if (action === 'run') {
// Tell Karma to run once and exit
options.singleRun = true;
// Disable watching
options.autoWatch = false;
}
if (options.configFile) {
options.configFile = path.resolve(options.configFile);
}
function done(code) {
// Stop the server if it's running
if (child) {
child.kill();
}
// End the stream if it exists
if (stream) {
if (code) {
stream.emit('error', new gutil.PluginError('gulp-karma', 'karma exited with code ' + code));
}
else {
stream.emit('end');
}
}
}
function startKarmaServer() {
gutil.log('Starting Karma server...');
// Start the server
child = spawn(
'node',
[
path.join(__dirname, 'lib', 'background.js'),
JSON.stringify(options)
],
{
stdio: 'inherit'
}
);
// Cleanup when the child process exits
child.on('exit', function(code) {
// gutil.log('Karma child process ended');
done(code);
});
}
function queueFile(file) {
if (file) {
// gutil.log('Queueing file '+file.path);
files.push(file.path);
}
else {
stream.emit('error', new Error('Got undefined file'));
}
}
function endStream() {
// Override files if they were piped
// This doesn't work with the runner, but works fine with singleRun and autoWatch
if (files.length) {
options.files = files;
}
// Start the server
// If options.singleRun: Server starts, tests run, task completes
// If options.background: Server starts, tests run, files watched
startKarmaServer();
}
stream = es.through(queueFile, endStream);
return stream;
};
module.exports = karmaPlugin;