checkpoint

This commit is contained in:
Rizky 2024-05-05 18:52:28 +07:00
parent e3a199b45b
commit 11ea32ac07
5 changed files with 42 additions and 96 deletions

View File

@ -9,14 +9,13 @@ import { ensureFiles } from "./utlis/ensure-files";
export const code = { export const code = {
internal: codeInternal, internal: codeInternal,
async init(id_site: string, note: string) { async init(id_site: string, note: string) {
const { frontend, server, typings } = this.internal;
const root = `/code/${id_site}/site/src`; const root = `/code/${id_site}/site/src`;
await initFrontEnd(root, id_site);
await initServer(root, id_site);
await ensureLib(root, id_site); await ensureLib(root, id_site);
await ensureFiles(root, id_site); await ensureFiles(root, id_site);
await initFrontEnd(root, id_site);
await initServer(root, id_site);
}, },
path( path(
id_site: string, id_site: string,

View File

@ -6,9 +6,9 @@ import { removeAsync } from "fs-jetpack";
import isEqual from "lodash.isequal"; import isEqual from "lodash.isequal";
import { code } from "../../code"; import { code } from "../../code";
import { buildTypes } from "./typings"; import { buildTypes } from "./typings";
import { appendFile } from "node:fs/promises";
const npm_list = {} as Record<string, Set<string>>; const decoder = new TextDecoder();
export const initFrontEnd = async (root: string, id_site: string) => { export const initFrontEnd = async (root: string, id_site: string) => {
let existing = code.internal.frontend[id_site]; let existing = code.internal.frontend[id_site];
@ -50,87 +50,8 @@ export const initFrontEnd = async (root: string, id_site: string) => {
}), }),
{ {
name: "prasi", name: "prasi",
setup(setup) { async setup(setup) {
try { try {
setup.onEnd(async (res) => {
if (!npm_list[id_site]) npm_list[id_site] = new Set<string>();
const imports = new Set<string>();
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) { } catch (e) {
console.log("ERROR"); 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"); const path = code.path(id_site, "site", "src", "index.log");
if (append) {
await appendFile(path, error);
return;
}
await Bun.write(path, error); await Bun.write(path, error);
}; };
@ -160,3 +86,23 @@ const isInstalling = async (id_site: string) => {
} catch (e) {} } catch (e) {}
return false; return false;
}; };
const readPackageJSON = async (id_site: string) => {
const file = Bun.file(code.path(id_site, "site", "src", "package.json"));
const deps = new Set<string>();
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;
};

View File

@ -19,7 +19,8 @@ export const ensureFiles = async (path: string, id_site: string) => {
const f = t.replaceAll("_", "."); const f = t.replaceAll("_", ".");
const to = dir.data(path + `/${f}`); const to = dir.data(path + `/${f}`);
const file = Bun.file(to); const file = Bun.file(to);
if (!(await file.exists())) { const exists = await file.exists();
if (!exists) {
const from = dir.path(`${tdir}/${t}`); const from = dir.path(`${tdir}/${t}`);
await dirAsync(dirname(to)); await dirAsync(dirname(to));
await copyAsync(from, to); await copyAsync(from, to);

View File

@ -29,13 +29,8 @@ const serverMain = () => ({
init(site_id: string) { init(site_id: string) {
clearTimeout(this.init_timeout); clearTimeout(this.init_timeout);
this.init_timeout = setTimeout(async () => { this.init_timeout = setTimeout(async () => {
const server_src_path = code.path(site_id, "server", "build", "index.js");
try { try {
const server_src_path = code.path(
site_id,
"server",
"build",
"index.js"
);
delete require.cache[server_src_path]; delete require.cache[server_src_path];
const svr = require(server_src_path); const svr = require(server_src_path);
@ -65,10 +60,15 @@ const serverMain = () => ({
} }
} }
} catch (e: any) { } catch (e: any) {
const file = await Bun.file(server_src_path).text();
const log_path = code.path(site_id, "site", "src", "server.log"); const log_path = code.path(site_id, "site", "src", "server.log");
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); await Bun.write(Bun.file(log_path), e.message);
console.log(`Failed to init server ${site_id}\n`, log_path); console.log(`Failed to init server ${site_id}\n`, log_path);
} }
}
}, 10); }, 10);
}, },
async http( async http(

Binary file not shown.

Before

Width:  |  Height:  |  Size: 115 KiB

After

Width:  |  Height:  |  Size: 38 KiB