forked from jbpros/cukestall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cukestall.js
58 lines (51 loc) · 2.18 KB
/
cukestall.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
var BACKDOOR_NAME_REGEXP = /[^\/]+$/;
var DEFAULT_MOUNT_ENDPOINT = '/cukestall';
var fs = require('fs');
var express = require('express');
var CukeStall = {
runner: function cukes(options) {
options = options || {};
var featurePaths = options.featurePaths;
var supportCodePaths = options.supportCodePaths;
var stepDefsPaths = options.stepDefsPaths;
var backdoors = options.backdoors || {};
var mountEndpoint = options.mountEndpoint || DEFAULT_MOUNT_ENDPOINT;
var serveStatic = express.static(__dirname + '/public')
return function (req, res, next) {
var serveCukeStall = function serveCukeStall() {
if (req.url == mountEndpoint || req.url == mountEndpoint + '/') {
var features = [];
featurePaths.forEach(function (featurePath) {
features.push(fs.readFileSync(featurePath));
});
res.render(__dirname + '/views/index.ejs', {features: features, layout: 'layouts/application'});
} else if (req.url == mountEndpoint + '/blank') {
res.render(__dirname + '/views/blank.ejs', {layout: 'layouts/application'});
} else if (req.url == mountEndpoint + '/javascripts/stepdefs.js') {
res.setHeader('Content-Type', 'application/javascript');
res.write("window.supportCode = function () {\n");
supportCodePaths.forEach(function (supportCodePath) {
var supportCode = require(supportCodePath);
res.write('(' + supportCode.toString() + ').apply(this);\n');
});
res.write("\n};\n");
res.write("window.stepDefs = function () {\n");
stepDefsPaths.forEach(function (stepDefsPath) {
var stepDefs = require(stepDefsPath);
res.write('(' + stepDefs.toString() + ').apply(this);\n');
});
res.end("\n};\n");
} else {
var backdoorName = BACKDOOR_NAME_REGEXP.exec(req.url);
if (backdoorName != null && backdoors[backdoorName]) {
backdoors[backdoorName](req, res, next);
} else {
next();
}
}
};
serveStatic(req, res, serveCukeStall);
};
}
};
module.exports = CukeStall;