-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
81 lines (69 loc) · 2.46 KB
/
server.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
81
const express = require('express');
const bodyParser = require('body-parser');
const { execSync } = require('child_process');
const config = require('./config.json');
const Build = require('github-build');
const server = express();
server.use(bodyParser.json());
server.post('/docs/:id/:secret', (req, res) => {
if (config[req.params.id] !== req.params.secret)
return res.send({ status: 403, body: 'Forbidden.'});
if (req.headers['x-github-event'] !== 'push')
return res.send({ status: 204, body: 'Untracked event.'});
const branch = req.body.ref.match(/refs\/heads\/(.+)/)[1];
if (!(branch === 'master' || branch === 'stable'))
return res.send({ status: 204, body: 'Untracked branch.'});
if (req.body.before === req.body.after)
return res.send({ status: 204, body: 'No changes.'});
const data = {
repo: 'yamdbf/core',
sha: req.body.after,
token: config.token,
label: 'YAMDBF Docs Build',
description: 'Building docs...',
url: `https://yamdbf.js.org${branch === 'master' ? '/indev' : ''}`
}
const build = new Build(data);
build.start().then(() => {
try
{
let result;
let type = branch === 'master' ? 'indev' : 'stable';
let opts = { cwd: config[type] };
console.log(`Starting docs build as of yamdbf/${type}#${req.body.after}`);
execSync('git clean -df && git checkout .', opts);
execSync('git pull', opts);
try { execSync('rm -rf node_modules', opts); } catch (err) {}
execSync('yarn && gulp', opts)
// Attempt to build the localization string list before building docs,
// ignoring the attempt if anything bad happens
try { execSync('yarn localization', opts); } catch (err) {}
execSync(`yarn docs:${type}`, opts);
let gitStatus = execSync(`cd ../yamdbf-docs && git status`, opts).toString();
if (gitStatus.includes('nothing to commit'))
{
data.description = 'No docs changes.';
build.pass();
}
else
{
result = execSync(
`cd ../yamdbf-docs && git add --all && git commit -m "Build ${type} docs: ${req.body.after}" && git push`,
opts).toString();
console.log(result);
data.description = 'Successfully built docs.';
build.pass();
}
return res.send({ status: 200, body: 'Successfully built docs.'});
}
catch (err)
{
console.error(err);
data.description = 'Docs build failed.';
build.fail();
return res.send({ status: 500, body: 'Failed building docs.'});
}
})
.catch(console.error);
});
server.listen(config.port, () => console.log('Server started'));