This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cukestall.js
83 lines (73 loc) · 3.36 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
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
var BACKDOOR_NAME_REGEXP = /[^\/]+$/;
var DEFAULT_MOUNT_ENDPOINT = '/cukestall';
var fs = require('fs');
var path = require('path');
var express = require('express');
var browserify = require('browserify');
var Bundler = require('cucumber/bundler');
var DEFAULT_MOUNT_ENDPOINT = "/cukestall";
var CukeStall = {
runner: function runner(options) {
options = options || {};
options.backdoors = options.backdoors || {};
options.mountEndPoint = options.mountEndPoint || DEFAULT_MOUNT_ENDPOINT;
options.require = options.require || [];
options.modules = options.modules || [];
var bundler = Bundler();
var serveStatic = express.static(__dirname + '/public');
var serveCukeStall = function serveCukeStall(req, res, next) {
if (req.path == options.mountEndPoint + '/javascripts/cucumber.js') {
res.setHeader('Content-Type', 'application/javascript');
res.end(bundler.bundle());
} else if (req.path == options.mountEndPoint + '/javascripts/cukestall.js') {
res.setHeader('Content-Type', 'application/javascript');
var supportCodeBundle = browserify();
supportCodeBundle.prepend('(function(context) {\n');
if (process.env.DEBUG_LEVEL)
supportCodeBundle.append("context.cukestallRequire = require;\n");
supportCodeBundle.addEntry('lib/kite.js', {dirname: __dirname+"/node_modules/kite", target: "/node_modules/kite"});
supportCodeBundle.addEntry('lib/kite/driver/cukestall_driver.js', {dirname: __dirname+"/node_modules/kite", target: "/node_modules/kite/lib/kite/driver/cukestall_driver"});
options.modules.forEach(function(modulePath) {
normalizedPath = path.normalize(modulePath);
supportCodeBundle.addEntry(modulePath, {target: normalizedPath});
});
supportCodeBundle.append("context.supportCode = function () {\n");
options.require.forEach(function(requirePath) {
normalizedPath = path.normalize(requirePath);
supportCodeBundle.require(requirePath, {target: normalizedPath});
supportCodeBundle.append("require('"+normalizedPath+"').call(this);");
});
supportCodeBundle.append("};\n");
supportCodeBundle.append("})(window);");
var bundle = supportCodeBundle.bundle();
if (Object.keys(supportCodeBundle.errors).length > 0) {
res.send("Errors in bundle", 500);
res.end();
} else {
res.end(bundle);
}
} else if (req.url == options.mountEndPoint || req.url == options.mountEndPoint + '/') {
var features = [];
options.features.forEach(function (feature) {
features.push(fs.readFileSync(feature));
});
res.render(__dirname + '/views/index.ejs', {features: features, layout: 'layouts/application'});
} else if (req.url == options.mountEndPoint + '/blank') {
res.render(__dirname + '/views/blank.ejs', {layout: 'layouts/application'});
} else {
var backdoorName = BACKDOOR_NAME_REGEXP.exec(req.url);
if (backdoorName != null && options.backdoors[backdoorName]) {
options.backdoors[backdoorName](req, res, next);
} else {
next();
}
}
};
return function (req, res, next) {
serveStatic(req, res, function () {
serveCukeStall(req, res, next);
});
};
}
};
module.exports = CukeStall;