-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-webhook.js
55 lines (48 loc) · 1.5 KB
/
gh-webhook.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
const http = require("http");
const exec = require("child_process").exec;
const path = require("path");
const async = require("async");
const index = (process.argv[2].startsWith("-") ? 3 : 2)
const projectPath = process.argv[index];
const repos = process.argv.filter((arg, i) => { return i > index; })
const absolutePath = path.join(__dirname, projectPath);
const cmds = ["git fetch", "git reset --hard HEAD", "git merge '@{u}'", "yarn generate"].concat(repos);
const execCmds = cmds.map((cmd) => {
return function (callback) {
exec(`cd ${absolutePath} && ${cmd}`, { maxBuffer: 1024 * 600 }, (err, stdout, stderr) => {
if (err) return callback(err);
callback(null, `--- ${cmd} ---:\n stdout: ${stdout} \n stderr: ${stderr}\n`);
});
};
});
const updateProject = function (callback) {
async.series(
execCmds
, function (err, results) {
if (err) return callback(err);
return callback(null, results.join(""));
});
};
const doRun = (func) => {
console.log("An event has been detected on the listened port: starting execution...")
updateProject((e, result) => {
let response = "";
if (e) {
console.error(`exec error: ${e}`);
response += `exec error: ${e}`;
}
if (result) {
console.log(result);
response += `\n ${result}`;
}
func(response)
});
}
if (process.argv.includes("-t"))
doRun((r) => { })
else
http.createServer(function (req, res) {
res.writeHead(200, { "Content-Type": "text/plain" });
doRun((r) => res.end(r))
}).listen(1338);
console.log("Git-auto-pull is running");