create-meridian-app 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +16 -30
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -41,43 +41,29 @@ var MIME_TYPES = {
|
|
|
41
41
|
".woff": "font/woff",
|
|
42
42
|
".woff2": "font/woff2"
|
|
43
43
|
};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return API_PATHS.some((p) => urlPath === p || urlPath.startsWith(p + "/") || urlPath.startsWith(p + "?"));
|
|
47
|
-
}
|
|
48
|
-
function proxyToApi(req, res, apiHost, apiPort) {
|
|
49
|
-
const proxyReq = http.request(
|
|
50
|
-
{
|
|
51
|
-
hostname: apiHost,
|
|
52
|
-
port: apiPort,
|
|
53
|
-
path: req.url,
|
|
54
|
-
method: req.method,
|
|
55
|
-
headers: { ...req.headers, host: `${apiHost}:${apiPort}` }
|
|
56
|
-
},
|
|
57
|
-
(proxyRes) => {
|
|
58
|
-
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
|
59
|
-
proxyRes.pipe(res, { end: true });
|
|
60
|
-
}
|
|
61
|
-
);
|
|
62
|
-
proxyReq.on("error", () => {
|
|
63
|
-
if (!res.headersSent) res.writeHead(502);
|
|
64
|
-
res.end("API unavailable");
|
|
65
|
-
});
|
|
66
|
-
req.pipe(proxyReq, { end: true });
|
|
67
|
-
}
|
|
68
|
-
function startDashboardServer(distDir, port, apiPort, apiHost = "127.0.0.1") {
|
|
44
|
+
function startDashboardServer(distDir, port, apiPort, apiHost = "localhost") {
|
|
45
|
+
const configScript = `<script>window.__MERIDIAN_CONFIG__ = { apiUrl: "http://${apiHost}:${apiPort}" };</script>`;
|
|
69
46
|
return new Promise((resolve, reject) => {
|
|
70
47
|
const server = http.createServer((req, res) => {
|
|
71
48
|
const urlPath = (req.url ?? "/").split("?")[0];
|
|
72
|
-
if (isApiRequest(urlPath)) {
|
|
73
|
-
proxyToApi(req, res, apiHost, apiPort);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
49
|
let filePath = path.join(distDir, urlPath === "/" ? "index.html" : urlPath);
|
|
77
50
|
if (!existsSync(filePath) || fs.statSync(filePath).isDirectory()) {
|
|
78
51
|
filePath = path.join(distDir, "index.html");
|
|
79
52
|
}
|
|
80
53
|
const ext = path.extname(filePath);
|
|
54
|
+
if (ext === ".html") {
|
|
55
|
+
try {
|
|
56
|
+
const html = fs.readFileSync(filePath, "utf-8");
|
|
57
|
+
const injected = html.replace("<head>", `<head>
|
|
58
|
+
${configScript}`);
|
|
59
|
+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
|
|
60
|
+
res.end(injected);
|
|
61
|
+
} catch {
|
|
62
|
+
res.writeHead(404);
|
|
63
|
+
res.end("Not found");
|
|
64
|
+
}
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
81
67
|
const contentType = MIME_TYPES[ext] ?? "application/octet-stream";
|
|
82
68
|
fs.readFile(filePath, (err, data) => {
|
|
83
69
|
if (err) {
|
|
@@ -109,7 +95,7 @@ async function runServeDashboard(portOverride) {
|
|
|
109
95
|
const port = portOverride ?? dashboardPort;
|
|
110
96
|
const server = await startDashboardServer(distDir, port, apiPort);
|
|
111
97
|
console.log(chalk.green(" \u2714 Admin dashboard: ") + chalk.cyan(`http://localhost:${port}`));
|
|
112
|
-
console.log(chalk.dim(` \u2192 API
|
|
98
|
+
console.log(chalk.dim(` \u2192 API: http://localhost:${apiPort}`));
|
|
113
99
|
const shutdown = () => {
|
|
114
100
|
server.close();
|
|
115
101
|
process.exit(0);
|