improve build script
All checks were successful
/ test (push) Successful in 15s

This commit is contained in:
Soph :3 2024-07-20 14:05:26 +03:00
parent 874905b96b
commit d67aeebd85
Signed by: sophie
GPG key ID: EDA5D222A0C270F2

View file

@ -1,11 +1,29 @@
import * as esbuild from "esbuild" import * as esbuild from "esbuild"
import {watch, cpSync, rmdirSync, createReadStream, readFileSync, ReadStream} from "node:fs" import {watch, cpSync, rmdirSync, readFileSync, ReadStream} from "node:fs"
import { serve } from 'micro'; import { serve } from 'micro';
import {Server} from "node:http" import {Server} from "node:http"
import handler from "serve-handler" import handler from "serve-handler"
import { Readable } from "node:stream"; import { Readable } from "node:stream";
import { WebSocketServer } from 'ws'; import { WebSocketServer } from 'ws';
const script = `let t;function rr() {console.log("connecting to dev server")
let a = new WebSocket("ws://localhost:8081");
a.addEventListener("message", g => {
if(g.data == "refresh"){
location.reload()
}
});
a.addEventListener("open", () => {
console.log("connected")
})
a.addEventListener("close", () => {
console.log("socket closed, restarting in 1s")
clearInterval(t)
t = setTimeout(()=>{
rr()
},1000)
})};rr();`;
const argv = process.argv.slice(2) const argv = process.argv.slice(2)
async function buildTs() { async function buildTs() {
@ -25,8 +43,6 @@ if(argv[0] == "--build") {
} }
if(argv[0] == "--dev") { if(argv[0] == "--dev") {
const scriptName = Math.random().toFixed(10).replace("0.","")
const wss = new WebSocketServer({ port: 8081 }); const wss = new WebSocketServer({ port: 8081 });
let allConnections = new Map<number, WebSocket>(); let allConnections = new Map<number, WebSocket>();
wss.on('connection', function connection(ws) { wss.on('connection', function connection(ws) {
@ -40,31 +56,6 @@ if(argv[0] == "--dev") {
}); });
const server = new Server( const server = new Server(
serve(async (req, res) => { serve(async (req, res) => {
if(req.url == `/${scriptName}.js`) {
const body = `let t;function rr() {console.log("connecting to dev server")
let a = new WebSocket("ws://localhost:8081");
a.addEventListener("message", g => {
if(g.data == "refresh"){
location.reload()
}
});
a.addEventListener("open", () => {
console.log("connected")
})
a.addEventListener("close", () => {
console.log("socket closed, restarting in 1s")
clearInterval(t)
t = setTimeout(()=>{
rr()
},1000)
})};rr();`;
res
.writeHead(200, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/plain',
})
.end(body);
}
if(req.url == "/" || req.url?.endsWith(".html")) { if(req.url == "/" || req.url?.endsWith(".html")) {
await handler(req, res, { await handler(req, res, {
directoryListing: false, directoryListing: false,
@ -72,7 +63,7 @@ if(argv[0] == "--dev") {
}, { }, {
createReadStream(path, options) { createReadStream(path, options) {
let sx = readFileSync(path).toString("utf8"); let sx = readFileSync(path).toString("utf8");
sx = sx.replaceAll("</head>", `<script src="/${scriptName}.js"></script></head>`) sx = sx.replaceAll("</head>", `<script>${script}</script></head>`)
return Readable.from([sx]) as ReadStream return Readable.from([sx]) as ReadStream
}, },
}); });
@ -86,7 +77,7 @@ if(argv[0] == "--dev") {
) )
server.listen(8080) server.listen(8080)
console.log('[http] Listening HTTP on 8080.') console.log('[http] Listening HTTP on 8080.')
const watcher = watch("./src/web", { watch("./src/web", {
recursive: true, recursive: true,
}, async (e,f) => { }, async (e,f) => {
if(f == "dist.js") return; if(f == "dist.js") return;