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