-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatch.js
43 lines (38 loc) · 1.18 KB
/
watch.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
const express = require('express');
const app = express()
const chokidar = require('chokidar');
const childProcess = require("child_process");
const watcher = chokidar.watch('localLinks', {ignored: /^\./, persistent: true});
let building = false
let triggeredDuringBuild = false
const rebuild = (path) => {
console.log(`File ${path} has been changed, rebuilding site...`)
if (building) {
console.log("Triggered during build, waiting for build to finish...")
triggeredDuringBuild = true
return
}
triggeredDuringBuild = false
building = true;
const process = childProcess.spawn("npx", ["antora", "playbook-dev.yaml"], {stdio: 'inherit'})
process.on("exit", (code) => {
if (code === 0) {
console.log("Site rebuilt successfully!")
} else {
console.error("Failed to rebuild site!")
}
building = false
if (triggeredDuringBuild) {
rebuild()
}
})
}
watcher
.on('change', rebuild)
.on('unlink', rebuild)
.on('error', rebuild)
app.use(express.static('build/site'))
app.listen(3000, () => {
console.log(`Started serving files on port 3000!`)
})
rebuild("none")