diff --git a/app/srv/ws/sync/code/code.ts b/app/srv/ws/sync/code/code.ts index d3187880..1920c086 100644 --- a/app/srv/ws/sync/code/code.ts +++ b/app/srv/ws/sync/code/code.ts @@ -9,14 +9,13 @@ import { ensureFiles } from "./utlis/ensure-files"; export const code = { internal: codeInternal, async init(id_site: string, note: string) { - const { frontend, server, typings } = this.internal; const root = `/code/${id_site}/site/src`; - await initFrontEnd(root, id_site); - await initServer(root, id_site); - await ensureLib(root, id_site); await ensureFiles(root, id_site); + + await initFrontEnd(root, id_site); + await initServer(root, id_site); }, path( id_site: string, diff --git a/app/srv/ws/sync/code/parts/init/frontend.ts b/app/srv/ws/sync/code/parts/init/frontend.ts index c4dde987..071f0c2d 100644 --- a/app/srv/ws/sync/code/parts/init/frontend.ts +++ b/app/srv/ws/sync/code/parts/init/frontend.ts @@ -6,9 +6,9 @@ import { removeAsync } from "fs-jetpack"; import isEqual from "lodash.isequal"; import { code } from "../../code"; import { buildTypes } from "./typings"; +import { appendFile } from "node:fs/promises"; -const npm_list = {} as Record>; - +const decoder = new TextDecoder(); export const initFrontEnd = async (root: string, id_site: string) => { let existing = code.internal.frontend[id_site]; @@ -50,87 +50,8 @@ export const initFrontEnd = async (root: string, id_site: string) => { }), { name: "prasi", - setup(setup) { + async setup(setup) { try { - setup.onEnd(async (res) => { - if (!npm_list[id_site]) npm_list[id_site] = new Set(); - const imports = new Set(); - if (!(await isInstalling(id_site))) - await codeError(id_site, ""); - - if (res.errors.length > 0) { - for (const err of res.errors) { - if ( - err.notes?.[0].text.startsWith("You can mark the path ") - ) { - let im = err.notes?.[0].text.split('"')[1]; - - if (!im.startsWith("@")) { - im = im.split("/").shift() || ""; - } - - imports.add(im); - } - } - } - - if (res.metafile) { - for (const [_, file] of Object.entries( - res.metafile?.inputs || {} - )) { - for (const im of file.imports) { - if (im.kind === "import-statement" && im.external) { - if ( - !im.path.startsWith(".") && - !im.path.startsWith("@/") - ) - imports.add(im.path); - } - } - } - } - - if (!isEqual(imports, npm_list[id_site])) { - await codeError( - id_site, - "Installing dependencies:\n " + [...imports].join("\n ") - ); - npm_list[id_site] = imports; - let proc = Bun.spawn( - [`npm`, `install`, "--silent", ...imports], - { - stdio: ["inherit", "pipe", "pipe"], - cwd: dir.data(root), - } - ); - - async function print(generator: any, prefix: any) { - for await (let value of generator) { - console.log(`${prefix} ${value}`); - await codeError(id_site, `${prefix} ${value}`); - } - } - - print(proc.stdout, "stdout:"); - print(proc.stderr, "stderr:"); - - await proc.exited; - await codeError(id_site, ""); - try { - await code.internal.frontend[id_site].rebuild(); - } catch (e) {} - return; - } - - if (res.errors.length > 0) { - await codeError( - id_site, - res.errors.map((e) => e.text).join("\n\n") - ); - } else { - buildTypes(root, id_site); - } - }); } catch (e) { console.log("ERROR"); } @@ -146,8 +67,13 @@ export const initFrontEnd = async (root: string, id_site: string) => { } }; -const codeError = async (id_site: string, error: string) => { +const codeError = async (id_site: string, error: string, append?: boolean) => { const path = code.path(id_site, "site", "src", "index.log"); + + if (append) { + await appendFile(path, error); + return; + } await Bun.write(path, error); }; @@ -160,3 +86,23 @@ const isInstalling = async (id_site: string) => { } catch (e) {} return false; }; + +const readPackageJSON = async (id_site: string) => { + const file = Bun.file(code.path(id_site, "site", "src", "package.json")); + const deps = new Set(); + + const json = await file.json(); + + if (json.dependencies) { + for (const k of Object.keys(json.dependencies)) { + deps.add(k); + } + } + + if (json.devDependencies) { + for (const k of Object.keys(json.devDependencies)) { + deps.add(k); + } + } + return deps; +}; diff --git a/app/srv/ws/sync/code/utlis/ensure-files.ts b/app/srv/ws/sync/code/utlis/ensure-files.ts index d6eafa5a..5dc61ea7 100644 --- a/app/srv/ws/sync/code/utlis/ensure-files.ts +++ b/app/srv/ws/sync/code/utlis/ensure-files.ts @@ -19,7 +19,8 @@ export const ensureFiles = async (path: string, id_site: string) => { const f = t.replaceAll("_", "."); const to = dir.data(path + `/${f}`); const file = Bun.file(to); - if (!(await file.exists())) { + const exists = await file.exists(); + if (!exists) { const from = dir.path(`${tdir}/${t}`); await dirAsync(dirname(to)); await copyAsync(from, to); diff --git a/app/srv/ws/sync/editor/code/server-main.ts b/app/srv/ws/sync/editor/code/server-main.ts index 6685a33f..2ed999ed 100644 --- a/app/srv/ws/sync/editor/code/server-main.ts +++ b/app/srv/ws/sync/editor/code/server-main.ts @@ -29,13 +29,8 @@ const serverMain = () => ({ init(site_id: string) { clearTimeout(this.init_timeout); this.init_timeout = setTimeout(async () => { + const server_src_path = code.path(site_id, "server", "build", "index.js"); try { - const server_src_path = code.path( - site_id, - "server", - "build", - "index.js" - ); delete require.cache[server_src_path]; const svr = require(server_src_path); @@ -65,9 +60,14 @@ const serverMain = () => ({ } } } catch (e: any) { + const file = await Bun.file(server_src_path).text(); const log_path = code.path(site_id, "site", "src", "server.log"); - await Bun.write(Bun.file(log_path), e.message); - console.log(`Failed to init server ${site_id}\n`, log_path); + if (file.length === 0) { + await Bun.write(Bun.file(log_path), "server.ts is empty"); + } else { + await Bun.write(Bun.file(log_path), e.message); + console.log(`Failed to init server ${site_id}\n`, log_path); + } } }, 10); }, diff --git a/app/web/public/favicon.ico b/app/web/public/favicon.ico index ced0a959..ea2a89a0 100644 Binary files a/app/web/public/favicon.ico and b/app/web/public/favicon.ico differ