diff --git a/package.json b/package.json index 3c89718b..b5ca4fbd 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "start": "node --enable-source-maps --import ./build/src/pathman.js build/src/index.js", "build": "tsc --incremental --sourceMap && ncp static/webui build/static/webui", + "build:dev": "tsc --incremental --sourceMap", "build-and-start": "npm run build && npm run start", "dev": "node scripts/dev.js", "verify": "tsgo --noEmit", diff --git a/scripts/dev.js b/scripts/dev.js index da3a0477..1852a131 100644 --- a/scripts/dev.js +++ b/scripts/dev.js @@ -3,7 +3,15 @@ const { spawn } = require("child_process"); const chokidar = require("chokidar"); const kill = require("tree-kill"); +let secret = ""; +for (let i = 0; i != 10; ++i) { + secret += String.fromCharCode(Math.floor(Math.random() * 26) + 0x41); +} + const args = [...process.argv].splice(2); +args.push("--dev"); +args.push("--secret"); +args.push(secret); let buildproc, runproc; function run(changedFile) { @@ -20,11 +28,11 @@ function run(changedFile) { runproc = undefined; } - buildproc = spawn("npm", ["run", "build"], { stdio: "inherit", shell: true }); + buildproc = spawn("npm", ["run", "build:dev"], { stdio: "inherit", shell: true }); buildproc.on("exit", code => { buildproc = undefined; if (code === 0) { - runproc = spawn("npm", ["run", "start", "--", "--dev", ...args], { stdio: "inherit", shell: true }); + runproc = spawn("npm", ["run", "start", "--", ...args], { stdio: "inherit", shell: true }); runproc.on("exit", () => { runproc = undefined; }); @@ -35,4 +43,7 @@ function run(changedFile) { run(); chokidar.watch("src").on("change", run); chokidar.watch("static/fixed_responses").on("change", run); -chokidar.watch("static/webui").on("change", run); + +chokidar.watch("static/webui").on("change", () => { + fetch("http://localhost/custom/webuiFileChangeDetected?secret=" + secret); +}); diff --git a/src/controllers/custom/webuiFileChangeDetectedController.ts b/src/controllers/custom/webuiFileChangeDetectedController.ts new file mode 100644 index 00000000..3ec52a48 --- /dev/null +++ b/src/controllers/custom/webuiFileChangeDetectedController.ts @@ -0,0 +1,11 @@ +import { args } from "@/src/helpers/commandLineArguments"; +import { config } from "@/src/services/configService"; +import { sendWsBroadcast } from "@/src/services/webService"; +import { RequestHandler } from "express"; + +export const webuiFileChangeDetectedController: RequestHandler = (req, res) => { + if (args.dev && args.secret && req.query.secret == args.secret) { + sendWsBroadcast({ ports: { http: config.httpPort, https: config.httpsPort } }); + } + res.end(); +}; diff --git a/src/helpers/commandLineArguments.ts b/src/helpers/commandLineArguments.ts index 30938abf..09af105d 100644 --- a/src/helpers/commandLineArguments.ts +++ b/src/helpers/commandLineArguments.ts @@ -1,6 +1,7 @@ interface IArguments { configPath?: string; dev?: boolean; + secret?: string; } export const args: IArguments = {}; @@ -14,5 +15,9 @@ for (let i = 2; i < process.argv.length; ) { case "--dev": args.dev = true; break; + + case "--secret": + args.secret = process.argv[i++]; + break; } } diff --git a/src/routes/custom.ts b/src/routes/custom.ts index 7d8c7c82..75eb80a4 100644 --- a/src/routes/custom.ts +++ b/src/routes/custom.ts @@ -11,6 +11,7 @@ import { renameAccountController } from "@/src/controllers/custom/renameAccountC import { ircDroppedController } from "@/src/controllers/custom/ircDroppedController"; import { unlockAllIntrinsicsController } from "@/src/controllers/custom/unlockAllIntrinsicsController"; import { addMissingMaxRankModsController } from "@/src/controllers/custom/addMissingMaxRankModsController"; +import { webuiFileChangeDetectedController } from "@/src/controllers/custom/webuiFileChangeDetectedController"; import { createAccountController } from "@/src/controllers/custom/createAccountController"; import { createMessageController } from "@/src/controllers/custom/createMessageController"; @@ -20,10 +21,10 @@ import { addXpController } from "@/src/controllers/custom/addXpController"; import { importController } from "@/src/controllers/custom/importController"; import { manageQuestsController } from "@/src/controllers/custom/manageQuestsController"; import { setEvolutionProgressController } from "@/src/controllers/custom/setEvolutionProgressController"; +import { setBoosterController } from "@/src/controllers/custom/setBoosterController"; import { getConfigDataController } from "@/src/controllers/custom/getConfigDataController"; import { updateConfigDataController } from "@/src/controllers/custom/updateConfigDataController"; -import { setBoosterController } from "../controllers/custom/setBoosterController"; const customRouter = express.Router(); @@ -38,6 +39,7 @@ customRouter.get("/renameAccount", renameAccountController); customRouter.get("/ircDropped", ircDroppedController); customRouter.get("/unlockAllIntrinsics", unlockAllIntrinsicsController); customRouter.get("/addMissingMaxRankMods", addMissingMaxRankModsController); +customRouter.get("/webuiFileChangeDetected", webuiFileChangeDetectedController); customRouter.post("/createAccount", createAccountController); customRouter.post("/createMessage", createMessageController); diff --git a/src/routes/webui.ts b/src/routes/webui.ts index 02224903..535d68b1 100644 --- a/src/routes/webui.ts +++ b/src/routes/webui.ts @@ -1,6 +1,9 @@ import express from "express"; import path from "path"; import { repoDir, rootDir } from "@/src/helpers/pathHelper"; +import { args } from "@/src/helpers/commandLineArguments"; + +const baseDir = args.dev ? repoDir : rootDir; const webuiRouter = express.Router(); @@ -19,29 +22,29 @@ webuiRouter.use("/webui", (req, res, next) => { // Serve virtual routes webuiRouter.get("/webui/inventory", (_req, res) => { - res.sendFile(path.join(rootDir, "static/webui/index.html")); + res.sendFile(path.join(baseDir, "static/webui/index.html")); }); webuiRouter.get(/webui\/powersuit\/(.+)/, (_req, res) => { - res.sendFile(path.join(rootDir, "static/webui/index.html")); + res.sendFile(path.join(baseDir, "static/webui/index.html")); }); webuiRouter.get("/webui/mods", (_req, res) => { - res.sendFile(path.join(rootDir, "static/webui/index.html")); + res.sendFile(path.join(baseDir, "static/webui/index.html")); }); webuiRouter.get("/webui/settings", (_req, res) => { - res.sendFile(path.join(rootDir, "static/webui/index.html")); + res.sendFile(path.join(baseDir, "static/webui/index.html")); }); webuiRouter.get("/webui/quests", (_req, res) => { - res.sendFile(path.join(rootDir, "static/webui/index.html")); + res.sendFile(path.join(baseDir, "static/webui/index.html")); }); webuiRouter.get("/webui/cheats", (_req, res) => { - res.sendFile(path.join(rootDir, "static/webui/index.html")); + res.sendFile(path.join(baseDir, "static/webui/index.html")); }); webuiRouter.get("/webui/import", (_req, res) => { - res.sendFile(path.join(rootDir, "static/webui/index.html")); + res.sendFile(path.join(baseDir, "static/webui/index.html")); }); // Serve static files -webuiRouter.use("/webui", express.static(path.join(rootDir, "static/webui"))); +webuiRouter.use("/webui", express.static(path.join(baseDir, "static/webui"))); // Serve favicon webuiRouter.get("/favicon.ico", (_req, res) => { @@ -58,7 +61,7 @@ webuiRouter.get("/webui/riven-tool/RivenParser.js", (_req, res) => { // Serve translations webuiRouter.get("/translations/:file", (req, res) => { - res.sendFile(path.join(rootDir, `static/webui/translations/${req.params.file}`)); + res.sendFile(path.join(baseDir, `static/webui/translations/${req.params.file}`)); }); export { webuiRouter };