fix
This commit is contained in:
parent
de88c0adcf
commit
073300d6d2
|
|
@ -4,6 +4,23 @@ import { Root } from "./base/root";
|
|||
import "./index.css";
|
||||
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 () => {
|
||||
if ("serviceWorker" in navigator) {
|
||||
try {
|
||||
|
|
@ -20,21 +37,4 @@ const registerServiceWorker = async () => {
|
|||
}
|
||||
};
|
||||
|
||||
registerServiceWorker();
|
||||
|
||||
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 />);
|
||||
})();
|
||||
}
|
||||
start();
|
||||
|
|
|
|||
|
|
@ -8,6 +8,9 @@ import { config } from "./utils/config";
|
|||
import { g } from "./utils/global";
|
||||
import { createLogger } from "./utils/logger";
|
||||
import { parcelBuild } from "utils/parcel";
|
||||
import { prepareApiRoutes } from "./server/api-scan";
|
||||
|
||||
g.status = "init";
|
||||
|
||||
await createLogger();
|
||||
g.port = parseInt(process.env.PORT || "4550");
|
||||
|
|
@ -30,4 +33,6 @@ if (g.db) {
|
|||
await createServer();
|
||||
await parcelBuild();
|
||||
await generateAPIFrm();
|
||||
await prepareApiRoutes();
|
||||
await prepareAPITypes();
|
||||
g.status = "ready";
|
||||
|
|
|
|||
|
|
@ -5,8 +5,7 @@ import { dir } from "../utils/dir";
|
|||
import { g } from "../utils/global";
|
||||
import { parseArgs } from "./parse-args";
|
||||
|
||||
export const scanApi = async () => {
|
||||
g.api = {};
|
||||
export const prepareApiRoutes = async () => {
|
||||
const scan = async (path: string, root?: string) => {
|
||||
const apis = await listAsync(path);
|
||||
if (apis) {
|
||||
|
|
|
|||
|
|
@ -1,21 +1,24 @@
|
|||
import { createRouter } from "radix3";
|
||||
import { dir } from "../utils/dir";
|
||||
import { g } from "../utils/global";
|
||||
import { scanApi } from "./api-scan";
|
||||
import { serveAPI } from "./serve-api";
|
||||
|
||||
export const createServer = async () => {
|
||||
g.api = {};
|
||||
g.router = createRouter({ strictTrailingSlash: true });
|
||||
await scanApi();
|
||||
|
||||
g.server = Bun.serve({
|
||||
port: g.port,
|
||||
async fetch(req) {
|
||||
if (g.status === "init") return new Response("initializing...");
|
||||
const url = new URL(req.url);
|
||||
|
||||
const api = await serveAPI(url, req);
|
||||
if (api) {
|
||||
return api;
|
||||
try {
|
||||
const api = await serveAPI(url, req);
|
||||
if (api) {
|
||||
return api;
|
||||
}
|
||||
} catch (e) {
|
||||
g.log.error(e);
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
@ -23,8 +26,15 @@ export const createServer = async () => {
|
|||
if (file.type !== "application/octet-stream") {
|
||||
return new Response(file as any);
|
||||
}
|
||||
} catch (e) {}
|
||||
return new Response(Bun.file(dir(`app/static/index.html`)) as any);
|
||||
} catch (e) {
|
||||
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...");
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { spawnSync } from "bun";
|
||||
import { spawn, spawnSync } from "bun";
|
||||
import { readAsync } from "fs-jetpack";
|
||||
import { dir } from "../utils/dir";
|
||||
import { g } from "../utils/global";
|
||||
|
|
@ -19,7 +19,7 @@ export const ${name} = {
|
|||
await Bun.write(dir(`app/srv/exports.ts`), out.join(`\n`));
|
||||
|
||||
const targetFile = dir("app/srv/exports.d.ts");
|
||||
spawnSync(
|
||||
const tsc = spawn(
|
||||
[
|
||||
dir("node_modules/.bin/tsc"),
|
||||
dir("app/srv/exports.ts"),
|
||||
|
|
@ -33,6 +33,8 @@ export const ${name} = {
|
|||
}
|
||||
);
|
||||
|
||||
await tsc.exited;
|
||||
|
||||
let res = await readAsync(targetFile);
|
||||
if (res) {
|
||||
res = res.replace('export * from "@prisma/client";', "");
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ type SingleRoute = {
|
|||
};
|
||||
|
||||
export const g = global as unknown as {
|
||||
status: "init" | "ready";
|
||||
db: PrismaClient;
|
||||
dburl: string;
|
||||
mode: "dev" | "prod";
|
||||
|
|
|
|||
Loading…
Reference in New Issue