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 { 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 { g } from "utils/global";
@ -7,38 +9,76 @@ export const _ = {
url: "/_zip",
raw: true,
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 || {})) {
if (directory !== "code" && directory !== "site") {
for (const comp of Object.values(files) as any) {
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") {
await Bun.write(
`${g.datadir}/bundle/${directory}.json`,
JSON.stringify(files),
{
createPath: true,
}
);
const filepath = `${g.datadir}/bundle/${directory}.json`;
add({
path: filepath,
type: mime.getType(filepath) || "text/plain",
content: JSON.stringify(files),
});
} else {
for (const [filename, content] of Object.entries(files)) {
let filepath = `${g.datadir}/bundle/${directory}/${filename}`;
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 {
for (const [k, v] of Object.entries(content || {})) {
filepath = `${g.datadir}/bundle/${directory}/${filename}/${k}`;
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 {
await Bun.write(filepath, JSON.stringify(v), {
createPath: true,
add({
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`);
return new Response(Bun.file(`${g.datadir}/bundle/bundle.zip`));
await $`zip bundle.zip bundle.sqlite`.quiet().cwd(`${g.datadir}`);
return new Response(Bun.file(`${g.datadir}/bundle.zip`));
},
};

BIN
pkgs/empty_bundle.sqlite Normal file

Binary file not shown.