cc-haxe/buildServer/index.js

106 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2023-08-09 13:22:12 +00:00
const express = require('express');
const childProcess = require('child_process');
const fs = require('fs').promises;
const path = require('path');
const async = require('async');
const bodyParser = require('body-parser');
const workingDir = process.env.REPO_DIR ?? "repo";
const buildQueue = async.queue(async (task) => {
return build(task);
});
const app = express();
app.use(bodyParser.json());
app.post('/build', async (req, res) => {
try{
let output = await buildQueue.push(req.body);
res.status(200);
res.send(output);
}catch(e){
res.status(500);
res.send(e);
}
});
app.post('/pull', async (req, res) => {
try{
await pull();
res.status(200);
res.send("Pulled");
}catch(e){
res.status(500);
res.send(e);
}
});
main();
async function build(body){
return await make();
}
async function main() {
await setup();
app.listen(3000, () => console.log('Server running on port 3000'));
process.on('SIGINT', async () => {
console.log("Shutting down");
process.exit(0);
});
}
async function pull(){
await runCommand("git pull");
}
async function make(makeCommand = "make build"){
try{
await runCommand(makeCommand);
let build = await fs.readFile(path.join(workingDir,"build/bundle.min.lua"), "utf8");
return build;
}catch(e){
console.log("Build failed");
}finally{
await runCommand("make clean");
}
}
async function setup() {
try {
await fs.mkdir(workingDir);
} catch (e) {
if (e.code !== "EEXIST") {
throw e;
}
}
if ((await fs.readdir(workingDir)).length !== 0) {
return;
}
console.log("Setting up repo");
await runCommand("git clone https://git.kapelle.org/niklas/cc-haxe.git .");
await runCommand("make deps");
}
function runCommand(cmd) {
return new Promise((resolve, reject) => {
childProcess.exec(cmd,{
cwd: workingDir,
}, (err, stdout, stderr) => {
if (err) {
reject(err);
}
console.log(stdout);
console.log(stderr);
resolve();
});
});
}