fix
This commit is contained in:
parent
de88c0adcf
commit
073300d6d2
|
|
@ -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 />);
|
|
||||||
})();
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -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";
|
||||||
|
|
|
||||||
|
|
@ -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) {
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,40 @@
|
||||||
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);
|
||||||
|
|
||||||
|
try {
|
||||||
const api = await serveAPI(url, req);
|
const api = await serveAPI(url, req);
|
||||||
if (api) {
|
if (api) {
|
||||||
return api;
|
return api;
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
g.log.error(e);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const file = Bun.file(dir(`app/static${url.pathname}`));
|
const file = Bun.file(dir(`app/static${url.pathname}`));
|
||||||
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) {
|
||||||
|
g.log.error(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
return new Response(Bun.file(dir(`app/static/index.html`)) as any);
|
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 { 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";', "");
|
||||||
|
|
|
||||||
|
|
@ -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";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue