diff --git a/pkgs/api/_deploy.ts b/pkgs/api/_deploy.ts index 866d71a..2fd73f0 100644 --- a/pkgs/api/_deploy.ts +++ b/pkgs/api/_deploy.ts @@ -1,3 +1,4 @@ +import { $ } from "bun"; import * as fs from "fs"; import { dirAsync, readAsync, removeAsync, writeAsync } from "fs-jetpack"; import { apiContext } from "service-srv"; @@ -6,7 +7,6 @@ import { dir } from "utils/dir"; import { g } from "utils/global"; import { genEnv, parseEnv } from "utils/parse-env"; import { restartServer } from "utils/restart"; -import { $ } from "bun"; export const _ = { url: "/_deploy", @@ -27,7 +27,7 @@ export const _ = { | { type: "redeploy"; ts: string } ) & { id_site: string; - } + }, ) { const { res } = apiContext(this); @@ -103,24 +103,24 @@ export const _ = { generator client { provider = "prisma-client-js" } - + datasource db { provider = "${type}" url = env("DATABASE_URL") - }` + }`, ); try { await Bun.write( dir("app/db/.env"), - `DATABASE_URL=${ENV.DATABASE_URL}` + `DATABASE_URL=${ENV.DATABASE_URL}`, ); await $`bun install`.cwd(dir("app/db")); await $`bun prisma db pull --force`.cwd(dir("app/db")); await $`bun prisma generate`.cwd(dir("app/db")); await Bun.write( dir(`${g.datadir}/db-ver`), - Date.now().toString() + Date.now().toString(), ); } catch (e) { console.error(e); @@ -205,7 +205,7 @@ export const _ = { export const downloadFile = async ( url: string, filePath: string, - progress?: (rec: number, total: number) => void + progress?: (rec: number, total: number) => void, ) => { try { const _url = new URL(url); @@ -238,7 +238,7 @@ export const downloadFile = async ( if (progress) { progress( receivedLength, - parseInt(res.headers.get("content-length") || "0") + parseInt(res.headers.get("content-length") || "0"), ); } } diff --git a/pkgs/server/create.ts b/pkgs/server/create.ts index 7ae5ccd..cf6026b 100644 --- a/pkgs/server/create.ts +++ b/pkgs/server/create.ts @@ -1,13 +1,13 @@ import { file } from "bun"; -import { existsAsync, inspectAsync, listAsync } from "fs-jetpack"; +import { inspectAsync, listAsync } from "fs-jetpack"; import { join } from "path"; import { createRouter } from "radix3"; +import { prodIndex } from "utils/prod-index"; import { dir } from "../utils/dir"; import { g } from "../utils/global"; import { parseArgs } from "./parse-args"; import { serveAPI } from "./serve-api"; import { serveWeb } from "./serve-web"; -import { prodIndex } from "utils/prod-index"; export const createServer = async () => { g.router = createRouter({ strictTrailingSlash: true }); @@ -33,8 +33,8 @@ export const createServer = async () => { } catch (e) { g.log.warn( `Failed to import app/srv/api${importPath.substring( - (root || path).length - )}` + (root || path).length, + )}`, ); const f = file(importPath); @@ -92,6 +92,7 @@ export const createServer = async () => { if (g.deploy.content) { const core = g.deploy.content.code.core; const site = g.deploy.content.code.site; + const pub = g.deploy.content.public; let pathname = url.pathname; if (url.pathname[0] === "/") pathname = pathname.substring(1); @@ -112,6 +113,7 @@ export const createServer = async () => { if (core[pathname]) content = core[pathname]; else if (site[pathname]) content = site[pathname]; + else if (pub[pathname]) content = pub[pathname]; if (content) { return await serveWeb({ diff --git a/pkgs/utils/deploy.ts b/pkgs/utils/deploy.ts index dc481f3..826e4f1 100644 --- a/pkgs/utils/deploy.ts +++ b/pkgs/utils/deploy.ts @@ -6,13 +6,12 @@ import { removeAsync, writeAsync, } from "fs-jetpack"; +import { decode } from "msgpackr"; +import { createRouter } from "radix3"; +import { startBrCompress } from "./br-load"; import { dir } from "./dir"; import { g } from "./global"; import { gunzipAsync } from "./gzip"; -import { createRouter } from "radix3"; -import { prodIndex } from "./prod-index"; -import { startBrCompress } from "./br-load"; -import { decode } from "msgpackr"; const decoder = new TextDecoder(); export const deploy = { @@ -33,19 +32,19 @@ export const deploy = { g.deploy.content = decode( await gunzipAsync( new Uint8Array( - await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer() - ) - ) + await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer(), + ), + ), ); } else { g.deploy.content = JSON.parse( decoder.decode( await gunzipAsync( new Uint8Array( - await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer() - ) - ) - ) + await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer(), + ), + ), + ), ); } @@ -99,7 +98,7 @@ export const deploy = { await removeAsync(dir(`app/web/server`)); await dirAsync(dir(`app/web/server`)); for (const [k, v] of Object.entries( - g.deploy.content.code.server + g.deploy.content.code.server, )) { await writeAsync(dir(`app/web/server/${k}`), v); } @@ -143,10 +142,10 @@ export const deploy = { } console.log( - `Downloading site deploy: ${this.config.site_id} [ts: ${this.config.deploy.ts}] ${base_url}` + `Downloading site deploy: ${this.config.site_id} [ts: ${this.config.deploy.ts}] ${base_url}`, ); const res = await fetch( - `${base_url}/prod-zip/${this.config.site_id}?ts=${Date.now()}&msgpack=1` + `${base_url}/prod-zip/${this.config.site_id}?ts=${Date.now()}&msgpack=1`, ); const ts = Date.now(); @@ -188,13 +187,13 @@ export const deploy = { saveConfig() { return Bun.write( Bun.file(dir(`app/web/config.json`)), - JSON.stringify(this.config, null, 2) + JSON.stringify(this.config, null, 2), ); }, has_gz() { if (this.config.deploy.ts) { return Bun.file( - dir(`app/web/deploy/${this.config.deploy.ts}.gz`) + dir(`app/web/deploy/${this.config.deploy.ts}.gz`), ).exists(); } diff --git a/pkgs/utils/restart.ts b/pkgs/utils/restart.ts index a125eb9..ede7e8b 100644 --- a/pkgs/utils/restart.ts +++ b/pkgs/utils/restart.ts @@ -5,6 +5,5 @@ export const restartServer = () => { if (g.mode === "dev") { $`bun ${g.mode}`; } - process.exit(0); };