wip prep-d-code
This commit is contained in:
parent
32c9d3a46c
commit
30c89dc617
|
|
@ -10,153 +10,157 @@ import { snapshot } from "../../entity/snapshot";
|
||||||
export type DBCode = Exclude<Awaited<ReturnType<typeof getCode>>, null>;
|
export type DBCode = Exclude<Awaited<ReturnType<typeof getCode>>, null>;
|
||||||
|
|
||||||
export const prepCode = async (site_id: string, name: string) => {
|
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(
|
const pkgfile = Bun.file(
|
||||||
dir.path(`${g.datadir}/site/code/${site_id}/package.json`)
|
dir.path(`${g.datadir}/site/code/${site_id}/package.json`),
|
||||||
);
|
);
|
||||||
if (!(await pkgfile.exists())) {
|
if (!(await pkgfile.exists())) {
|
||||||
await dirAsync(dir.path(`${g.datadir}/site/code/${site_id}`));
|
await dirAsync(dir.path(`${g.datadir}/site/code/${site_id}`));
|
||||||
await Bun.write(
|
await Bun.write(
|
||||||
pkgfile,
|
pkgfile,
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
{
|
{
|
||||||
name: "code",
|
name: "code",
|
||||||
workspaces: ["./*"],
|
workspaces: ["./*"],
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
2
|
2,
|
||||||
)
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (code) {
|
if (code) {
|
||||||
await dirAsync(dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`));
|
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);
|
await db.code_file.create({
|
||||||
return code;
|
data: {
|
||||||
}
|
id_code: new_code.id,
|
||||||
let new_code = await db.code.create({
|
path: "index.tsx",
|
||||||
data: {
|
content: `\
|
||||||
id_site: site_id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await db.code_file.create({
|
|
||||||
data: {
|
|
||||||
id_code: new_code.id,
|
|
||||||
path: "index.tsx",
|
|
||||||
content: `\
|
|
||||||
export const hello_world = () => {
|
export const hello_world = () => {
|
||||||
console.log('hello world')
|
console.log('hello world')
|
||||||
}`,
|
}`,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
await db.code_file.create({
|
await db.code_file.create({
|
||||||
data: {
|
data: {
|
||||||
id_code: new_code.id,
|
id_code: new_code.id,
|
||||||
path: "package.json",
|
path: "package.json",
|
||||||
content: JSON.stringify(
|
content: JSON.stringify(
|
||||||
{
|
{
|
||||||
name: new_code.id,
|
name: new_code.id,
|
||||||
dependencies: {},
|
dependencies: {},
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
2
|
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) => {
|
export const getCode = async (site_id: string, name?: string) => {
|
||||||
return await db.code.findFirst({
|
return await db.code.findFirst({
|
||||||
where: name
|
where: name
|
||||||
? { id_site: site_id, name }
|
? { id_site: site_id, name }
|
||||||
: {
|
: {
|
||||||
id_site: site_id,
|
id_site: site_id,
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
id_site: true,
|
id_site: true,
|
||||||
name: true,
|
name: true,
|
||||||
code_file: true,
|
code_file: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const prepDCode = async (site_id: string) => {
|
export const prepDCode = async (site_id: string) => {
|
||||||
let exists = false;
|
let exists = false;
|
||||||
if (!docs.code[site_id]) {
|
if (!docs.code[site_id]) {
|
||||||
const code = await getCode(site_id, "site");
|
const code = await getCode(site_id, "site");
|
||||||
|
|
||||||
if (code) {
|
if (code) {
|
||||||
const path = {
|
const path = {
|
||||||
src: dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`),
|
src: dir.path(`${g.datadir}/site/code/${site_id}/${code.id}`),
|
||||||
build: dir.path(`${g.datadir}/site/build/${code.id}`),
|
build: dir.path(`${g.datadir}/site/build/${code.id}`),
|
||||||
};
|
};
|
||||||
|
|
||||||
if ((await existsAsync(path.src)) && (await existsAsync(path.build))) {
|
if ((await existsAsync(path.src)) && (await existsAsync(path.build))) {
|
||||||
docs.code[site_id] = {
|
docs.code[site_id] = {
|
||||||
id: site_id,
|
id: site_id,
|
||||||
src: loadFolderAsDCode(code.id, path.src),
|
src: loadFolderAsDCode(code.id, path.src),
|
||||||
build: loadFolderAsDCode(code.id, path.build),
|
build: loadFolderAsDCode(code.id, path.build),
|
||||||
};
|
};
|
||||||
exists = true;
|
exists = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exists) {
|
if (exists) {
|
||||||
const src_bin = Y.encodeStateAsUpdate(docs.code[site_id].src as Doc);
|
Bun.spawn({
|
||||||
const build_bin = Y.encodeStateAsUpdate(docs.code[site_id].build as Doc);
|
cmd: ["chmod", "777", "-R", "."],
|
||||||
|
cwd: dir.path(`${g.datadir}/site}`),
|
||||||
|
});
|
||||||
|
|
||||||
let snap = await snapshot.getOrCreate({
|
const src_bin = Y.encodeStateAsUpdate(docs.code[site_id].src as Doc);
|
||||||
type: "site",
|
const build_bin = Y.encodeStateAsUpdate(docs.code[site_id].build as Doc);
|
||||||
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") {
|
let snap = await snapshot.getOrCreate({
|
||||||
return {
|
type: "site",
|
||||||
bin: {
|
id: site_id,
|
||||||
src: snap.src.bin,
|
name: "",
|
||||||
build: snap.build.bin,
|
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 loadFolderAsDCode = (id: string, path: string) => {
|
||||||
const doc = new Y.Doc() as DCode;
|
const doc = new Y.Doc() as DCode;
|
||||||
const map = doc.getMap("map");
|
const map = doc.getMap("map");
|
||||||
|
|
||||||
const files = new Y.Map();
|
const files = new Y.Map();
|
||||||
|
|
||||||
const dirs = readDirectoryRecursively(path);
|
const dirs = readDirectoryRecursively(path);
|
||||||
for (const [k, v] of Object.entries(dirs)) {
|
for (const [k, v] of Object.entries(dirs)) {
|
||||||
files.set(k, new Y.Text(v));
|
files.set(k, new Y.Text(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
doc.transact(() => {
|
doc.transact(() => {
|
||||||
map.set("files", files as any);
|
map.set("files", files as any);
|
||||||
map.set("id", id);
|
map.set("id", id);
|
||||||
});
|
});
|
||||||
|
|
||||||
return doc;
|
return doc;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue