You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.3 KiB
JavaScript

"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;
};