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";
export const _ = {
url: "/local-files",
async api() {
async api(arg: {
id_site: string;
mode: "list" | "write" | "read";
path: string;
content?: string;
}) {
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) => {
const fe = code.internal.frontend[id_site];
const srv = code.internal.server[id_site];
if (filename?.startsWith("app/routes")) {
if (filename?.startsWith("app/routes/")) {
prasiDefineLocalRoute(id_site);
}
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)) {
const file = Bun.file(path + `/${f}`);
if (file.size > 0) {
const source = await file.text();
if (source.includes("export const route =")) {
if (f.endsWith(".tsx")) {
imports.add(f.substring(0, f.length - 4));
} else if (f.endsWith(".ts")) {
@ -22,8 +24,9 @@ export const prasiDefineLocalRoute = (id_site: string) => {
}
}
}
}
const content = `\
let content = `\
${[...imports]
.map(
(e) => `import { route as ${e.replace(/\W/g, "_")} } from "./routes/${e}"`
@ -33,7 +36,9 @@ ${[...imports]
export const router = {
${[...imports].map((e) => `${e.replace(/\W/g, "_")}`).join(",\n ")}
}
`;
`.trim();
content = `// AUTOGENERATED - DO NOT EDIT //\n\n${content}`;
const hash = simpleHash(content);
const router = Bun.file(
dir.data(`code/${id_site}/site/src/app/router.tsx`)

View File

@ -22,6 +22,20 @@ export default page({
(window as any).Y = await import("yjs");
(window as any).syncronize = (await import("y-pojo")).syncronize;
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" />;
}

View File

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

View File

@ -1,13 +1,12 @@
import { createId } from "@paralleldrive/cuid2";
import { FC, useEffect } from "react";
import { FMCompDef, FNCompDef } from "../../../../../utils/types/meta-fn";
import { useGlobal, useLocal } from "web-utils";
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 { 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 { createEditScript } from "./edit-script";
export const propPopover = {
name: "",