wip fix
This commit is contained in:
parent
1de319400d
commit
7975ee351e
|
|
@ -4,6 +4,7 @@ import { dirname } from "path";
|
|||
import { apiContext } from "../../../pkgs/core/server/api/api-ctx";
|
||||
import { g } from "utils/global";
|
||||
import { baseTypings } from "../../web/src/utils/script/types/base";
|
||||
import { dir } from "dir";
|
||||
|
||||
export const _ = {
|
||||
url: "/code/:site_id/:action",
|
||||
|
|
@ -27,21 +28,21 @@ export const _ = {
|
|||
if (typeof json.apiTypes === "string") {
|
||||
apiPath = "gen/srv/api/srv";
|
||||
await Bun.write(
|
||||
`${g.datadir}/site/code/${site_id}/api-types.d.ts`,
|
||||
dir.data(`/site/code/${site_id}/api-types.d.ts`),
|
||||
json.apiTypes
|
||||
);
|
||||
}
|
||||
|
||||
for (const [k, v] of Object.entries(json.prismaTypes)) {
|
||||
await dirAsync(dirname(`${g.datadir}/site/code/${site_id}/${k}`));
|
||||
await dirAsync(dirname(dir.data(`/site/code/${site_id}/${k}`)));
|
||||
await Bun.write(
|
||||
`${g.datadir}/site/code/${site_id}/${k}`,
|
||||
dir.data(`/site/code/${site_id}/${k}`),
|
||||
JSON.parse(v)
|
||||
);
|
||||
}
|
||||
|
||||
await Bun.write(
|
||||
`${g.datadir}/site/code/${site_id}/global.d.ts`,
|
||||
dir.data(`/site/code/${site_id}/global.d.ts`),
|
||||
`\
|
||||
import React from "react";
|
||||
import {
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ window._prasi={basepath: "/deploy/${site_id}",site_id:"${site_id}"}
|
|||
} else if (pathname === "index.html" || pathname === "_") {
|
||||
return index_html;
|
||||
} else {
|
||||
const res = dir.path(`${g.datadir}/deploy/${pathname}`);
|
||||
const res = dir.data(`/deploy/${pathname}`);
|
||||
const file = Bun.file(res);
|
||||
if (!(await file.exists())) {
|
||||
return index_html;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import { dir } from "dir";
|
||||
import { apiContext } from "../../../pkgs/core/server/api/api-ctx";
|
||||
import { g } from "utils/global";
|
||||
|
||||
export const _ = {
|
||||
url: "/_img/**",
|
||||
async api() {
|
||||
const { req, res } = apiContext(this);
|
||||
const file = Bun.file(`${g.datadir}/upload/${req.params["*"]}`);
|
||||
const file = Bun.file(dir.data(`/upload/${req.params["*"]}`));
|
||||
if (await file.exists()) {
|
||||
return new Response(file as any);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ export const _ = {
|
|||
async api(mode: "site" | "page", id: string) {
|
||||
const { req, res } = apiContext(this);
|
||||
try {
|
||||
const s = await stat(dir.path(`${g.datadir}/npm/${mode}/${id}/index.js`));
|
||||
const s = await stat(dir.data(`$/npm/${mode}/${id}/index.js`));
|
||||
return s.size.toString();
|
||||
} catch (e) {}
|
||||
return "-";
|
||||
|
|
|
|||
|
|
@ -12,14 +12,14 @@ export const _ = {
|
|||
const { req, res, mode: _mode } = apiContext(this);
|
||||
|
||||
if (mode === "apk-qr") {
|
||||
const file_apk = Bun.file(dir.path(`${g.datadir}/prasi-wrap.apk`));
|
||||
const file_apk = Bun.file(dir.data(`/prasi-wrap.apk`));
|
||||
if (!await file_apk.exists()) {
|
||||
return new Response('not found');
|
||||
}
|
||||
return new Response(file_apk);
|
||||
}
|
||||
|
||||
let path = dir.path(`${g.datadir}/npm/${mode}/${id}/${req.params._}`);
|
||||
let path = dir.data(`/npm/${mode}/${id}/${req.params._}`);
|
||||
|
||||
const contentType = mime.lookup(path);
|
||||
if (contentType) res.setHeader("content-type", contentType);
|
||||
|
|
@ -53,7 +53,7 @@ export const _ = {
|
|||
}
|
||||
}
|
||||
|
||||
if (path.length > dir.path(`${g.datadir}/npm/${mode}/${id}`).length) {
|
||||
if (path.length > dir.data(`/npm/${mode}/${id}`).length) {
|
||||
const file = await readAsync(path, "buffer");
|
||||
|
||||
if (file) {
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ export const _ = {
|
|||
where: { id_page: { in: page_ids.map((e) => e.id) } },
|
||||
});
|
||||
|
||||
if (!exists(dir.path(`${g.datadir}/npm/site/${site_id}`))) {
|
||||
if (!exists(dir.data(`/npm/site/${site_id}`))) {
|
||||
await buildNpm({ id: site_id, mode: "site" });
|
||||
}
|
||||
const npm_page_ids = {} as Record<string, any[]>;
|
||||
|
|
@ -89,19 +89,19 @@ export const _ = {
|
|||
}
|
||||
|
||||
for (const [k, v] of Object.entries(npm_page_ids)) {
|
||||
if (!exists(dir.path(`${g.datadir}/npm/page/${k}`))) {
|
||||
if (!exists(dir.data(`/npm/page/${k}`))) {
|
||||
await buildNpm({ id: k, mode: "page", _items: v });
|
||||
}
|
||||
}
|
||||
|
||||
npm.site = readDirectoryRecursively(
|
||||
dir.path(`${g.datadir}/npm/site/${site_id}`)
|
||||
dir.data(`/npm/site/${site_id}`)
|
||||
);
|
||||
|
||||
for (const page of pages) {
|
||||
if (exists(dir.path(`${g.datadir}/npm/page/${page.id}`))) {
|
||||
if (exists(dir.data(`/npm/page/${page.id}`))) {
|
||||
npm.pages[page.id] = readDirectoryRecursively(
|
||||
dir.path(`${g.datadir}/npm/page/${page.id}`)
|
||||
dir.data(`/npm/page/${page.id}`)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -107,26 +107,26 @@ export const buildNpm = async ({
|
|||
${imports}
|
||||
${exports}
|
||||
`.trim();
|
||||
await dirAsync(dir.path(`${g.datadir}/npm/${mode}/${id}`));
|
||||
await writeAsync(dir.path(`${g.datadir}/npm/${mode}/${id}/input.js`), src);
|
||||
await dirAsync(dir.data(`/npm/${mode}/${id}`));
|
||||
await writeAsync(dir.data(`/npm/${mode}/${id}/input.js`), src);
|
||||
packages["react"] = "18.2.0";
|
||||
packages["react-dom"] = "18.2.0";
|
||||
await writeAsync(dir.path(`${g.datadir}/npm/${mode}/${id}/package.json`), {
|
||||
await writeAsync(dir.data(`/npm/${mode}/${id}/package.json`), {
|
||||
dependencies: packages,
|
||||
});
|
||||
await writeAsync(
|
||||
dir.path(`${g.datadir}/npm/${mode}/${id}/pnpm-workspace.yaml`),
|
||||
dir.data(`/npm/${mode}/${id}/pnpm-workspace.yaml`),
|
||||
`\
|
||||
packages:
|
||||
- ./*`
|
||||
);
|
||||
try {
|
||||
await $({
|
||||
cwd: dir.path(`${g.datadir}/npm/${mode}/${id}`),
|
||||
cwd: dir.data(`/npm/${mode}/${id}`),
|
||||
})`pnpm i`;
|
||||
|
||||
await build({
|
||||
absWorkingDir: dir.path(`${g.datadir}/npm/${mode}/${id}`),
|
||||
absWorkingDir: dir.data(`/npm/${mode}/${id}`),
|
||||
entryPoints: ["input.js"],
|
||||
bundle: true,
|
||||
outfile: "index.js",
|
||||
|
|
@ -153,7 +153,7 @@ packages:
|
|||
}
|
||||
|
||||
try {
|
||||
const s = await stat(dir.path(`${g.datadir}/npm/${mode}/${id}/index.js`));
|
||||
const s = await stat(dir.data(`/npm/${mode}/${id}/index.js`));
|
||||
|
||||
if (mode === "page") {
|
||||
delete glb.npm.page[id];
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { writeAsync, dirAsync } from "fs-jetpack";
|
|||
import { ExportMobileConfig } from "../../web/src/render/editor/panel/toolbar/center/mobile/config";
|
||||
|
||||
const mpath = (site_id: string, path?: string) =>
|
||||
dir.path(`${g.datadir}/mobile/${site_id}/${path || ""}`);
|
||||
dir.data(`/mobile/${site_id}/${path || ""}`);
|
||||
|
||||
export const exmobile = {
|
||||
config: {
|
||||
|
|
|
|||
|
|
@ -11,21 +11,21 @@ export const code = {
|
|||
if (path) {
|
||||
file_path = path[0] === "/" ? path : `/${path}`;
|
||||
}
|
||||
return dir.path(`${g.datadir}/code/${id_site}/${mode}/${type}${file_path}`);
|
||||
return dir.data(`/code/${id_site}/${mode}/${type}${file_path}`);
|
||||
},
|
||||
esbuild: {} as Record<string, Record<CodeMode, null | BuildContext>>,
|
||||
prep(id_site: string, mode: CodeMode) {
|
||||
if (exists(`${g.datadir}`)) {
|
||||
if (exists(dir.data(""))) {
|
||||
Bun.spawn({
|
||||
cmd: ["chmod", "-R", "777", "."],
|
||||
cwd: dir.path(`${g.datadir}`),
|
||||
cwd: dir.data(``),
|
||||
});
|
||||
}
|
||||
const promises: Promise<void>[] = [];
|
||||
return {
|
||||
path(type: "src" | "build", path: string) {
|
||||
return dir.path(
|
||||
`${g.datadir}/code/${id_site}/${mode}/${type}${
|
||||
return dir.data(
|
||||
`/code/${id_site}/${mode}/${type}${
|
||||
path[0] === "/" ? path : `/${path}`
|
||||
}`
|
||||
);
|
||||
|
|
|
|||
|
|
@ -69,7 +69,7 @@ export const snapshot = {
|
|||
init() {
|
||||
this._db = open<DocSnapshot, string>({
|
||||
name: "doc-snapshot",
|
||||
path: dir.path(`${g.datadir}/lmdb/doc-snapshot.lmdb`),
|
||||
path: dir.data(`/lmdb/doc-snapshot.lmdb`),
|
||||
compression: true,
|
||||
});
|
||||
return this._db;
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ export const user = {
|
|||
init() {
|
||||
this._db = open<UserConf, string>({
|
||||
name: "user-conf",
|
||||
path: dir.path(`${g.datadir}/lmdb/user-conf.lmdb`),
|
||||
path: dir.data(`/lmdb/user-conf.lmdb`),
|
||||
});
|
||||
return this._db;
|
||||
},
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ export const _ = {
|
|||
async api() {
|
||||
const { req } = apiContext(this);
|
||||
const rpath = decodeURIComponent(req.params._);
|
||||
const path = dir.path(`${g.datadir}/upload/${rpath}`);
|
||||
const path = dir.data(`/upload/${rpath}`);
|
||||
|
||||
try {
|
||||
return new Response(Bun.file(path) as any);
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ export const _ = {
|
|||
.toLowerCase()}`;
|
||||
|
||||
url = `/_file/${path}`;
|
||||
await writeAsync(dir.path(`${g.datadir}/upload/${path}`), part.buffer);
|
||||
await writeAsync(dir.data(`/upload/${path}`), part.buffer);
|
||||
}
|
||||
|
||||
return url;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ const ctx = await context({
|
|||
bundle: true,
|
||||
absWorkingDir: dir.path(""),
|
||||
entryPoints: [dir.path("app/web/src/nova/deploy/main.tsx")],
|
||||
outdir: dir.path(`${g.datadir}/deploy`),
|
||||
outdir: dir.data(`/deploy`),
|
||||
splitting: true,
|
||||
format: "esm",
|
||||
jsx: "transform",
|
||||
|
|
|
|||
|
|
@ -30,8 +30,6 @@ if (!g.Y) {
|
|||
await createLogger();
|
||||
g._api = {};
|
||||
g.mode = process.argv.includes("dev") ? "dev" : "prod";
|
||||
g.datadir = dir.path(g.mode === "prod" ? "../data" : "data", false);
|
||||
console.log("DataDir", g.datadir);
|
||||
g.port = parseInt(process.env.PORT || "4550");
|
||||
|
||||
g.log.info(g.mode === "dev" ? "DEVELOPMENT" : "PRODUCTION");
|
||||
|
|
|
|||
|
|
@ -1,13 +1,16 @@
|
|||
import { join } from "path";
|
||||
import { g } from "./global";
|
||||
|
||||
export const dir = {
|
||||
path: (path: string, safe?: boolean) => {
|
||||
if (safe === false) return join(process.cwd(), path);
|
||||
|
||||
if (path.startsWith(process.cwd())) {
|
||||
return path;
|
||||
}
|
||||
|
||||
data: (path: string) => {
|
||||
const final_path = path
|
||||
.split("/")
|
||||
.filter((e) => e !== "..")
|
||||
.join("/");
|
||||
if (g.mode === "prod") return join(process.cwd(), "..", "data", final_path);
|
||||
else return join(process.cwd(), "data", final_path);
|
||||
},
|
||||
path: (path: string) => {
|
||||
const final_path = path
|
||||
.split("/")
|
||||
.filter((e) => e !== "..")
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ type SingleRoute = {
|
|||
|
||||
export const g = global as unknown as {
|
||||
status: "init" | "ready";
|
||||
datadir: string;
|
||||
_db: PrismaClient;
|
||||
dburl: string;
|
||||
mode: "dev" | "prod";
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import { createId } from "@paralleldrive/cuid2";
|
|||
import { dirAsync } from "fs-jetpack";
|
||||
import lmdb, { RootDatabase, open } from "lmdb";
|
||||
import { dirname, join } from "path";
|
||||
import { g } from "./global";
|
||||
import { dir } from "./dir";
|
||||
|
||||
const cuid = createId;
|
||||
|
|
@ -14,7 +13,7 @@ export const createCache = <T>() => ({
|
|||
lmdb: null as unknown as RootDatabase<SessionEntry<T>>,
|
||||
cookieKey: "",
|
||||
async init(arg: { cookieKey: string; dbname?: string }) {
|
||||
const dbpath = dir.path(join(g.datadir, (arg.dbname || "session") + ".lmdb"));
|
||||
const dbpath = dir.data(join(arg.dbname || "session") + ".lmdb");
|
||||
await dirAsync(dirname(dbpath));
|
||||
self(this).lmdb = open({
|
||||
path: dbpath,
|
||||
|
|
|
|||
Loading…
Reference in New Issue