-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
42 lines (41 loc) · 1.4 KB
/
config.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
const { watchFile, readFileSync } = require("fs");
const CONFIG_FILE = "./config.json";
function resyncConfig() {
"use strict";
console.log("Syncing the config at ", new Date());
const content = readFileSync(CONFIG_FILE, {
encoding: "utf8",
});
let data;
try {
data = JSON.parse(content);
} catch (e) {
console.error("Config is not valid JSON, ", new Date());
return;
}
Object.assign(exports, data);
console.log("Config synced at ", new Date());
}
watchFile(CONFIG_FILE, {
interval: 1000,
persistent: false,
}, (current, previous) => {
"use strict";
if (current.birthtimeMs !== previous.birthtimeMs) {
console.log("Config birth date changed from ", previous.birthtime, " to ", current.birthtime);
}
if (current.atimeMs !== previous.atimeMs) {
console.log("Config access date changed from ", previous.atime, " to ", current.atime);
}
if (current.ctimeMs !== previous.ctimeMs) {
console.log("Config change date changed from ", previous.ctime, " to ", current.ctime);
}
if (current.size !== previous.size) {
console.log("Config size changed from ", previous.size, " to ", current.size);
}
if (current.mtimeMs !== previous.mtimeMs) {
console.log("Config modify date changed from ", previous.mtime, " to ", current.mtime);
resyncConfig();
}
});
resyncConfig();