From 3e0cd161f07be87f11cd51d6c59f1660e8d58aee Mon Sep 17 00:00:00 2001 From: Rizky Date: Thu, 9 Nov 2023 16:09:02 +0700 Subject: [PATCH] prepare code --- app/db/prisma/schema.prisma | 18 ++++++ app/srv/ws/sync/actions-def.ts | 70 +++++++++++------------- app/srv/ws/sync/actions.ts | 4 -- app/srv/ws/sync/actions/activity.ts | 10 +++- app/srv/ws/sync/actions/code_close.ts | 11 ---- app/srv/ws/sync/actions/code_open.ts | 10 ---- app/srv/ws/sync/actions/index.ts | 2 - app/srv/ws/sync/editor/code/dev.ts | 45 +++++++++++++++ app/srv/ws/sync/editor/code/prep-code.ts | 53 ++++++++++++++++++ app/srv/ws/sync/editor/prep-code.ts | 6 -- 10 files changed, 155 insertions(+), 74 deletions(-) delete mode 100644 app/srv/ws/sync/actions/code_close.ts delete mode 100644 app/srv/ws/sync/actions/code_open.ts create mode 100644 app/srv/ws/sync/editor/code/dev.ts create mode 100644 app/srv/ws/sync/editor/code/prep-code.ts delete mode 100644 app/srv/ws/sync/editor/prep-code.ts diff --git a/app/db/prisma/schema.prisma b/app/db/prisma/schema.prisma index 35102d4e..7c5fddd3 100644 --- a/app/db/prisma/schema.prisma +++ b/app/db/prisma/schema.prisma @@ -223,6 +223,7 @@ model site { is_deleted Boolean @default(false) responsive String @default("all") npm_cache String @default(" ") @db.VarChar + code code[] component_site component_site[] npm_site npm_site[] page page[] @@ -275,3 +276,20 @@ model user { org_user org_user[] site site[] } + +model code { + id String @id(map: "code_id") @default(dbgenerated("gen_random_uuid()")) @db.Uuid + id_site String @db.Uuid + name String @default("site") + site site @relation(fields: [id_site], references: [id], onDelete: NoAction, onUpdate: NoAction) + code_file code_file[] +} + +model code_file { + path String + content String + id_code String @db.Uuid + code code @relation(fields: [id_code], references: [id], onDelete: NoAction, onUpdate: NoAction) + + @@id([path, id_code], map: "id_code_file") +} diff --git a/app/srv/ws/sync/actions-def.ts b/app/srv/ws/sync/actions-def.ts index 96d405cb..0c588fe7 100644 --- a/app/srv/ws/sync/actions-def.ts +++ b/app/srv/ws/sync/actions-def.ts @@ -1,52 +1,46 @@ export const SyncActionDefinition = { - "code": { - "open": "0", - "close": "1" - }, "site": { - "list": "2", - "group": "3", - "load": "4", - "update": "5" + "list": "0", + "group": "1", + "load": "2", + "update": "3" }, "comp": { - "new": "6", - "list": "7", - "group": "8", - "load": "9" + "new": "4", + "list": "5", + "group": "6", + "load": "7" }, "page": { - "list": "10", - "load": "11" + "list": "8", + "load": "9" }, "yjs": { - "um": "12", - "sv_local": "13", - "diff_local": "14", - "sv_remote": "15" + "um": "10", + "sv_local": "11", + "diff_local": "12", + "sv_remote": "13" }, - "activity": "16", + "activity": "14", "client": { - "info": "17" + "info": "15" } }; export const SyncActionPaths = { - "0": "code.open", - "1": "code.close", - "2": "site.list", - "3": "site.group", - "4": "site.load", - "5": "site.update", - "6": "comp.new", - "7": "comp.list", - "8": "comp.group", - "9": "comp.load", - "10": "page.list", - "11": "page.load", - "12": "yjs.um", - "13": "yjs.sv_local", - "14": "yjs.diff_local", - "15": "yjs.sv_remote", - "16": "activity", - "17": "client.info" + "0": "site.list", + "1": "site.group", + "2": "site.load", + "3": "site.update", + "4": "comp.new", + "5": "comp.list", + "6": "comp.group", + "7": "comp.load", + "8": "page.list", + "9": "page.load", + "10": "yjs.um", + "11": "yjs.sv_local", + "12": "yjs.diff_local", + "13": "yjs.sv_remote", + "14": "activity", + "15": "client.info" }; diff --git a/app/srv/ws/sync/actions.ts b/app/srv/ws/sync/actions.ts index 1b4bf1cc..40f79a90 100644 --- a/app/srv/ws/sync/actions.ts +++ b/app/srv/ws/sync/actions.ts @@ -17,10 +17,6 @@ import { activity } from "./entity/activity"; export type SAction = typeof SyncActions; export const SyncActions = { - code: { - open: async (id_site: string) => ({}) as { id: string }, - close: async (id_site: string) => ({}) as { id: string }, - }, site: { list: async () => ({}) as Record, diff --git a/app/srv/ws/sync/actions/activity.ts b/app/srv/ws/sync/actions/activity.ts index 6e64d1a5..07d913df 100644 --- a/app/srv/ws/sync/actions/activity.ts +++ b/app/srv/ws/sync/actions/activity.ts @@ -1,7 +1,8 @@ import { SAction } from "../actions"; import { SyncConnection } from "../type"; import { activity as a } from "../entity/activity"; -import { prepCode } from "../editor/prep-code"; +import { prepCode } from "../editor/code/prep-code"; +import { startCodeWatcher } from "../editor/code/dev"; export const activity: SAction["activity"] = async function ( this: SyncConnection, name, @@ -13,10 +14,13 @@ export const activity: SAction["activity"] = async function ( a.site.set(act.id, this.ws, async (data) => { if (act.action === "open") { data.site_js = act.name; - await prepCode(act.id, act.name); + const code = await prepCode(act.id, act.name); + if (code) { + await startCodeWatcher(code); + } } else { delete data.site_js; - } + } return data; }); } diff --git a/app/srv/ws/sync/actions/code_close.ts b/app/srv/ws/sync/actions/code_close.ts deleted file mode 100644 index 26b7ff02..00000000 --- a/app/srv/ws/sync/actions/code_close.ts +++ /dev/null @@ -1,11 +0,0 @@ - -import { SAction } from "../actions"; -import { SyncConnection } from "../type"; -export const code_close: SAction["code"]["close"] = async function ( - this: SyncConnection, -) { - let result = null as unknown as Awaited< - ReturnType - >; - return result; -} \ No newline at end of file diff --git a/app/srv/ws/sync/actions/code_open.ts b/app/srv/ws/sync/actions/code_open.ts deleted file mode 100644 index 3a2387f3..00000000 --- a/app/srv/ws/sync/actions/code_open.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { activity } from "."; -import { SAction } from "../actions"; -import { SyncConnection } from "../type"; -export const code_open: SAction["code"]["open"] = async function ( - this: SyncConnection -) { - let result = null as unknown as Awaited>; - - return result; -}; diff --git a/app/srv/ws/sync/actions/index.ts b/app/srv/ws/sync/actions/index.ts index b63cf3b0..0fe4ab44 100644 --- a/app/srv/ws/sync/actions/index.ts +++ b/app/srv/ws/sync/actions/index.ts @@ -1,5 +1,3 @@ -export * from "./code_open"; -export * from "./code_close"; export * from "./site_list"; export * from "./site_group"; export * from "./site_load"; diff --git a/app/srv/ws/sync/editor/code/dev.ts b/app/srv/ws/sync/editor/code/dev.ts new file mode 100644 index 00000000..651ede78 --- /dev/null +++ b/app/srv/ws/sync/editor/code/dev.ts @@ -0,0 +1,45 @@ +import { watch } from "fs"; +import { DBCode } from "./prep-code"; +import { dir } from "dir"; +import { g } from "utils/global"; +import { dirAsync } from "fs-jetpack"; +import { dirname } from "path"; +import { spawn } from "bun"; + +export const Code = { + watchers: {} as Record>, + path: (id: string, p?: string) => { + return dir.path(`${g.datadir}/code/${id}${p ? "/" + p : ""}`); + }, +}; + +export const startCodeWatcher = async (code: DBCode) => { + // if (Code.watchers[code.id]) { + // return; + // } + + for (const c of code.code_file) { + const path = Code.path(c.id_code, c.path); + + const file = Bun.file(path); + if (!(await file.exists())) { + await dirAsync(dirname(path)); + await Bun.write(file, c.content); + } + } + + await spawn({ + cmd: ["bun", "i"], + cwd: Code.path(code.id), + stderr: "ignore", + stdout: "ignore", + }).exited; + + Code.watchers[code.id] = watch( + Code.path(code.id), + { recursive: true }, + (event, path) => { + console.log(event, path); + } + ); +}; diff --git a/app/srv/ws/sync/editor/code/prep-code.ts b/app/srv/ws/sync/editor/code/prep-code.ts new file mode 100644 index 00000000..985253d0 --- /dev/null +++ b/app/srv/ws/sync/editor/code/prep-code.ts @@ -0,0 +1,53 @@ +export type DBCode = Exclude>, null>; + +export const prepCode = async (site_id: string, name: string) => { + let code = await getCode(site_id); + if (code) return code; + let new_code = await db.code.create({ + data: { + id_site: site_id, + }, + }); + + await db.code_file.create({ + data: { + id_code: new_code.id, + path: "index.tsx", + content: `\ +export const hello_world = () => { + console.log('hello world') +}`, + }, + }); + + await db.code_file.create({ + data: { + id_code: new_code.id, + path: "package.json", + content: JSON.stringify( + { + name: new_code.id, + dependencies: {}, + }, + null, + 2 + ), + }, + }); + + code = await getCode(site_id); + return code; +}; + +const getCode = async (site_id: string) => { + return await db.code.findFirst({ + where: { + id_site: site_id, + }, + select: { + id: true, + name: true, + code_file: true, + }, + }); +}; diff --git a/app/srv/ws/sync/editor/prep-code.ts b/app/srv/ws/sync/editor/prep-code.ts deleted file mode 100644 index 0074734d..00000000 --- a/app/srv/ws/sync/editor/prep-code.ts +++ /dev/null @@ -1,6 +0,0 @@ -export const prepCode = async (site_id: string, name: string) => { - // cek folder code - // pastikan struktur folder code - // create jika ga ada - // nyalain bun file watcher -};