From b60d2f77c6477a81254436f962f25edd3ab33d02 Mon Sep 17 00:00:00 2001 From: Rizky Date: Fri, 10 Nov 2023 10:39:06 +0700 Subject: [PATCH] fix --- app/srv/ws/handler.ts | 1 + app/srv/ws/sync/editor/code/build.ts | 3 ++ app/srv/ws/sync/editor/code/pkg-install.ts | 1 + app/srv/ws/sync/editor/code/prep-code.ts | 1 + app/srv/ws/sync/editor/code/watcher.ts | 57 +++++++++++++++++++-- app/srv/ws/sync/sync-handler.ts | 1 + app/web/src/render/ed/logic/ed-sync.tsx | 4 +- app/web/src/render/live/logic/mobile.ts | 1 + app/web/src/utils/sync/ws-client.ts | 6 +++ bun.lockb | Bin 306176 -> 306176 bytes package.json | 2 +- pkgs/core/server/create.ts | 2 +- 12 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 app/srv/ws/sync/editor/code/build.ts create mode 100644 app/srv/ws/sync/editor/code/pkg-install.ts diff --git a/app/srv/ws/handler.ts b/app/srv/ws/handler.ts index 29d07499..2d4b5402 100644 --- a/app/srv/ws/handler.ts +++ b/app/srv/ws/handler.ts @@ -10,6 +10,7 @@ eg.edit = { page: {}, ws: new WeakMap(), }; + export const wsHandler: Record> = { "/sync": syncHandler, "/edit": editHandler, diff --git a/app/srv/ws/sync/editor/code/build.ts b/app/srv/ws/sync/editor/code/build.ts new file mode 100644 index 00000000..54fa4c1d --- /dev/null +++ b/app/srv/ws/sync/editor/code/build.ts @@ -0,0 +1,3 @@ +import { build } from "bun"; + +export const codeBuild = (id_code: string) => {}; diff --git a/app/srv/ws/sync/editor/code/pkg-install.ts b/app/srv/ws/sync/editor/code/pkg-install.ts new file mode 100644 index 00000000..98c7d4a8 --- /dev/null +++ b/app/srv/ws/sync/editor/code/pkg-install.ts @@ -0,0 +1 @@ +export const codePkgInstall = (id_code: string) => {}; diff --git a/app/srv/ws/sync/editor/code/prep-code.ts b/app/srv/ws/sync/editor/code/prep-code.ts index 5664dc27..f5991b0c 100644 --- a/app/srv/ws/sync/editor/code/prep-code.ts +++ b/app/srv/ws/sync/editor/code/prep-code.ts @@ -66,6 +66,7 @@ const getCode = async (site_id: string) => { }, select: { id: true, + id_site: true, name: true, code_file: true, }, diff --git a/app/srv/ws/sync/editor/code/watcher.ts b/app/srv/ws/sync/editor/code/watcher.ts index 22d1fc7a..e47b8840 100644 --- a/app/srv/ws/sync/editor/code/watcher.ts +++ b/app/srv/ws/sync/editor/code/watcher.ts @@ -5,8 +5,12 @@ import { g } from "utils/global"; import { dirAsync } from "fs-jetpack"; import { dirname } from "path"; import { spawn } from "bun"; +import { activity } from "../../entity/activity"; +import { sendWS } from "../../sync-handler"; +import { SyncType } from "../../type"; export const Code = { + pkginstall: {} as Record, watchers: {} as Record< string, { id: string; watcher: ReturnType } @@ -16,6 +20,7 @@ export const Code = { }, }; +const decoder = new TextDecoder(); export const startCodeWatcher = async (code: DBCode) => { if (Code.watchers[code.id]) { return; @@ -55,19 +60,63 @@ export const startCodeWatcher = async (code: DBCode) => { async (event, path) => { if (path) { const file = Bun.file(Code.path(code.id, path)); - const item = indexes[path]; - if (event === "change") { if (item) { - console.log(path, await file.text()); + let content = await file.text(); + if (path === "package.json") { + try { + activity.site + .room(code.id_site) + .findAll({ site_js: code.name }) + .forEach((item, ws) => { + sendWS(ws, { + type: SyncType.Event, + evant: "code", + data: { + name: code.name, + id: code.id, + event: "pkg-install-start", + }, + }); + }); + + const proc = spawn({ + cmd: ["bun", "i"], + cwd: Code.path(code.id), + stderr: "pipe", + stdout: "pipe", + }); + const stdout = await Bun.readableStreamToText(proc.stdout); + const stderr = await Bun.readableStreamToText(proc.stderr); + await proc.exited; + + activity.site + .room(code.id_site) + .findAll({ site_js: code.name }) + .forEach((item, ws) => { + sendWS(ws, { + type: SyncType.Event, + evant: "code", + data: { + name: code.name, + id: code.id, + event: "pkg-install-end", + content: `${stdout}${ + !!stderr ? `\n\nERROR:\n ${stderr}` : "" + }`, + }, + }); + }); + } catch (e) {} + } await db.code_file.update({ where: { path_id_code: { id_code: item.id_code, path: item.path }, }, data: { - content: await file.text(), + content, }, }); } diff --git a/app/srv/ws/sync/sync-handler.ts b/app/srv/ws/sync/sync-handler.ts index c45a898c..b658b1d8 100644 --- a/app/srv/ws/sync/sync-handler.ts +++ b/app/srv/ws/sync/sync-handler.ts @@ -16,6 +16,7 @@ const packr = new Packr({ structuredClone: true }); export const sendWS = (ws: ServerWebSocket, msg: any) => { ws.sendBinary(packr.pack(msg)); }; + export const syncHandler: WebSocketHandler = { open(ws) { const client_id = createId(); diff --git a/app/web/src/render/ed/logic/ed-sync.tsx b/app/web/src/render/ed/logic/ed-sync.tsx index 0fa27f67..ae8baa91 100644 --- a/app/web/src/render/ed/logic/ed-sync.tsx +++ b/app/web/src/render/ed/logic/ed-sync.tsx @@ -62,8 +62,10 @@ export const edInitSync = (p: PG) => { site_id: params.site_id, page_id: params.page_id, events: { - activity(arg) { + code(arg) { + console.log(arg); }, + activity(arg) {}, opened() { if (w.offline) { console.log("reconnected!"); diff --git a/app/web/src/render/live/logic/mobile.ts b/app/web/src/render/live/logic/mobile.ts index 5c7ab09c..a4bb6c6d 100644 --- a/app/web/src/render/live/logic/mobile.ts +++ b/app/web/src/render/live/logic/mobile.ts @@ -54,6 +54,7 @@ export const registerMobile = () => { if (window.parent) { window.addEventListener("message", async ({ data: raw }) => { if (typeof raw === "object" && raw.mobile) { + console.log("capacitor", raw); const data = raw as unknown as | { type: "notification-token"; diff --git a/app/web/src/utils/sync/ws-client.ts b/app/web/src/utils/sync/ws-client.ts index a6d80d68..8d409920 100644 --- a/app/web/src/utils/sync/ws-client.ts +++ b/app/web/src/utils/sync/ws-client.ts @@ -75,6 +75,12 @@ export const clientStartSync = async (arg: { site_id?: string; page_id?: string; events: { + code: (arg: { + name: string; + id: string; + event: "pkg-install-start" | "pkg-isntall-end"; + content?: string; + }) => void; activity: (arg: { activity: string; room_id: string; diff --git a/bun.lockb b/bun.lockb index 7904c3421b797c2fdffb0209f00e946d1cc49c3e..67ed468aeb54aa3f88fc763786869a9a629eb6b9 100755 GIT binary patch delta 35 pcmZp;A=CgwEsQNpTbN^2rpGR0=A1qyfLWluOoe%SnF>o+I{@qq3|0UD delta 33 jcmZp;A=CgwEsQNpTbN^2*qIo>pgl!}d3%Zq%ba!qtE~wc diff --git a/package.json b/package.json index 83e42848..8a8cfe8c 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "module": "src/index.ts", "type": "module", "scripts": { - "dev": "bun run --hot --silent ./pkgs/core/index.ts dev", + "dev": "bun run --watch ./pkgs/core/index.ts dev", "clean": "rm -rf data && rm -rf app/static && rm -rf app/web/.parcel-cache", "build": "bun run --silent ./pkgs/core/build.ts", "build-site": "bun run --silent ./pkgs/core/build-site.ts", diff --git a/pkgs/core/server/create.ts b/pkgs/core/server/create.ts index 4cc24925..5b4afe78 100644 --- a/pkgs/core/server/create.ts +++ b/pkgs/core/server/create.ts @@ -17,7 +17,7 @@ export const cache = { }; export type WSData = { url: URL }; - + export const createServer = async () => { g.router = createRouter({ strictTrailingSlash: false });