-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (201 loc) · 7.25 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
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
212
213
214
215
216
var fs = require('fs');
var gulp = require('gulp');
var connect = require('gulp-connect');
var jsonServer = require('gulp-json-srv');
var urlHandler = require('url');
var proxy = require('proxy-middleware');
var _ = require('underscore');
var spawn = require('spawn-cmd').spawn;
var ls = require('list-directory-contents');
var copyDir = require('copy-dir');
var settings = require('../../settings.json');
if (!settings) {
throw "could not find any settings.json in root. Please supply one";
}
settings.config = settings.config || {};
settings.path = settings.path || {};
var backenderApp = function () {
copyDir.sync('./node_modules/backender/report-x', './report');
function ensureExists(path, mask, cb) {
if (typeof mask == 'function') { // allow the `mask` parameter to be optional
cb = mask;
mask = 0777;
}
fs.mkdir(path, mask, function (err) {
if (err) {
if (err.code == 'EEXIST') cb(null); // ignore the error if the folder already exists
else cb(err); // something else went wrong
} else cb(null); // successfully created folder
});
}
var reportPath = './report/dalek';
ensureExists(reportPath, 0744, function (err) {
if (err) {
console.log("REPORT FOLDER CREATION ERROR : - ");
console.log(err);
}
});
var isRootMapEntry = function (o) {
return o && (o.route == '/');
};
var serve = function (maps, port, livereload, middleware) {
var root = maps.filter(isRootMapEntry)[0];
connect.server({
root: root && root.path,
livereload: livereload,
port: port,
middleware: middleware
});
};
var createProxyOptions = function (url, route) {
var options = urlHandler.parse(url);
options.route = route;
return options;
};
var mapAndFilterConfig = function (d, path, ext) {
ext = ext || 'json';
return d.map(function (o) {
var name = "";
var data = {};
if (typeof o == 'string') {
if (o.split('.').pop() == ext) {
name = o;
console.log("provided path : " + __dirname + path);
data = require('../..' + ((path && path != '/') ? path : '/') + '/' + o);
} else {
data = false;
}
} else {
if (ext == "json") {
data = o;
}
}
return data;
}).filter(function (o) {
return o;
});
};
var configs = mapAndFilterConfig(settings.config, settings.path);
var setupMiddleware = function (port, maps, datasources, datasourcepath, apiverbose) {
datasources = Array.isArray(datasources) ? datasources : [datasources];
var datasource = _.reduce(mapAndFilterConfig(datasources, datasourcepath, 'json'), function (a, b) {
return _.extend(a, b);
}, {});
var customRoutes = _.reduce(mapAndFilterConfig(datasources, datasourcepath, 'js'), function (a, b) {
return _.extend(a, b);
}, {});
return function (connect, o) {
console.log(customRoutes);
console.log("sppppppp");
if (datasource && customRoutes) {
jsonServer.start({
port: port,
data: datasource,
customRoutes: customRoutes
});
} else {
datasource && jsonServer.start({
port: port,
data: datasource
});
}
return maps.map(function (o) {
if ((o.endpoint || o.data) && !isRootMapEntry(o)) {
return o.data ? proxy(createProxyOptions('http://localhost:' + port + "/" + o.data, o.route)) : proxy(createProxyOptions(o.endpoint, o.route));
}
}).filter(function (o) {
return o;
});
};
};
var loads = [];
_.each(configs, function (config) {
config.localport = config.localport || 8800;
config.livereload = config.livereload || false;
config.testdir = config.testdir || './tests';
config.stepdir = config.stepdir || "./steps";
config.useheadless = config.useheadless || false;
config.routes = config.routes || [{
"route": "/ping",
"data": "ping"
}
];
config.datasource = config.datasource || [{
"ping": ["pong"]
}
];
config.apiport = config.apiport || config.localport + 1;
var middleWare = setupMiddleware(config.apiport, config.routes, config.datasource, config.datasourcepath, config.apiverbose);
middleWare = middleWare || function (req, res, next) {
next();
};
var load = {
routes: config.routes,
localport: config.localport,
livereload: config.livereload,
middleWare: middleWare,
testdir: config.testdir,
stepdir: config.stepdir,
useheadless: config.useheadless
};
loads.push(load);
config.ref && gulp.task(config.ref, function () {
serve(load.routes, load.localport, load.livereload, load.middleWare);
});
});
var doruntest = function (testdir, useheadless) {
ls(testdir, function (err, tree) {
tree.map(function (o) {
spawn("dalek " + o + (useheadless ? "" : " -b chrome ") + " -r console,html", [], {
stdio: 'inherit'
}).on('exit', function () { });
});
});
};
var hasInstalledModule = function (name) {
console.log('checking for ' + name + ' installation....');
try {
console.log(require.resolve(name));
return true;
} catch (e) {
console.error(name + " is not found");
process.exit(e.code);
}
};
var runtest = function (testdir, useheadless) {
var installCli = function (f) {
spawn("npm install ", ['dalek-cli ', '--save-dev'], {
stdio: 'inherit'
}).on('exit', function () {
f && f();
});
};
var installChrome = function () {
spawn("npm install ", ['dalek-browser-chrome ', '--save-dev'], {
stdio: 'inherit'
}).on('exit', function () {
doruntest(testdir, useheadless);
});
};
var installReady = hasInstalledModule('dalek-cli') && hasInstalledModule('dalek-browser-chrome');
installReady ? doruntest(testdir, useheadless) : installCli(installChrome);
};
var runServer = function () {
_.each(loads, function (load) {
serve(load.routes, load.localport, load.livereload, load.middleWare);
});
};
var runTests = function () {
runServer();
settings.testdir = settings.testdir || "tests";
settings.testdir && runtest(settings.testdir, settings.useheadless);
};
return {
runServer: runServer,
runTests: runTests
};
};
module.exports = {
init: backenderApp
};