-
Notifications
You must be signed in to change notification settings - Fork 0
/
configWatcher.js
43 lines (34 loc) · 1.12 KB
/
configWatcher.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 fs = require("fs");
const YAML = require("yaml");
// const isDev = process.env.NODE_ENV === "development";
console.log("Config Watcher Started");
// console.log(process.env.NODE_ENV);
const conf = YAML.parse(fs.readFileSync("config.yml").toString());
// Exit if api key isn't set
// Without it we can't call the revalidate route
if (!conf.api_key) {
console.error("API Key not set in config! Config file won't be watched.");
process.exit(1);
}
fs.unwatchFile("config.yml");
fs.watchFile("config.yml", (curr, prev) => {
if (+curr.mtime - +prev.mtime) {
console.log("config modified... revalidating home page");
// const http = isDev ? require("http") : require("https");
const http = require("http");
const req = http.get(
{
hostname: "127.0.0.1",
port: 3000,
path: `/api/revalidate?api_key=${encodeURIComponent(conf.api_key)}`
},
(res) => {
if (res.statusCode === 200) console.log("..Home page revalidated");
}
);
req.on("error", (err) => {
console.error("..Error attempting to revalidate home page: ", err);
});
req.end();
}
});