generated from ModDota/TypeScriptAddonTemplate
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfsWatcher.ts
66 lines (58 loc) · 2 KB
/
fsWatcher.ts
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
import * as fs from "fs";
const path = require("path");
import { LocalizationCompiler } from "./game/resource/localizationCompiler";
import { LocalizationData } from "./game/resource/localizationInterfaces";
const watch = require("node-watch");
let completeData: { [path: string]: LocalizationData } = {};
let watcher = watch(["./game/resource/localization", "./game/resource/localizationCompiler.js"], { recursive: true });
watcher.on("change", (eventType?: "update" | "remove" | undefined, filePath?: string) => {
if (!filePath) return;
if (filePath.includes("localizationCompiler.js")) {
compiler = loadCompiler();
}
let match = /(.*[\/|\\](\w+)).js/g.exec(filePath);
if (eventType == "update" && filePath && match) {
const curpath = match[1];
const data = getDataFromFile(".\\" + curpath + ".js");
if (data) {
completeData[curpath] = data;
combineData();
}
} else if (eventType == "remove" && match) {
if (completeData.hasOwnProperty(match[1])) {
delete completeData[match[1]];
combineData();
}
}
});
// not really neccessarry:
watcher.on("error", (error: Error) => {
console.log("Something went wrong!");
console.log(error);
});
watcher.on("ready", () => {
console.log("Ready!");
});
let compiler = loadCompiler();
function getDataFromFile(filePath: string): LocalizationData | undefined {
if (!fs.existsSync(filePath)) {
return;
}
delete require.cache[require.resolve(filePath)];
let file = require(filePath);
if (file["GenerateLocalizationData"]) {
const localizationArr: LocalizationData = file["GenerateLocalizationData"]();
return localizationArr;
}
return;
}
function combineData() {
compiler.OnLocalizationDataChanged(completeData);
}
function loadCompiler(): LocalizationCompiler {
// Clear require cache
delete require.cache[require.resolve("./game/resource/localizationCompiler")];
// Require latest compiler version
const compilerClass: new () => LocalizationCompiler = require("./game/resource/localizationCompiler").LocalizationCompiler;
return new compilerClass();
}