This commit is contained in:
Rizky 2024-04-29 19:13:33 +07:00
parent ee39e5d5db
commit 4b126d29f8
3 changed files with 19 additions and 195 deletions

View File

@ -39,7 +39,14 @@ export const _ = {
comps: await _db.component.findMany({ comps: await _db.component.findMany({
where: { where: {
component_group: { component_group: {
component_site: { some: { id_site: site_id } }, OR: [
{
id: "13143272-d4e3-4301-b790-2b3fd3e524e6",
},
{
component_site: { some: { id_site: site_id } },
},
],
}, },
}, },
select: { id: true, content_tree: true }, select: { id: true, content_tree: true },

View File

@ -1,61 +1,9 @@
import { apiContext } from "../../../pkgs/core/server/api/api-ctx"; import { apiContext } from "service-srv";
import ts from "typescript";
import { createHash } from "crypto";
const dts = {} as Record<string, { etag: string; dts: string }>;
export const _ = { export const _ = {
url: "/site-dts/:site_id", url: "/site-dts",
async api(site_id: string) { async api() {
const { req, res } = apiContext(this); const { req, res } = apiContext(this);
return "This is site-dts.ts";
let sent = false; }
if (dts[site_id]) { }
res.setHeader("etag", dts[site_id].etag);
if (dts[site_id].etag === req.headers.get("if-none-match")) {
res.sendStatus(304);
sent = true;
}
if (!sent) {
sent = true;
res.send(dts[site_id].dts);
}
}
// const site = await _db.site.findFirst({
// where: { id: site_id },
// select: { js: true },
// });
// if (site && site.js) {
// const options = {
// emitDeclarationOnly: true,
// declaration: true,
// };
// let dtsrc = "";
// const host = ts.createCompilerHost(options);
// host.writeFile = (fileName, contents) => (dtsrc = contents);
// host.readFile = () =>
// (site.js || "") + "\n\nexport const ______: string;";
// const program = ts.createProgram(["sitedts"], options, host);
// program.emit();
// const etag = createHash("md5").update(dtsrc).digest("hex");
// if (!sent) res.setHeader("etag", etag);
// dts[site_id] = { etag, dts: dtsrc };
// if (sent) {
// return "";
// }
// return dtsrc;
// }
res.setHeader("etag", "empty");
return "";
},
};

View File

@ -1,140 +1,9 @@
import { apiContext } from "../../../pkgs/core/server/api/api-ctx"; import { apiContext } from "service-srv";
import { dir } from "dir";
import fs from "fs";
import { exists } from "fs-jetpack";
import path from "path";
import { gzipSync } from "zlib";
import { buildNpm } from "../util/build-npm";
export const _ = { export const _ = {
url: "/site-export/:site_id", url: "/site-export",
async api(site_id: string) { async api() {
const { req, res } = apiContext(this); const { req, res } = apiContext(this);
const site = (await _db.site.findFirst({ return "This is site-export.ts";
where: { id: site_id },
})) as any;
const pages = await _db.page.findMany({
where: {
id_site: site_id,
is_deleted: false,
name: { not: { startsWith: "layout:" } },
},
});
if (site) {
const layout = await _db.page.findFirst({
where: {
id_site: site.id,
name: { startsWith: "layout:" },
is_default_layout: true,
is_deleted: false,
},
select: { content_tree: true, id: true },
});
const cgroups = await _db.site_use_comp.findMany({
where: { id_site: site.id },
});
if (Array.isArray(cgroups)) {
site.cgroup_ids = [];
for (const id of cgroups.map((c) => c.use_id_site)) {
site.cgroup_ids.push(id);
}
}
if (layout) {
const childs = (layout.content_tree as any).childs;
if (childs && childs.length > 0) {
(site as any).layout = childs[0];
(site as any).layout_id = layout.id;
}
}
}
const comps = await _db.component.findMany({
where: {
component_group: {
component_site: {
some: {
id_site: site_id,
},
},
},
},
});
const npm = {
site: {} as Record<string, string>,
pages: {} as Record<string, Record<string, string>>,
};
const page_ids = await _db.page.findMany({
where: { id_site: site_id, is_deleted: false },
select: { id: true },
});
const npm_page = await _db.npm_page.findMany({
where: { id_page: { in: page_ids.map((e) => e.id) } },
});
if (!exists(dir.data(`/npm/site/${site_id}`))) {
await buildNpm({ id: site_id, mode: "site" });
}
const npm_page_ids = {} as Record<string, any[]>;
for (const np of npm_page) {
if (!npm_page_ids[np.id_page]) {
npm_page_ids[np.id_page] = [];
}
npm_page_ids[np.id_page].push(np);
}
for (const [k, v] of Object.entries(npm_page_ids)) {
if (!exists(dir.data(`/npm/page/${k}`))) {
await buildNpm({ id: k, mode: "page", _items: v });
}
}
npm.site = readDirectoryRecursively(
dir.data(`/npm/site/${site_id}`)
);
for (const page of pages) {
if (exists(dir.data(`/npm/page/${page.id}`))) {
npm.pages[page.id] = readDirectoryRecursively(
dir.data(`/npm/page/${page.id}`)
);
}
}
const str = gzipSync(JSON.stringify({ site, pages, npm, comps }));
res.send(str);
},
};
export function readDirectoryRecursively(
dirPath: string,
baseDir?: string[]
): Record<string, string> {
const result: Record<string, string> = {};
const contents = fs.readdirSync(dirPath);
for (const item of contents) {
const itemPath = path.join(dirPath, item);
const stats = fs.statSync(itemPath);
if (stats.isFile()) {
const content = fs.readFileSync(itemPath, "utf-8");
result[[...(baseDir || []), item].join("/")] = content;
} else if (stats.isDirectory()) {
if (item !== "node_modules") {
const subdirResult = readDirectoryRecursively(itemPath, [
...(baseDir || []),
item,
]);
Object.assign(result, subdirResult);
}
}
} }
}
return result;
}