diff --git a/app/srv/api/local-files.ts b/app/srv/api/local-files.ts
index 40eab891..23b87840 100644
--- a/app/srv/api/local-files.ts
+++ b/app/srv/api/local-files.ts
@@ -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";
- }
-}
\ No newline at end of file
+
+ 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";
+ },
+};
diff --git a/app/srv/ws/sync/code/parts/init/frontend.ts b/app/srv/ws/sync/code/parts/init/frontend.ts
index c6ea2e56..5bd683d7 100644
--- a/app/srv/ws/sync/code/parts/init/frontend.ts
+++ b/app/srv/ws/sync/code/parts/init/frontend.ts
@@ -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;
diff --git a/app/srv/ws/sync/code/utlis/local-routers.ts b/app/srv/ws/sync/code/utlis/local-routers.ts
index 6db2063d..fefdbd05 100644
--- a/app/srv/ws/sync/code/utlis/local-routers.ts
+++ b/app/srv/ws/sync/code/utlis/local-routers.ts
@@ -15,15 +15,18 @@ export const prasiDefineLocalRoute = (id_site: string) => {
for await (const f of glob.scan(path)) {
const file = Bun.file(path + `/${f}`);
if (file.size > 0) {
- if (f.endsWith(".tsx")) {
- imports.add(f.substring(0, f.length - 4));
- } else if (f.endsWith(".ts")) {
- imports.add(f.substring(0, f.length - 3));
+ 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")) {
+ imports.add(f.substring(0, f.length - 3));
+ }
}
}
}
- 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`)
diff --git a/app/web/src/base/page/ed.tsx b/app/web/src/base/page/ed.tsx
index 6218e7bb..8dbe2cd9 100644
--- a/app/web/src/base/page/ed.tsx
+++ b/app/web/src/base/page/ed.tsx
@@ -9,7 +9,7 @@ import { Loading } from "../../utils/ui/loading";
export default page({
url: "/ed/:site_id/:page_id",
- component: ({ }) => {
+ component: ({}) => {
const p = useGlobal(EDGlobal, "EDITOR");
const local = useLocal({
new_site: false,
@@ -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