From 30c89dc6173320538222703f4bf357b13436a8bf Mon Sep 17 00:00:00 2001 From: Rizky Date: Tue, 23 Jan 2024 05:52:01 +0700 Subject: [PATCH] wip prep-d-code --- app/srv/ws/sync/editor/code/prep-code.ts | 246 ++++++++++++----------- 1 file changed, 125 insertions(+), 121 deletions(-) diff --git a/app/srv/ws/sync/editor/code/prep-code.ts b/app/srv/ws/sync/editor/code/prep-code.ts index 45ea7f9f..28cd70c1 100644 --- a/app/srv/ws/sync/editor/code/prep-code.ts +++ b/app/srv/ws/sync/editor/code/prep-code.ts @@ -10,153 +10,157 @@ import { snapshot } from "../../entity/snapshot"; export type DBCode = Exclude>, null>; export const prepCode = async (site_id: string, name: string) => { - let code = await getCode(site_id, name); + let code = await getCode(site_id, name); - const pkgfile = Bun.file( - dir.path(`${g.datadir}/site/code/${site_id}/package.json`) - ); - if (!(await pkgfile.exists())) { - await dirAsync(dir.path(`${g.datadir}/site/code/${site_id}`)); - await Bun.write( - pkgfile, - JSON.stringify( - { - name: "code", - workspaces: ["./*"], - }, - null, - 2 - ) - ); - } + const pkgfile = Bun.file( + dir.path(`${g.datadir}/site/code/${site_id}/package.json`), + ); + if (!(await pkgfile.exists())) { + await dirAsync(dir.path(`${g.datadir}/site/code/${site_id}`)); + await Bun.write( + pkgfile, + JSON.stringify( + { + name: "code", + workspaces: ["./*"], + }, + null, + 2, + ), + ); + } - if (code) { - await dirAsync(dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`)); + if (code) { + await dirAsync(dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`)); + await prepDCode(site_id); + return code; + } + let new_code = await db.code.create({ + data: { + id_site: site_id, + }, + }); - await prepDCode(site_id); - 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: `\ + 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 - ), - }, - }); + 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); + code = await getCode(site_id); - await prepDCode(site_id); + await prepDCode(site_id); - return code as DBCode; + return code as DBCode; }; export const getCode = async (site_id: string, name?: string) => { - return await db.code.findFirst({ - where: name - ? { id_site: site_id, name } - : { - id_site: site_id, - }, - select: { - id: true, - id_site: true, - name: true, - code_file: true, - }, - }); + return await db.code.findFirst({ + where: name + ? { id_site: site_id, name } + : { + id_site: site_id, + }, + select: { + id: true, + id_site: true, + name: true, + code_file: true, + }, + }); }; export const prepDCode = async (site_id: string) => { - let exists = false; - if (!docs.code[site_id]) { - const code = await getCode(site_id, "site"); + let exists = false; + if (!docs.code[site_id]) { + const code = await getCode(site_id, "site"); - if (code) { - const path = { - src: dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`), - build: dir.path(`${g.datadir}/site/build/${code.id}`), - }; + if (code) { + const path = { + src: dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`), + build: dir.path(`${g.datadir}/site/build/${code.id}`), + }; - if ((await existsAsync(path.src)) && (await existsAsync(path.build))) { - docs.code[site_id] = { - id: site_id, - src: loadFolderAsDCode(code.id, path.src), - build: loadFolderAsDCode(code.id, path.build), - }; - exists = true; - } - } - } + if ((await existsAsync(path.src)) && (await existsAsync(path.build))) { + docs.code[site_id] = { + id: site_id, + src: loadFolderAsDCode(code.id, path.src), + build: loadFolderAsDCode(code.id, path.build), + }; + exists = true; + } + } + } - if (exists) { - const src_bin = Y.encodeStateAsUpdate(docs.code[site_id].src as Doc); - const build_bin = Y.encodeStateAsUpdate(docs.code[site_id].build as Doc); + if (exists) { + Bun.spawn({ + cmd: ["chmod", "777", "-R", "."], + cwd: dir.path(`${g.datadir}/site}`), + }); - let snap = await snapshot.getOrCreate({ - type: "site", - id: site_id, - name: "", - src: { - bin: src_bin, - id_doc: docs.code[site_id].src.clientID, - }, - build: { - bin: build_bin, - id_doc: docs.code[site_id].build.clientID, - }, - }); + const src_bin = Y.encodeStateAsUpdate(docs.code[site_id].src as Doc); + const build_bin = Y.encodeStateAsUpdate(docs.code[site_id].build as Doc); - if (snap && snap.type === "site") { - return { - bin: { - src: snap.src.bin, - build: snap.build.bin, - }, - }; - } - } + let snap = await snapshot.getOrCreate({ + type: "site", + id: site_id, + name: "", + src: { + bin: src_bin, + id_doc: docs.code[site_id].src.clientID, + }, + build: { + bin: build_bin, + id_doc: docs.code[site_id].build.clientID, + }, + }); + + if (snap && snap.type === "site") { + return { + bin: { + src: snap.src.bin, + build: snap.build.bin, + }, + }; + } + } }; const loadFolderAsDCode = (id: string, path: string) => { - const doc = new Y.Doc() as DCode; - const map = doc.getMap("map"); + const doc = new Y.Doc() as DCode; + const map = doc.getMap("map"); - const files = new Y.Map(); + const files = new Y.Map(); - const dirs = readDirectoryRecursively(path); - for (const [k, v] of Object.entries(dirs)) { - files.set(k, new Y.Text(v)); - } + const dirs = readDirectoryRecursively(path); + for (const [k, v] of Object.entries(dirs)) { + files.set(k, new Y.Text(v)); + } - doc.transact(() => { - map.set("files", files as any); - map.set("id", id); - }); + doc.transact(() => { + map.set("files", files as any); + map.set("id", id); + }); - return doc; + return doc; };