This commit is contained in:
Rizky 2023-11-10 10:39:06 +07:00
parent ac34579b84
commit b60d2f77c6
12 changed files with 72 additions and 7 deletions

View File

@ -10,6 +10,7 @@ eg.edit = {
page: {}, page: {},
ws: new WeakMap(), ws: new WeakMap(),
}; };
export const wsHandler: Record<string, WebSocketHandler<WSData>> = { export const wsHandler: Record<string, WebSocketHandler<WSData>> = {
"/sync": syncHandler, "/sync": syncHandler,
"/edit": editHandler, "/edit": editHandler,

View File

@ -0,0 +1,3 @@
import { build } from "bun";
export const codeBuild = (id_code: string) => {};

View File

@ -0,0 +1 @@
export const codePkgInstall = (id_code: string) => {};

View File

@ -66,6 +66,7 @@ const getCode = async (site_id: string) => {
}, },
select: { select: {
id: true, id: true,
id_site: true,
name: true, name: true,
code_file: true, code_file: true,
}, },

View File

@ -5,8 +5,12 @@ import { g } from "utils/global";
import { dirAsync } from "fs-jetpack"; import { dirAsync } from "fs-jetpack";
import { dirname } from "path"; import { dirname } from "path";
import { spawn } from "bun"; import { spawn } from "bun";
import { activity } from "../../entity/activity";
import { sendWS } from "../../sync-handler";
import { SyncType } from "../../type";
export const Code = { export const Code = {
pkginstall: {} as Record<string, true>,
watchers: {} as Record< watchers: {} as Record<
string, string,
{ id: string; watcher: ReturnType<typeof watch> } { id: string; watcher: ReturnType<typeof watch> }
@ -16,6 +20,7 @@ export const Code = {
}, },
}; };
const decoder = new TextDecoder();
export const startCodeWatcher = async (code: DBCode) => { export const startCodeWatcher = async (code: DBCode) => {
if (Code.watchers[code.id]) { if (Code.watchers[code.id]) {
return; return;
@ -55,19 +60,63 @@ export const startCodeWatcher = async (code: DBCode) => {
async (event, path) => { async (event, path) => {
if (path) { if (path) {
const file = Bun.file(Code.path(code.id, path)); const file = Bun.file(Code.path(code.id, path));
const item = indexes[path]; const item = indexes[path];
if (event === "change") { if (event === "change") {
if (item) { 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({ await db.code_file.update({
where: { where: {
path_id_code: { id_code: item.id_code, path: item.path }, path_id_code: { id_code: item.id_code, path: item.path },
}, },
data: { data: {
content: await file.text(), content,
}, },
}); });
} }

View File

@ -16,6 +16,7 @@ const packr = new Packr({ structuredClone: true });
export const sendWS = (ws: ServerWebSocket<WSData>, msg: any) => { export const sendWS = (ws: ServerWebSocket<WSData>, msg: any) => {
ws.sendBinary(packr.pack(msg)); ws.sendBinary(packr.pack(msg));
}; };
export const syncHandler: WebSocketHandler<WSData> = { export const syncHandler: WebSocketHandler<WSData> = {
open(ws) { open(ws) {
const client_id = createId(); const client_id = createId();

View File

@ -62,8 +62,10 @@ export const edInitSync = (p: PG) => {
site_id: params.site_id, site_id: params.site_id,
page_id: params.page_id, page_id: params.page_id,
events: { events: {
activity(arg) { code(arg) {
console.log(arg);
}, },
activity(arg) {},
opened() { opened() {
if (w.offline) { if (w.offline) {
console.log("reconnected!"); console.log("reconnected!");

View File

@ -54,6 +54,7 @@ export const registerMobile = () => {
if (window.parent) { if (window.parent) {
window.addEventListener("message", async ({ data: raw }) => { window.addEventListener("message", async ({ data: raw }) => {
if (typeof raw === "object" && raw.mobile) { if (typeof raw === "object" && raw.mobile) {
console.log("capacitor", raw);
const data = raw as unknown as const data = raw as unknown as
| { | {
type: "notification-token"; type: "notification-token";

View File

@ -75,6 +75,12 @@ export const clientStartSync = async (arg: {
site_id?: string; site_id?: string;
page_id?: string; page_id?: string;
events: { events: {
code: (arg: {
name: string;
id: string;
event: "pkg-install-start" | "pkg-isntall-end";
content?: string;
}) => void;
activity: (arg: { activity: (arg: {
activity: string; activity: string;
room_id: string; room_id: string;

BIN
bun.lockb

Binary file not shown.

View File

@ -3,7 +3,7 @@
"module": "src/index.ts", "module": "src/index.ts",
"type": "module", "type": "module",
"scripts": { "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", "clean": "rm -rf data && rm -rf app/static && rm -rf app/web/.parcel-cache",
"build": "bun run --silent ./pkgs/core/build.ts", "build": "bun run --silent ./pkgs/core/build.ts",
"build-site": "bun run --silent ./pkgs/core/build-site.ts", "build-site": "bun run --silent ./pkgs/core/build-site.ts",

View File

@ -17,7 +17,7 @@ export const cache = {
}; };
export type WSData = { url: URL }; export type WSData = { url: URL };
export const createServer = async () => { export const createServer = async () => {
g.router = createRouter({ strictTrailingSlash: false }); g.router = createRouter({ strictTrailingSlash: false });