Skip to content

Commit a25e255

Browse files
committed
add scramjet devserver for demo
1 parent ba8b8d2 commit a25e255

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

packages/scramjet/devserver.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { createServer } from "vite";
2+
import fs from "node:fs/promises";
3+
import { stdout } from "node:process";
4+
import chalk from "chalk";
5+
import { execSync } from "node:child_process";
6+
import http from "node:http";
7+
import path from "node:path";
8+
import { createReadStream } from "node:fs";
9+
import rspackConfig from "./rspack.config.ts";
10+
import { server as wisp } from "@mercuryworkshop/wisp-js/server";
11+
import {
12+
black,
13+
logSuccess,
14+
printBanner,
15+
resetSuccessLog,
16+
runRspack,
17+
} from "./devlib.ts";
18+
19+
const image = await fs.readFile("./assets/scramjet-mini-noalpha.png");
20+
21+
const commit = execSync("git rev-parse --short HEAD", {
22+
encoding: "utf-8",
23+
}).replace(/\r?\n|\r/g, "");
24+
const branch = execSync("git rev-parse --abbrev-ref HEAD", {
25+
encoding: "utf-8",
26+
}).replace(/\r?\n|\r/g, "");
27+
const packagejson = JSON.parse(await fs.readFile("./package.json", "utf-8"));
28+
const version = packagejson.version;
29+
30+
const DEMO_PORT = process.env.CHROME_PORT || 4141;
31+
const WISP_PORT = process.env.WISP_PORT || 4142;
32+
33+
process.env.VITE_WISP_URL =
34+
process.env.VITE_WISP_URL || `ws://localhost:${WISP_PORT}/`;
35+
36+
const wispserver = http.createServer((req, res) => {
37+
res.writeHead(200, { "Content-Type": "text/plain" });
38+
res.end("wisp server js rewrite");
39+
});
40+
41+
wispserver.on("upgrade", (req, socket, head) => {
42+
wisp.routeRequest(req, socket, head);
43+
});
44+
45+
wispserver.listen(Number(WISP_PORT));
46+
47+
const server = await createServer({
48+
configFile: "./packages/demo/vite.config.ts",
49+
root: "./packages/demo",
50+
server: {
51+
port: Number(DEMO_PORT),
52+
strictPort: true,
53+
},
54+
});
55+
56+
await server.listen();
57+
58+
const accent = (text: string) => chalk.hex("#f1855bff").bold(text);
59+
const highlight = (text: string) => chalk.hex("#fdd76cff").bold(text);
60+
const urlColor = (text: string) => chalk.hex("#64DFDF").underline(text);
61+
const note = (text: string) => chalk.hex("#CDB4DB")(text);
62+
const connector = chalk.hex("#8D99AE").dim("@");
63+
64+
const lines = [
65+
black()(`${highlight("SCRAMJET DEV SERVER")}`),
66+
black()(
67+
`${accent("demo")} ${connector} ${urlColor(
68+
`http://localhost:${DEMO_PORT}/`
69+
)}`
70+
),
71+
black()(
72+
`${accent("wisp")} ${connector} ${urlColor(
73+
process.env.VITE_WISP_URL ?? ""
74+
)}`
75+
),
76+
black()(chalk.dim(`[${branch}] ${commit} scramjet/${version}`)),
77+
];
78+
79+
printBanner(image, lines);
80+
81+
runRspack(rspackConfig);

packages/scramjet/pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)