"use strict"; const path = require("path"); const express = require("express"); const expressSSE = require("express-sse"); const chokidar = require("chokidar"); const debounce = require("debounce"); const processManager = require("../process-manager"); // TODO: Add a 'reloading...' indicator when re-running the process, since it may be slow -- as well as an "error occurred" indicator module.exports = function ({ absoluteProcessPath, processArguments }) { let lastData = "null"; let sse = new expressSSE(); let nodeProcess = processManager(absoluteProcessPath, processArguments, { onOutput: (output) => { lastData = { type: "output", payload: output }; sse.send(lastData); }, onError: (error) => { lastData = { type: "error", payload: error }; sse.send(lastData); }, onStart: () => { sse.send({ type: "running" }); } }); nodeProcess.start(); chokidar .watch(process.cwd()) .on("all", debounce((_event, path) => { nodeProcess.restart(path); }, 500)); let app = express(); app.use(express.static(path.join(__dirname, "public"))); app.get("/", (req, res) => { res.sendFile(path.join(__dirname, "public/index.html")); }); app.get("/initial_data", (req, res) => { res.send(lastData); }); app.get("/stream", sse.init); return app; };