forked from lazd/gulp-karma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (103 loc) · 2.86 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
115
116
117
118
119
120
121
122
123
124
125
/*jshint node:true */
'use strict';
var gutil = require('gulp-util');
var es = require('event-stream');
var extend = require('xtend');
var fs = require('fs');
var path = require('path');
var spawn = require('child_process').spawn;
var tmp = require('tmp');
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...');
// Run Karma from the same instance of node that was used to start the process
var nodePath = process.execPath;
// Write to temp file to avoid ENAMETOOLONG error with large `options` input
tmp.setGracefulCleanup();
tmp.file(function _tempFileCreated(err, tmpPath, fd, cleanupCallback) {
if (err) throw err;
fs.writeFile(tmpPath, JSON.stringify(options), function (err) {
if (err) throw err;
});
// Start the server
child = spawn(
nodePath,
[
path.join(__dirname, 'lib', 'background.js'),
tmpPath
],
{
stdio: 'inherit'
}
);
// Cleanup when the child process exits
child.on('exit', function(code) {
// gutil.log('Karma child process ended');
cleanupCallback();
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;