This commit is contained in:
Rizky 2024-08-05 20:57:05 +07:00
parent 49d18ad359
commit d6644860ee
1 changed files with 64 additions and 14 deletions

View File

@ -2,20 +2,23 @@ import { $ } from "bun";
import Database from "bun:sqlite"; import Database from "bun:sqlite";
import { copyAsync, removeAsync } from "fs-jetpack"; import { copyAsync, removeAsync } from "fs-jetpack";
import mime from "mime"; import mime from "mime";
import { deploy } from "utils/deploy";
import { dir } from "utils/dir"; import { dir } from "utils/dir";
import { g } from "utils/global"; import { g, SinglePage } from "utils/global";
import { getContent } from "../server/prep-api-ts";
export const _ = { export const _ = {
url: "/_zip", url: "/_zip",
raw: true, raw: true,
async api() { async api() {
await removeAsync(dir(`${g.datadir}/bundle.sqlite`)); await $`rm bundle*`.nothrow().quiet().cwd(`${g.datadir}`);
await copyAsync( await copyAsync(
dir(`pkgs/empty_bundle.sqlite`), dir(`pkgs/empty_bundle.sqlite`),
dir(`${g.datadir}/bundle.sqlite`) dir(`${g.datadir}/bundle.sqlite`)
); );
const db = new Database(dir(`${g.datadir}/bundle.sqlite`)); const db = new Database(dir(`${g.datadir}/bundle.sqlite`));
const ts = g.deploy.config.deploy.ts;
const add = ({ const add = ({
path, path,
type, type,
@ -25,18 +28,62 @@ export const _ = {
type: string; type: string;
content: string | Buffer; content: string | Buffer;
}) => { }) => {
const query = db.query( if (path) {
"INSERT INTO files (path, type, content) VALUES ($path, $type, $content)" const query = db.query(
); "INSERT INTO files (path, type, content) VALUES ($path, $type, $content)"
);
const res = query.run({ query.run({
$path: path.substring(`${g.datadir}/bundle`.length), $path: path.startsWith(g.datadir)
$type: type, ? path.substring(`${g.datadir}/bundle`.length)
$content: content, : path,
}); $type: type,
console.log(res); $content: content,
});
}
}; };
add({ path: "version", type: "", content: deploy.config.deploy.ts + "" });
add({ path: "site_id", type: "", content: deploy.config.site_id + "" });
add({
path: "base_url",
type: "",
content: g.deploy.content?.site?.config?.api_url || "",
});
const gz = g.deploy.content;
if (gz) {
let layout = null as null | SinglePage;
for (const l of gz.layouts) {
if (!layout) layout = l;
if (l.is_default_layout) layout = l;
}
let api_url = (gz.site as any)?.config?.api_url;
add({
path: "route",
type: "",
content: JSON.stringify({
site: {
...gz.site,
api_url,
},
urls: gz.pages.map((e) => {
return { id: e.id, url: e.url };
}),
layout: {
id: layout?.id,
root: layout?.content_tree,
},
}),
});
add({
path: "load-js",
type: "",
content: await getContent("load.js.prod", api_url),
});
}
for (const [directory, files] of Object.entries(g.deploy.content || {})) { for (const [directory, files] of Object.entries(g.deploy.content || {})) {
if (directory !== "code" && directory !== "site") { if (directory !== "code" && directory !== "site") {
for (const comp of Object.values(files) as any) { for (const comp of Object.values(files) as any) {
@ -87,7 +134,10 @@ export const _ = {
} }
} }
await $`zip bundle.zip bundle.sqlite`.quiet().cwd(`${g.datadir}`); await $`zip "bundle-${ts}.zip" bundle.sqlite`
return new Response(Bun.file(`${g.datadir}/bundle.zip`)); .nothrow()
.quiet()
.cwd(`${g.datadir}`);
return new Response(Bun.file(`${g.datadir}/bundle-${ts}.zip`));
}, },
}; };