fix empty bundle

This commit is contained in:
Rizky 2024-08-05 14:25:57 +07:00
parent 2e168805e0
commit 49d18ad359
2 changed files with 57 additions and 17 deletions

View File

@ -1,5 +1,7 @@
import { $ } from "bun"; import { $ } from "bun";
import { removeAsync } from "fs-jetpack"; import Database from "bun:sqlite";
import { copyAsync, removeAsync } from "fs-jetpack";
import mime from "mime";
import { dir } from "utils/dir"; import { dir } from "utils/dir";
import { g } from "utils/global"; import { g } from "utils/global";
@ -7,38 +9,76 @@ export const _ = {
url: "/_zip", url: "/_zip",
raw: true, raw: true,
async api() { async api() {
await removeAsync(dir(`${g.datadir}/bundle`)); await removeAsync(dir(`${g.datadir}/bundle.sqlite`));
await copyAsync(
dir(`pkgs/empty_bundle.sqlite`),
dir(`${g.datadir}/bundle.sqlite`)
);
const db = new Database(dir(`${g.datadir}/bundle.sqlite`));
const add = ({
path,
type,
content,
}: {
path: string;
type: string;
content: string | Buffer;
}) => {
const query = db.query(
"INSERT INTO files (path, type, content) VALUES ($path, $type, $content)"
);
const res = query.run({
$path: path.substring(`${g.datadir}/bundle`.length),
$type: type,
$content: content,
});
console.log(res);
};
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) {
let filepath = `${g.datadir}/bundle/${directory}/${comp.id}.json`; let filepath = `${g.datadir}/bundle/${directory}/${comp.id}.json`;
await Bun.write(filepath, JSON.stringify(comp), {
createPath: true, add({
path: filepath,
type: mime.getType(filepath) || "text/plain",
content: JSON.stringify(comp),
}); });
} }
} else if (directory === "site") { } else if (directory === "site") {
await Bun.write( const filepath = `${g.datadir}/bundle/${directory}.json`;
`${g.datadir}/bundle/${directory}.json`, add({
JSON.stringify(files), path: filepath,
{ type: mime.getType(filepath) || "text/plain",
createPath: true, content: JSON.stringify(files),
} });
);
} else { } else {
for (const [filename, content] of Object.entries(files)) { for (const [filename, content] of Object.entries(files)) {
let filepath = `${g.datadir}/bundle/${directory}/${filename}`; let filepath = `${g.datadir}/bundle/${directory}/${filename}`;
if (content instanceof Buffer || typeof content === "string") { if (content instanceof Buffer || typeof content === "string") {
await Bun.write(filepath, content, { createPath: true }); add({
path: filepath,
type: mime.getType(filepath) || "text/plain",
content,
});
} else { } else {
for (const [k, v] of Object.entries(content || {})) { for (const [k, v] of Object.entries(content || {})) {
filepath = `${g.datadir}/bundle/${directory}/${filename}/${k}`; filepath = `${g.datadir}/bundle/${directory}/${filename}/${k}`;
if (v instanceof Buffer || typeof v === "string") { if (v instanceof Buffer || typeof v === "string") {
await Bun.write(filepath, v, { createPath: true }); add({
path: filepath,
type: mime.getType(filepath) || "text/plain",
content: v,
});
} else { } else {
await Bun.write(filepath, JSON.stringify(v), { add({
createPath: true, path: filepath,
type: mime.getType(filepath) || "text/plain",
content: JSON.stringify(v),
}); });
} }
} }
@ -47,7 +87,7 @@ export const _ = {
} }
} }
await $`zip -r bundle.zip .`.quiet().cwd(`${g.datadir}/bundle`); await $`zip bundle.zip bundle.sqlite`.quiet().cwd(`${g.datadir}`);
return new Response(Bun.file(`${g.datadir}/bundle/bundle.zip`)); return new Response(Bun.file(`${g.datadir}/bundle.zip`));
}, },
}; };

BIN
pkgs/empty_bundle.sqlite Normal file

Binary file not shown.