-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest-handler.js
More file actions
48 lines (40 loc) · 1.3 KB
/
request-handler.js
File metadata and controls
48 lines (40 loc) · 1.3 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
const executeSequence = require("./command-sequence");
const fs = require("fs");
const repositories = loadRepositories();
createLogFolder("logs");
module.exports = (req, res) => {
let repository = null;
let repositoryName = null;
try {
repository = req.body.repository;
repositoryName = repository.name;
} catch (error) {
res.status(500).send("Parsing error: " + error.message);
return;
}
if (!(repositoryName in repositories)) {
res.status(404).send("Repository \"" + repositoryName + "\" not found.");
} else {
res.status(200).send("OK");
const command = repositories[repositoryName];
executeSequence(command.commands, command.cwd, (error, stdout) => {
writeLog(repositoryName, stdout + error);
});
}
};
function createLogFolder(folderName) {
if (fs.existsSync(folderName) === false) {
fs.mkdirSync(folderName);
}
}
function loadRepositories() {
const repositoriesJSON = fs.readFileSync("repositories.json");
if (repositoriesJSON === null) {
throw new Error("Can't read repositories.json.");
}
const repositories = JSON.parse(repositoriesJSON);
return repositories;
}
function writeLog(repositoryName, text) {
fs.writeFile(`logs/${repositoryName}.log`, text);
}