const express = require("express"); const { join: joinPath } = require("path"); const homeDir = require("os").homedir(); const fs = require("fs/promises"); const sass = require("sass"); const FACTORIO_DATA_DIR = process.env["FACTORIO_DATA_DIR"] ?? `${homeDir}/.steam/steam/steamapps/common/Factorio/data`; const exists = path => fs.stat(path).then(() => true, () => false); const app = express(); app.use(express.json()); app.use(express.static("public")); app.get("/data.json", (_req, res) => { res.sendFile(joinPath(__dirname, "build/data.json")); }); app.get(/.*\.css$/, async (req, res) => { const strippedFilepath = joinPath("public", req.path.slice(0, -4)); const isScss = await exists(strippedFilepath + ".scss"); const isSass = await exists(strippedFilepath + ".sass"); try { if (isScss) { res.type('css').send(sass.compile(strippedFilepath + ".scss").css); return; } if (isSass) { res.type('css').send(sass.compile(strippedFilepath + ".sass").css); return; } } catch(e) { console.error(e); } res.type('css').send("body { background: red; }"); }); app.get(/__.*__/, async (req, res) => { const [ , mod, path ] = /^\/__(.+?)__\/(.+)/.exec(req.path); const data = await fs.readFile(joinPath(FACTORIO_DATA_DIR, mod, path)); res.type('png').send(data); }) app.listen( Number(process.env["PORT"] ?? 8080), function () { return console.log(`listening on ${this.address().port}`); } );