-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
executable file
·70 lines (60 loc) · 1.58 KB
/
main.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
const { execSync } = require('child_process')
const express = require('express')
const bodyParser = require('body-parser')
async function fetch_and_update() {
const BRANCH='master'
const mirror_script = `
set -x
git fetch origin ${BRANCH}
git reset --hard HEAD
git checkout origin/${BRANCH}
rm -rf _build
sphinx-build -b html -d _build/doctrees . _build/html
`
try {
await execSync(mirror_script, {stdio: 'inherit'})
} catch (err) {
console.error(err.toString())
}
}
if (process.argv.length != 3) {
console.error('Bad args.')
process.exit()
}
const option = process.argv.pop()
if (option === 'serve') {
/* setup webhook httpd */
const app = express()
app.use(bodyParser.json())
app.use('/', express.static('./_build/html'))
app.listen(8080)
console.log('httpd ready (at port 8080).')
process.on('SIGINT', function() {
console.log('')
console.log('Bye bye.')
process.exit()
})
/* route root to default TOC page */
app.get('/', function(req, res) {
res.status(404).redirect('content/en/index.html')
})
app.post('/webhook', async function (req, res) {
const data = req.body
const secret = process.env['WEBHOOKSECRET']
console.log('[ request from ]', data.repository)
if (data.secret === secret) {
fetch_and_update()
res.send('OK')
} else {
res.send('Wrong secret')
}
})
} else if (option === 'update') {
/* run one-time script to manually update content */
(async function () {
await fetch_and_update()
process.exit()
})()
} else {
console.error('Invalid arg option.')
}