This commit is contained in:
Rizky
2024-02-14 21:40:12 +07:00
parent 2e6c0de9d5
commit 3b553b8349
10 changed files with 264 additions and 134 deletions
+22 -49
View File
@@ -2,6 +2,7 @@ import { $ } from "execa";
import * as fs from "fs";
import { dirAsync, removeAsync, writeAsync } from "fs-jetpack";
import { apiContext } from "service-srv";
import { deploy } from "utils/deploy";
import { dir } from "utils/dir";
import { g } from "utils/global";
import { restartServer } from "utils/restart";
@@ -29,14 +30,14 @@ export const _ = {
const path = dir(`app/web/`);
await dirAsync(path);
const web = g.web;
switch (action.type) {
case "check":
const deploys = fs.readdirSync(dir(`/app/web/deploy`));
return {
now: Date.now(),
current: 0,
deploys: [],
current: parseInt(g.deploy.config.deploy.ts),
deploys: deploys.map((e) => parseInt(e.replace(".gz", ""))),
db: {
url: g.dburl || "-",
},
@@ -66,6 +67,7 @@ datasource db {
url = env("DATABASE_URL")
}`
);
await $({ cwd: dir("app/db") })`bun install`;
await $({ cwd: dir("app/db") })`bun prisma db pull`;
await $({ cwd: dir("app/db") })`bun prisma generate`;
@@ -85,14 +87,13 @@ datasource db {
break;
case "deploy-del":
{
web.deploys = web.deploys.filter((e) => e !== parseInt(action.ts));
try {
await removeAsync(`${path}/deploys/${action.ts}`);
} catch (e) {}
await removeAsync(dir(`/app/web/deploy/${action.ts}.gz`));
const deploys = fs.readdirSync(dir(`/app/web/deploy`));
return {
now: Date.now(),
current: web.current,
deploys: web.deploys,
current: parseInt(deploy.config.deploy.ts),
deploys: deploys.map((e) => parseInt(e.replace(".gz", ""))),
};
}
break;
@@ -100,56 +101,28 @@ datasource db {
break;
case "deploy":
{
await fs.promises.mkdir(`${path}/deploys`, { recursive: true });
const cur = Date.now();
const filePath = `${path}/deploys/${cur}`;
web.deploying = {
status: "generating",
received: 0,
total: 0,
};
if (
await downloadFile(action.dlurl, filePath, (rec, total) => {
web.deploying = {
status: "transfering",
received: rec,
total: total,
};
})
) {
web.deploying.status = "deploying";
await fs.promises.writeFile(`${path}/current`, cur.toString());
web.current = cur;
web.deploys.push(cur);
}
web.deploying = null;
deploy.config.deploy.ts = Date.now() + "";
await deploy.init();
const deploys = fs.readdirSync(dir(`/app/web/deploy`));
return {
now: Date.now(),
current: web.current,
deploys: web.deploys,
current: parseInt(deploy.config.deploy.ts),
deploys: deploys.map((e) => parseInt(e.replace(".gz", ""))),
};
}
break;
case "redeploy":
{
const cur = parseInt(action.ts);
const lastcur = web.current;
try {
if (web.deploys.find((e) => e === cur)) {
web.current = cur;
await fs.promises.writeFile(`${path}/current`, cur.toString());
}
} catch (e) {
web.current = lastcur;
web.deploys = web.deploys.filter((e) => e !== parseInt(action.ts));
await removeAsync(`${path}/deploys/${action.ts}`);
}
deploy.config.deploy.ts = action.ts;
await deploy.saveConfig();
await deploy.load(action.ts);
const deploys = fs.readdirSync(dir(`/app/web/deploy`));
return {
now: Date.now(),
current: web.current,
deploys: web.deploys,
current: parseInt(deploy.config.deploy.ts),
deploys: deploys.map((e) => parseInt(e.replace(".gz", ""))),
};
}
break;
+76 -3
View File
@@ -2,6 +2,7 @@ import { readAsync } from "fs-jetpack";
import { apiContext } from "service-srv";
import { g } from "utils/global";
import { dir } from "utils/dir";
import { gzipAsync } from "utils/gzip";
const generated = {
"load.json": "",
@@ -14,11 +15,83 @@ export const _ = {
async api() {
const { req, res } = apiContext(this);
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "content-type");
const gz = g.deploy.gz;
const parts = req.params._.split("/");
const action = {
_: () => {
res.send({ prasi: "v2" });
},
route: async () => {
if (gz) {
let layout = null as any;
for (const l of gz.layouts) {
if (!layout) layout = l;
if (l.is_default_layout) layout = l;
}
const result = await gzipAsync(
JSON.stringify({
site: { ...gz.site, api_url: (gz.site as any)?.config?.api_url },
urls: gz.pages.map((e) => {
return { id: e.id, url: e.url };
}),
layout,
})
);
return new Response(result, { headers: res.headers });
}
},
page: async () => {
const page = g.deploy.pages[parts[1]];
if (page) {
const result = await gzipAsync(
JSON.stringify({
id: page.id,
root: page.content_tree,
url: page.url,
})
);
return new Response(result, { headers: res.headers });
}
},
pages: async () => {
const pages = [];
if (req.params.ids) {
for (const id of req.params.ids) {
const page = g.deploy.pages[id];
if (page) {
pages.push({
id: page.id,
root: page.content_tree,
url: page.url,
});
}
}
}
return new Response(await gzipAsync(JSON.stringify(pages)), {
headers: res.headers,
});
},
comp: async () => {
const comps = {} as Record<string, any>;
if (req.params.ids) {
for (const id of req.params.ids) {
const comp = g.deploy.comps[id];
if (comp) {
comps[id] = comp;
}
}
}
return new Response(await gzipAsync(JSON.stringify(comps)), {
headers: res.headers,
});
},
"load.json": async () => {
res.setHeader("content-type", "application/json");
res.send(
@@ -47,11 +120,11 @@ export const _ = {
},
};
const pathname: keyof typeof action = req.params._.split("/")[0] as any;
const pathname: keyof typeof action = parts[0] as any;
const run = action[pathname];
if (run) {
await run();
return await run();
}
},
};
@@ -112,7 +185,7 @@ const getContent = async (type: keyof typeof generated, url?: string) => {
}
w.prasiApi[url] = {
apiEntry: ${JSON.stringify(getApiEntry())},
});
}
})();`;
}
return generated[type];