Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/build #3

Merged
merged 5 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ if [ "$1" = "dev" ]; then
done
elif [ "$1" = "gen" ]; then
GATEWAY_GEN="$2" bun node_modules/gateway/src/index.ts
elif [ "$1" = "build" ]; then
GATEWAY_BUILD="${2:-1}" bun node_modules/gateway/src/index.ts
else
bun node_modules/gateway/src/index.ts
fi
27 changes: 25 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Route } from "./route";
import { generateFile, parseBoolean, walk } from "./utils";
import { MatchedRoute, Server, ServerWebSocket } from "bun";
import { WebSocketContext } from "./ws";
import { exists } from "fs/promises";
import { exists, mkdir, cp, rmdir } from "fs/promises";

declare global {
var reloads: number;
Expand All @@ -34,6 +34,7 @@ const maxRequestBodySize = process.env.GATEWAY_MAX_REQUEST_BODY_SIZE
: 1024 * 1024 * 128;

const generate = process.env.GATEWAY_GEN;
const useStaticFiles = await exists("./public");

if (!debug) console.debug = () => {};

Expand Down Expand Up @@ -63,6 +64,28 @@ for await (const file of walk("./pages", ["ts"])) {
pages.set(file.split("/").slice(1).join("/"), route);
}

if (process.env.GATEWAY_BUILD) {
const buildDir =
process.env.GATEWAY_BUILD != "1" && process.env.GATEWAY_BUILD != "" ? process.env.GATEWAY_BUILD : "dist";
console.log("🏗️ Building...");
if (await exists(buildDir)) {
await rmdir(buildDir, { recursive: true });
}
await mkdir(buildDir);
if (useStaticFiles) {
await cp("public", buildDir, { recursive: true });
}
for await (let [key, route] of pages.entries()) {
const file = Bun.file(path.join(buildDir, key.replace(".ts", ".html")));
// @ts-ignore
const data = route.data ? await route.data(new Request(`http://127.0.0.1/${key}`), {}) : null;
const head = route.head ? route.head(data) : "";
const body = route.body ? route.body(data) : "";
await Bun.write(file, page(head, body instanceof Response ? await body.text() : body, false));
}
process.exit(0);
}

if (env == "dev") {
watch(
"./pages",
Expand Down Expand Up @@ -294,7 +317,7 @@ globalThis.server = Bun.serve<WebSocketContext>({
},
});

if (env == "dev" && (await exists("./public"))) {
if (env == "dev" && useStaticFiles) {
console.log(`🔎 Watching "public" for changes...`);
watch(
"./public",
Expand Down