This commit is contained in:
Rizky 2024-07-16 14:26:47 +07:00
parent 8a8288f00a
commit 0ea9a29eac
6 changed files with 81 additions and 34 deletions

View File

@ -1,9 +1,39 @@
import { dir } from "dir";
import { apiContext } from "service-srv"; import { apiContext } from "service-srv";
export const _ = { export const _ = {
url: "/local-files", url: "/local-files",
async api() { async api(arg: {
id_site: string;
mode: "list" | "write" | "read";
path: string;
content?: string;
}) {
const { req, res } = apiContext(this); const { req, res } = apiContext(this);
return "This is local-files.ts";
const { mode, id_site, path, content } = arg;
const root = dir.data(`/code/${id_site}/site/src`);
switch (mode) {
case "list": {
const glob = new Bun.Glob("**");
const files = [];
for await (const file of glob.scan(root + path)) {
files.push(file);
} }
} return files;
}
case "write": {
await Bun.write(root + path, content || "", { createPath: true });
}
case "read": {
try {
return await Bun.file(root + path).text();
} catch (e) {
return null;
}
}
}
return "ok";
},
};

View File

@ -130,7 +130,7 @@ export const initFrontEnd = async (
async (event, filename) => { async (event, filename) => {
const fe = code.internal.frontend[id_site]; const fe = code.internal.frontend[id_site];
const srv = code.internal.server[id_site]; const srv = code.internal.server[id_site];
if (filename?.startsWith("app/routes")) { if (filename?.startsWith("app/routes/")) {
prasiDefineLocalRoute(id_site); prasiDefineLocalRoute(id_site);
} }
if (filename?.startsWith("node_modules")) return; if (filename?.startsWith("node_modules")) return;

View File

@ -15,6 +15,8 @@ export const prasiDefineLocalRoute = (id_site: string) => {
for await (const f of glob.scan(path)) { for await (const f of glob.scan(path)) {
const file = Bun.file(path + `/${f}`); const file = Bun.file(path + `/${f}`);
if (file.size > 0) { if (file.size > 0) {
const source = await file.text();
if (source.includes("export const route =")) {
if (f.endsWith(".tsx")) { if (f.endsWith(".tsx")) {
imports.add(f.substring(0, f.length - 4)); imports.add(f.substring(0, f.length - 4));
} else if (f.endsWith(".ts")) { } else if (f.endsWith(".ts")) {
@ -22,8 +24,9 @@ export const prasiDefineLocalRoute = (id_site: string) => {
} }
} }
} }
}
const content = `\ let content = `\
${[...imports] ${[...imports]
.map( .map(
(e) => `import { route as ${e.replace(/\W/g, "_")} } from "./routes/${e}"` (e) => `import { route as ${e.replace(/\W/g, "_")} } from "./routes/${e}"`
@ -33,7 +36,9 @@ ${[...imports]
export const router = { export const router = {
${[...imports].map((e) => `${e.replace(/\W/g, "_")}`).join(",\n ")} ${[...imports].map((e) => `${e.replace(/\W/g, "_")}`).join(",\n ")}
} }
`; `.trim();
content = `// AUTOGENERATED - DO NOT EDIT //\n\n${content}`;
const hash = simpleHash(content); const hash = simpleHash(content);
const router = Bun.file( const router = Bun.file(
dir.data(`code/${id_site}/site/src/app/router.tsx`) dir.data(`code/${id_site}/site/src/app/router.tsx`)

View File

@ -9,7 +9,7 @@ import { Loading } from "../../utils/ui/loading";
export default page({ export default page({
url: "/ed/:site_id/:page_id", url: "/ed/:site_id/:page_id",
component: ({ }) => { component: ({}) => {
const p = useGlobal(EDGlobal, "EDITOR"); const p = useGlobal(EDGlobal, "EDITOR");
const local = useLocal({ const local = useLocal({
new_site: false, new_site: false,
@ -22,6 +22,20 @@ export default page({
(window as any).Y = await import("yjs"); (window as any).Y = await import("yjs");
(window as any).syncronize = (await import("y-pojo")).syncronize; (window as any).syncronize = (await import("y-pojo")).syncronize;
p.render(); p.render();
// const res = await _api.local_files({
// mode: "list",
// path: "/app",
// id_site: "53f19c29-a36b-48b1-b13a-25dcdaef8ea5",
// });
// console.log(res);
// await _api.local_files({
// mode: "write",
// path: "/app/routes/path.tsx",
// content: "haloha",
// id_site: "53f19c29-a36b-48b1-b13a-25dcdaef8ea5",
// });
})(); })();
return <Loading note="init" />; return <Loading note="init" />;
} }
@ -40,7 +54,7 @@ export default page({
location.href = `/ed/${data.id}/_`; location.href = `/ed/${data.id}/_`;
} }
}} }}
onClose={() => { }} onClose={() => {}}
header={ header={
<div className="border-b border-blue-500 text-xl"> <div className="border-b border-blue-500 text-xl">
Create New Site Create New Site

View File

@ -189,7 +189,6 @@ export const edInitSync = (p: PG) => {
if (mode === "frontend") { if (mode === "frontend") {
await loadFrontEnd(p, ts); await loadFrontEnd(p, ts);
} else { } else {
console.log("Code updated");
await loadTypings(p); await loadTypings(p);
if (p.ui.monaco) { if (p.ui.monaco) {
registerSiteTypings(p.ui.monaco, p); registerSiteTypings(p.ui.monaco, p);

View File

@ -1,13 +1,12 @@
import { createId } from "@paralleldrive/cuid2";
import { FC, useEffect } from "react"; import { FC, useEffect } from "react";
import { FMCompDef, FNCompDef } from "../../../../../utils/types/meta-fn";
import { useGlobal, useLocal } from "web-utils"; import { useGlobal, useLocal } from "web-utils";
import { TypedMap } from "yjs-types"; import { TypedMap } from "yjs-types";
import { createEditScript } from "./edit-script";
import { EDGlobal } from "../../../logic/ed-global";
import { createId } from "@paralleldrive/cuid2";
import { IItem, MItem } from "../../../../../utils/types/item"; import { IItem, MItem } from "../../../../../utils/types/item";
import { MContent } from "../../../../../utils/types/general"; import { FMCompDef } from "../../../../../utils/types/meta-fn";
import { EDGlobal } from "../../../logic/ed-global";
import { fillID } from "../../../logic/tree/fill-id"; import { fillID } from "../../../logic/tree/fill-id";
import { createEditScript } from "./edit-script";
export const propPopover = { export const propPopover = {
name: "", name: "",