-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserve.js
87 lines (74 loc) · 1.95 KB
/
serve.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const fs = require("fs");
const express = require("express");
const path = require("path");
const cors = require("cors");
const getFilesList = require("./getFilesList");
const getWidgetsFolder = require("./getWidgetsFolder");
const app = express();
// WIDGETS_FOLDER
const WIDGETS_FOLDER = process.env.WIDGETS_FOLDER;
if (!WIDGETS_FOLDER) {
throw new Error("You must provide WIDGETS_FOLDER");
}
// PROJECT_DIR
const PROJECT_DIR = process.env.PROJECT_DIR;
if (!PROJECT_DIR) {
throw new Error("You must provide PROJECT_DIR");
}
// Get final widgets folder dir
const widgetsFolder = getWidgetsFolder(WIDGETS_FOLDER, PROJECT_DIR);
app.use(cors({ origin: "*" }));
app.use(express.static(path.join(__dirname, "..", "dist")));
app.get("/widget/list", function (req, res) {
// Get widget files list
getFilesList(widgetsFolder).then((widgetsList) => {
const widgetKeys = Object.keys(widgetsList);
const widgetsName = widgetKeys.map((name) => name.replace(".jsx", ""));
// Send widget file names
res.json({
list: widgetsName,
});
});
});
const notFoundMessage = (widgetName) => `
return (
<div>
<p style={{ fontWeight: 600, color: "#AB2E28", fontFamily: "Courier new" }}>
The Widget <span
style={{
background: "#000",
color: "#fff",
padding: "2px",
}}
>
${widgetName}</span> was found.
</p>
</div>
);
`;
app.get("/widget/get/:widget", function (req, res) {
const widget = req.params?.widget;
if (!widget) {
res.status(400).send();
return;
}
const cachedWidget = `cache/${widget}.txt`;
// Check if the cache file exists
if (!fs.existsSync(cachedWidget)) {
res.json({
code: notFoundMessage(widget),
});
return;
}
// Serve cache file
fs.readFile(cachedWidget, "utf8", (err, cacheContent) => {
if (err) {
console.log("File read failed:", err);
return;
}
res.json({
code: cacheContent,
});
});
});
app.listen(9000);