-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodebuildserver.js
80 lines (68 loc) · 2.39 KB
/
nodebuildserver.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
var spawn = require('child_process').spawn,
tmp = require("tmp"),
_ = require("underscore"),
fs = require("fs"),
async = require("async"),
express = require('express'),
app = express();
var run_process = function (cmd,args,opts,callback) {
if (_.isFunction(opts)) {
callback = opts;
}
console.log("running: " + cmd);
var process = spawn(cmd, args,opts||{}),
process_stdout = "",
process_stderr = "",
replied = false;
process.on("error", function (err) {
replied = true;
callback("Error: " + err, process_stdout, process_stderr);
});
var finished_handler = function (code, signal) {
if (replied) { return true; }
if (signal) {
callback("Killed by signal: " + signal, process_stdout, process_stderr);
} else {
console.log("Got: " + code);
if (code === 0) {
callback(null,process_stdout,process_stderr);
} else {
callback("Error: Process exited with : " + code,process_stdout,process_stderr);
}
}
}
process.on("close", finished_handler);
process.stdout.on("data", function (data) {
process_stdout += data;
});
process.stderr.on("data", function (data) {
process_stderr += data;
});
}
app.get('/', function (req, res) {
res.end("<html><h1>Hello Clive</h1></html>");
});
app.get('/build/:app', function(req, res){
var app = req.params.app,
url = "git@git.rorotika:" + app;
tmp.dir(function (err, tmp_dir) {
tmp.tmpName(function (err,tar) {
async.series([
run_process.bind(null,"git",["clone",url,tmp_dir]),
run_process.bind(null,"npm",["install"],{cwd:tmp_dir}),
run_process.bind(null,"/bin/bash",["-c","tar czf " + tar + " *"],{cwd:tmp_dir})
], function (err, outs) {
if (err) {
res.writeHead(500,{"Content-type":"text/html"});
res.end("Error: " + err + ":\n" + _.pluck(outs,"1").join("\n"));
} else {
res.writeHead(200,{"Content-type":"application/gzip"});
fs.createReadStream(tar).pipe(res);
}
});
});
});
});
var port = process.env.PORT || 3000;
console.log('listening on', port);
app.listen(port);