From 72c9212d9e0f7901aa65ce1ea42c69299552513b Mon Sep 17 00:00:00 2001 From: Rizky Date: Mon, 19 Aug 2024 20:17:23 +0700 Subject: [PATCH] fix --- pkgs/index.ts | 4 ++ pkgs/prod.ts | 6 +-- pkgs/server/prep-api-ts.ts | 75 ++++++++++++++++++++------------------ pkgs/utils/global.ts | 1 + 4 files changed, 48 insertions(+), 38 deletions(-) diff --git a/pkgs/index.ts b/pkgs/index.ts index 86855fd..3b6d1bd 100644 --- a/pkgs/index.ts +++ b/pkgs/index.ts @@ -12,6 +12,10 @@ import { config } from "./utils/config"; import { g } from "./utils/global"; import { createLogger } from "./utils/logger"; +if (process.argv[process.argv.length - 1] === "skip_types") { + g.skip_build_types = true; +} + let db_env: any = {}; try { db_env = parseEnv(await Bun.file(dir("app/db/.env")).text()); diff --git a/pkgs/prod.ts b/pkgs/prod.ts index d400a98..b22548c 100644 --- a/pkgs/prod.ts +++ b/pkgs/prod.ts @@ -38,9 +38,9 @@ if (process.env.DATABASE_URL) { } } -const startMain = () => { +const startMain = (argv?: string) => { return Bun.spawn({ - cmd: ["bun", "run", "pkgs/index.ts"], + cmd: ["bun", "run", "pkgs/index.ts", argv].filter((e) => e) as string[], cwd: process.cwd(), stdout: "inherit", stderr: "inherit", @@ -49,7 +49,7 @@ const startMain = () => { setTimeout(() => { subprocess.kill(); }, 5000); - main.process = startMain(); + main.process = startMain("skip_types"); } }, onExit(subprocess, exitCode, signalCode, error) { diff --git a/pkgs/server/prep-api-ts.ts b/pkgs/server/prep-api-ts.ts index c3ecf50..f067e35 100644 --- a/pkgs/server/prep-api-ts.ts +++ b/pkgs/server/prep-api-ts.ts @@ -4,21 +4,25 @@ import { dir } from "../utils/dir"; import { g } from "../utils/global"; export const prepareAPITypes = async () => { - const out: string[] = []; - for (const [k, v] of Object.entries(g.api)) { - const name = k.substring(0, k.length - 3).replace(/\W/gi, "_"); + if (!g.skip_build_types) { + const out: string[] = []; + for (const [k, v] of Object.entries(g.api)) { + const name = k.substring(0, k.length - 3).replace(/\W/gi, "_"); - let p = { - path: `"app/srv/api/${v.path}"`, - handler: `"./api/${v.path.substring(0, v.path.length - 3)}"`, - }; + let p = { + path: `"app/srv/api/${v.path}"`, + handler: `"./api/${v.path.substring(0, v.path.length - 3)}"`, + }; - if (!(await existsAsync(dir(p.path)))) { - p.path = `"pkgs/api/${v.path}"`; - p.handler = `"../../pkgs/api/${v.path.substring(0, v.path.length - 3)}"`; - } + if (!(await existsAsync(dir(p.path)))) { + p.path = `"pkgs/api/${v.path}"`; + p.handler = `"../../pkgs/api/${v.path.substring( + 0, + v.path.length - 3 + )}"`; + } - out.push(`\ + out.push(`\ export const ${name} = { name: "${name}", url: "${v.url}", @@ -26,31 +30,32 @@ export const ${name} = { args: ${JSON.stringify(v.args)}, handler: import(${p.handler}) }`); - } - await Bun.write(dir(`app/srv/exports.ts`), out.join(`\n`)); - - const targetFile = dir("app/srv/exports.d.ts"); - spawnSync( - [ - dir("node_modules/.bin/tsc"), - dir("app/srv/exports.ts"), - "--declaration", - "--emitDeclarationOnly", - "--outFile", - targetFile, - ], - { - cwd: dir(`node_modules/.bin`), } - ); + await Bun.write(dir(`app/srv/exports.ts`), out.join(`\n`)); - let res = await readAsync(targetFile); - if (res) { - res = res.replace('export * from "@prisma/client";', ""); - res = res.replace("server: Server;", ""); - res = res.replace(`import { PrismaClient } from "app/db/db";`, ""); - res = res.replace(`db: PrismaClient;`, ""); - await Bun.write(targetFile, res); + const targetFile = dir("app/srv/exports.d.ts"); + spawnSync( + [ + dir("node_modules/.bin/tsc"), + dir("app/srv/exports.ts"), + "--declaration", + "--emitDeclarationOnly", + "--outFile", + targetFile, + ], + { + cwd: dir(`node_modules/.bin`), + } + ); + + let res = await readAsync(targetFile); + if (res) { + res = res.replace('export * from "@prisma/client";', ""); + res = res.replace("server: Server;", ""); + res = res.replace(`import { PrismaClient } from "app/db/db";`, ""); + res = res.replace(`db: PrismaClient;`, ""); + await Bun.write(targetFile, res); + } } await getContent("load.js.dev"); diff --git a/pkgs/utils/global.ts b/pkgs/utils/global.ts index bc6a27e..c5aae08 100644 --- a/pkgs/utils/global.ts +++ b/pkgs/utils/global.ts @@ -47,6 +47,7 @@ export const g = global as unknown as { log: Logger; firebaseInit: boolean; firebase: admin.app.App; + skip_build_types: boolean; main: { process: null | Subprocess; restart: {