wip prep-d-code

This commit is contained in:
Rizky 2024-01-23 05:52:01 +07:00
parent 32c9d3a46c
commit 30c89dc617
1 changed files with 125 additions and 121 deletions

View File

@ -10,153 +10,157 @@ import { snapshot } from "../../entity/snapshot";
export type DBCode = Exclude<Awaited<ReturnType<typeof getCode>>, 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;
};