-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (45 loc) · 1.22 KB
/
index.js
File metadata and controls
49 lines (45 loc) · 1.22 KB
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
var exec = require('child_process').exec;
var path = require('path');
module.exports = function (options) {
if(options.gitRoot === undefined) {
throw new Error('missing option gitRoot');
}
else if(options.baseRoute === undefined) {
throw new Error('missing option baseRoute');
}
else if(options.basePath === undefined) {
throw new Error('missing option basePath');
}
var baseRoute = options.baseRoute,
gitRoot = options.gitRoot,
basePath = options.basePath,
relative,
cmd;
return function (req, res, next) {
var matchesRoute = baseRoute === '' || req.path.indexOf(baseRoute) === 0
if(matchesRoute && ~~req.query.diff) {
relative = req.path.substring(baseRoute.length);
cmd = [
__dirname + '/bin/diffserv-cli/src/base.py',
path.join(process.cwd(), gitRoot),
basePath + relative,
req.query.callback ? req.query.callback : '',
req.query.version ? req.query.version : '',
req.query.next ? req.query.next : ''
].join(' ');
console.log('exec: ' + cmd);
exec(cmd, function (err, stdout, stderr) {
if(err) {
next(err);
}
else {
res.set('Content-Type','application/javascript');
res.send(200, stdout.toString());
}
});
}
else {
next();
}
}
};