mirror of
https://github.com/wah-su/wah-su.github.io.git
synced 2025-09-13 18:03:54 +05:00
add build script
This commit is contained in:
parent
7288957cf9
commit
af9ac7f452
8 changed files with 587 additions and 0 deletions
1
src/build.tsx
Normal file
1
src/build.tsx
Normal file
|
@ -0,0 +1 @@
|
|||
console.log("Hello via Bun!");
|
11
src/static_dev/hotreload.js
Normal file
11
src/static_dev/hotreload.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
let webSocket = new WebSocket(`ws://${window.location.hostname}:3001`);
|
||||
webSocket.onmessage = function (e) {
|
||||
if (e.data == "RELOAD") {
|
||||
console.log("Reloading page after build");
|
||||
location.reload();
|
||||
} else if (e.data == "CONNECTED") {
|
||||
console.log("Connected to server");
|
||||
} else {
|
||||
console.warn(`unknown data received: ${e}`);
|
||||
}
|
||||
};
|
75
src/utils.ts
Normal file
75
src/utils.ts
Normal file
|
@ -0,0 +1,75 @@
|
|||
import { red, yellow } from "picocolors";
|
||||
|
||||
export class Log {
|
||||
_level = 1;
|
||||
_levelText = "INFO";
|
||||
|
||||
constructor(level: "DEBUG" | "INFO" | "WARN" | "ERROR" = "INFO") {
|
||||
let _level = 1;
|
||||
switch (level) {
|
||||
case "DEBUG":
|
||||
_level = 0;
|
||||
break;
|
||||
case "INFO":
|
||||
_level = 1;
|
||||
break;
|
||||
case "WARN":
|
||||
_level = 2;
|
||||
break;
|
||||
case "ERROR":
|
||||
_level = 3;
|
||||
break;
|
||||
}
|
||||
this._level = _level;
|
||||
this._levelText = level;
|
||||
}
|
||||
|
||||
_time() {
|
||||
const date = new Date();
|
||||
return date.toLocaleTimeString();
|
||||
}
|
||||
|
||||
_fmt(time: string, level: string, message: string) {
|
||||
return `${time}:${level} - ${message}`;
|
||||
}
|
||||
|
||||
debug(message: string | any, isConnected = false) {
|
||||
if (this._level == 0) {
|
||||
if (isConnected) {
|
||||
message = `↳ ${message}`;
|
||||
}
|
||||
console.log(this._fmt(this._time(), "DEBUG", message));
|
||||
return this._fmt(this._time(), "DEBUG", message);
|
||||
}
|
||||
}
|
||||
|
||||
info(message: string | any, isConnected = false) {
|
||||
if (this._level <= 1) {
|
||||
if (isConnected) {
|
||||
message = `↳ ${message}`;
|
||||
}
|
||||
console.log(this._fmt(this._time(), "INFO", message));
|
||||
return this._fmt(this._time(), "INFO", message);
|
||||
}
|
||||
}
|
||||
|
||||
warn(message: string | any, isConnected = false) {
|
||||
if (this._level <= 2) {
|
||||
if (isConnected) {
|
||||
message = `↳ ${message}`;
|
||||
}
|
||||
console.log(yellow(this._fmt(this._time(), "WARN", message)));
|
||||
return this._fmt(this._time(), "WARN", message);
|
||||
}
|
||||
}
|
||||
|
||||
error(message: string | any, isConnected = false) {
|
||||
if (this._level <= 3) {
|
||||
if (isConnected) {
|
||||
message = `↳ ${message}`;
|
||||
}
|
||||
console.log(red(this._fmt(this._time(), "ERROR", message)));
|
||||
return this._fmt(this._time(), "ERROR", message);
|
||||
}
|
||||
}
|
||||
}
|
105
src/watch.ts
Normal file
105
src/watch.ts
Normal file
|
@ -0,0 +1,105 @@
|
|||
import WebSocket from "ws";
|
||||
import fs from "fs";
|
||||
import chokidar from "chokidar";
|
||||
import exec from "child_process";
|
||||
import express from "express";
|
||||
import { Log } from "./utils";
|
||||
import path from "path";
|
||||
import { WebSocketServer } from "ws";
|
||||
|
||||
let triggered = 0;
|
||||
const delay = 1000;
|
||||
|
||||
let SIGINTCount = 0;
|
||||
let WSclients: any[] = [];
|
||||
|
||||
const log = new Log();
|
||||
|
||||
function onChange() {
|
||||
if (triggered != 0 && Date.now() - triggered < delay) {
|
||||
log.warn(`Rebuild was triggered less than a ${delay}ms ago!`, true);
|
||||
return;
|
||||
}
|
||||
triggered = Date.now();
|
||||
|
||||
exec.exec("bun run ./src/build.tsx", (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
log.error(error.message);
|
||||
return;
|
||||
}
|
||||
if (stderr) {
|
||||
log.error(stderr);
|
||||
return;
|
||||
}
|
||||
log.info(stdout);
|
||||
if (WSclients.length > 0) {
|
||||
log.info("Reloading web page...");
|
||||
WSclients.forEach((ws) => ws.send("RELOAD"));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function onExit() {
|
||||
fs.rmdirSync("out", { recursive: true });
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const watcher = chokidar.watch(["./src", "./stickerpacks", "./static"], {
|
||||
ignored: (filePath, stats) => filePath.endsWith("watch.ts"),
|
||||
atomic: true,
|
||||
awaitWriteFinish: true,
|
||||
persistent: true,
|
||||
});
|
||||
|
||||
function startServerWithRebuild() {
|
||||
const app = express();
|
||||
const folder = path.join(__dirname, "../out");
|
||||
const wss = new WebSocketServer({ port: 3001 });
|
||||
|
||||
process.env.ENVIRONMENT = "dev";
|
||||
|
||||
wss.on("connection", (ws: WebSocket) => {
|
||||
WSclients.push(ws);
|
||||
log.info(`Client ${WSclients.length} connected`);
|
||||
ws.send("CONNECTED");
|
||||
});
|
||||
|
||||
process.on("SIGINT", () => {
|
||||
SIGINTCount += 1;
|
||||
if (WSclients.length > 0) {
|
||||
async function _closeWS() {
|
||||
WSclients.forEach(async (ws) => await ws.close());
|
||||
}
|
||||
_closeWS();
|
||||
}
|
||||
if (SIGINTCount == 1) {
|
||||
log.info("Gracefully shutdown and cleanup...");
|
||||
onExit();
|
||||
} else if (SIGINTCount >= 3) {
|
||||
log.info("Received 3+ SIGINT signals. Force exit...");
|
||||
process.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
app.use(express.static(folder));
|
||||
app.listen(3000, () => {
|
||||
log.info(`Serving files from folder ${folder}`);
|
||||
log.info("Express server is running on port 3000: http://127.0.0.1:3000");
|
||||
|
||||
watcher
|
||||
.on("add", (path) => {
|
||||
log.info(`File ${path} has been added, rebuilding...`);
|
||||
onChange();
|
||||
})
|
||||
.on("change", (path) => {
|
||||
log.info(`File ${path} has been changed, rebuilding...`);
|
||||
onChange();
|
||||
})
|
||||
.on("unlink", (path) => {
|
||||
log.info(`File ${path} has been removed, rebuilding...`);
|
||||
onChange();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
startServerWithRebuild();
|
Loading…
Add table
Add a link
Reference in a new issue