This commit is contained in:
Rizky 2023-10-14 14:31:46 +07:00
parent de88c0adcf
commit 073300d6d2
6 changed files with 47 additions and 30 deletions

View File

@ -4,6 +4,23 @@ import { Root } from "./base/root";
import "./index.css"; import "./index.css";
import { createAPI, createDB, reloadDBAPI } from "./utils/script/init-api"; import { createAPI, createDB, reloadDBAPI } from "./utils/script/init-api";
const start = async () => {
registerServiceWorker();
defineReact();
await defineWindow(false);
const w = window as any;
const base = `${location.protocol}//${location.host}`;
await reloadDBAPI(base);
w.api = createAPI(base);
w.db = createDB(base);
const el = document.getElementById("root");
if (el) {
createRoot(el).render(<Root />);
}
};
const registerServiceWorker = async () => { const registerServiceWorker = async () => {
if ("serviceWorker" in navigator) { if ("serviceWorker" in navigator) {
try { try {
@ -20,21 +37,4 @@ const registerServiceWorker = async () => {
} }
}; };
registerServiceWorker(); start();
const el = document.getElementById("root");
if (el) {
(async () => {
defineReact();
await defineWindow(false);
const w = window as any;
const base = `${location.protocol}//${location.host}`;
await reloadDBAPI(base);
w.api = createAPI(base);
w.db = createDB(base);
createRoot(el).render(<Root />);
})();
}

View File

@ -8,6 +8,9 @@ import { config } from "./utils/config";
import { g } from "./utils/global"; import { g } from "./utils/global";
import { createLogger } from "./utils/logger"; import { createLogger } from "./utils/logger";
import { parcelBuild } from "utils/parcel"; import { parcelBuild } from "utils/parcel";
import { prepareApiRoutes } from "./server/api-scan";
g.status = "init";
await createLogger(); await createLogger();
g.port = parseInt(process.env.PORT || "4550"); g.port = parseInt(process.env.PORT || "4550");
@ -30,4 +33,6 @@ if (g.db) {
await createServer(); await createServer();
await parcelBuild(); await parcelBuild();
await generateAPIFrm(); await generateAPIFrm();
await prepareApiRoutes();
await prepareAPITypes(); await prepareAPITypes();
g.status = "ready";

View File

@ -5,8 +5,7 @@ import { dir } from "../utils/dir";
import { g } from "../utils/global"; import { g } from "../utils/global";
import { parseArgs } from "./parse-args"; import { parseArgs } from "./parse-args";
export const scanApi = async () => { export const prepareApiRoutes = async () => {
g.api = {};
const scan = async (path: string, root?: string) => { const scan = async (path: string, root?: string) => {
const apis = await listAsync(path); const apis = await listAsync(path);
if (apis) { if (apis) {

View File

@ -1,21 +1,24 @@
import { createRouter } from "radix3"; import { createRouter } from "radix3";
import { dir } from "../utils/dir"; import { dir } from "../utils/dir";
import { g } from "../utils/global"; import { g } from "../utils/global";
import { scanApi } from "./api-scan";
import { serveAPI } from "./serve-api"; import { serveAPI } from "./serve-api";
export const createServer = async () => { export const createServer = async () => {
g.api = {};
g.router = createRouter({ strictTrailingSlash: true }); g.router = createRouter({ strictTrailingSlash: true });
await scanApi();
g.server = Bun.serve({ g.server = Bun.serve({
port: g.port, port: g.port,
async fetch(req) { async fetch(req) {
if (g.status === "init") return new Response("initializing...");
const url = new URL(req.url); const url = new URL(req.url);
const api = await serveAPI(url, req); try {
if (api) { const api = await serveAPI(url, req);
return api; if (api) {
return api;
}
} catch (e) {
g.log.error(e);
} }
try { try {
@ -23,8 +26,15 @@ export const createServer = async () => {
if (file.type !== "application/octet-stream") { if (file.type !== "application/octet-stream") {
return new Response(file as any); return new Response(file as any);
} }
} catch (e) {} } catch (e) {
return new Response(Bun.file(dir(`app/static/index.html`)) as any); g.log.error(e);
}
try {
return new Response(Bun.file(dir(`app/static/index.html`)) as any);
} catch (e) {
g.log.error(e);
return new Response("Loading...");
}
}, },
}); });

View File

@ -1,4 +1,4 @@
import { spawnSync } from "bun"; import { spawn, spawnSync } from "bun";
import { readAsync } from "fs-jetpack"; import { readAsync } from "fs-jetpack";
import { dir } from "../utils/dir"; import { dir } from "../utils/dir";
import { g } from "../utils/global"; import { g } from "../utils/global";
@ -19,7 +19,7 @@ export const ${name} = {
await Bun.write(dir(`app/srv/exports.ts`), out.join(`\n`)); await Bun.write(dir(`app/srv/exports.ts`), out.join(`\n`));
const targetFile = dir("app/srv/exports.d.ts"); const targetFile = dir("app/srv/exports.d.ts");
spawnSync( const tsc = spawn(
[ [
dir("node_modules/.bin/tsc"), dir("node_modules/.bin/tsc"),
dir("app/srv/exports.ts"), dir("app/srv/exports.ts"),
@ -33,6 +33,8 @@ export const ${name} = {
} }
); );
await tsc.exited;
let res = await readAsync(targetFile); let res = await readAsync(targetFile);
if (res) { if (res) {
res = res.replace('export * from "@prisma/client";', ""); res = res.replace('export * from "@prisma/client";', "");

View File

@ -11,6 +11,7 @@ type SingleRoute = {
}; };
export const g = global as unknown as { export const g = global as unknown as {
status: "init" | "ready";
db: PrismaClient; db: PrismaClient;
dburl: string; dburl: string;
mode: "dev" | "prod"; mode: "dev" | "prod";