prepare code

This commit is contained in:
Rizky 2023-11-09 16:09:02 +07:00
parent f1871e21bb
commit 3e0cd161f0
10 changed files with 155 additions and 74 deletions

View File

@ -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")
}

View File

@ -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"
};

View File

@ -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<string, { id: string; name: string; domain: string }>,

View File

@ -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,7 +14,10 @@ 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;
}

View File

@ -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<SAction["code"]["close"]>
>;
return result;
}

View File

@ -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<ReturnType<SAction["code"]["open"]>>;
return result;
};

View File

@ -1,5 +1,3 @@
export * from "./code_open";
export * from "./code_close";
export * from "./site_list";
export * from "./site_group";
export * from "./site_load";

View File

@ -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<string, ReturnType<typeof watch>>,
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);
}
);
};

View File

@ -0,0 +1,53 @@
export type DBCode = Exclude<Awaited<ReturnType<typeof getCode>>, 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,
},
});
};

View File

@ -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
};