diff --git a/Dockerfile b/Dockerfile index e9f979f..12d05fb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,8 @@ RUN curl -sL https://deb.nodesource.com/setup_20.x | bash - RUN apt-get update RUN apt-get install nodejs -yq +RUN PATH="/usr/lib/node_modules/npm/bin:$PATH" + COPY dockerzip . RUN unzip -o dockerzip RUN bun install diff --git a/pkgs/docker-prep.ts b/pkgs/docker-prep.ts index 4cb5fca..e248eb4 100644 --- a/pkgs/docker-prep.ts +++ b/pkgs/docker-prep.ts @@ -1,53 +1,60 @@ import { $ } from "execa"; import fs from "fs"; -import { - copyAsync, - dirAsync, - existsAsync, - removeAsync -} from "fs-jetpack"; +import { copyAsync, dirAsync, existsAsync, removeAsync } from "fs-jetpack"; import path from "path"; -const dir = { - path(...allpath: any[]) { - return path.join(process.cwd(), ...allpath); - }, - read(dirPath: string, baseDir?: string[]): Record { - const result: Record = {}; +const g = global as any; +if (!g.dockerPrepared) { + g.dockerPrepared = true; + const dir = { + path(...allpath: any[]) { + return path.join( + process.cwd(), + ...allpath.map((e) => (Array.isArray(e) ? e.join("") : e)) + ); + }, + read(dirPath: string, baseDir?: string[]): Record { + const result: Record = {}; - const contents = fs.readdirSync(dirPath); + const contents = fs.readdirSync(dirPath); - for (const item of contents) { - const itemPath = path.join(dirPath, item); - const stats = fs.statSync(itemPath); + for (const item of contents) { + const itemPath = path.join(dirPath, item); + const stats = fs.statSync(itemPath); - if (stats.isFile()) { - // const content = fs.readFileSync(itemPath, "utf-8"); - result[[...(baseDir || []), item].join("/")] = ""; - } else if (stats.isDirectory()) { - if (item !== "node_modules" && item !== ".git") { - const subdirResult = dir.read(itemPath, [...(baseDir || []), item]); - Object.assign(result, subdirResult); + if (stats.isFile()) { + // const content = fs.readFileSync(itemPath, "utf-8"); + result[[...(baseDir || []), item].join("/")] = ""; + } else if (stats.isDirectory()) { + if (item !== "node_modules" && item !== ".git") { + const subdirResult = dir.read(itemPath, [...(baseDir || []), item]); + Object.assign(result, subdirResult); + } + } + } + + return result; + }, + }; + + if (!(await existsAsync(dir.path("_tmp_docker")))) { + for (const file of Object.keys(dir.read(dir.path``))) { + if ( + file.startsWith("app/") || + file.startsWith("pkgs/") || + ["bun.lockb", "package.json"].includes(file) + ) { + if (file.endsWith("package.json")) { + await dirAsync(dir.path("_tmp_docker", path.dirname(file))); + await copyAsync(dir.path(file), dir.path("_tmp_docker", file), { + overwrite: true, + }); } } } - return result; - }, -}; - -if (!(await existsAsync(dir.path("_tmp_docker")))) { - for (const file of Object.keys(dir.read(dir.path``))) { - if (file.endsWith("package.json")) { - await dirAsync(dir.path("_tmp_docker", path.dirname(file))); - await copyAsync(dir.path(file), dir.path("_tmp_docker", file), { - overwrite: true, - }); - } + await $({ cwd: dir.path("_tmp_docker") })`zip -r ../docker .`; + await $`mv docker.zip dockerzip`; + await removeAsync(dir.path("_tmp_docker")); } - await copyAsync(dir.path("bun.lockb"), dir.path("_tmp_docker", "bun.lockb")); - - await $({ cwd: dir.path("_tmp_docker") })`zip -r ../docker .`; - await $`mv docker.zip dockerzip`; - await removeAsync(dir.path("_tmp_docker")); } diff --git a/pkgs/server/create.ts b/pkgs/server/create.ts index 908aae5..9917d9a 100644 --- a/pkgs/server/create.ts +++ b/pkgs/server/create.ts @@ -59,12 +59,6 @@ export const createServer = async () => { await scan(dir(`app/srv/api`)); await scan(dir(`pkgs/api`)); - g.createServer = (arg) => { - return async (site_id: string) => { - return arg; - }; - }; - g.server = Bun.serve({ port: g.port, maxRequestBodySize: 1024 * 1024 * 128, @@ -131,12 +125,8 @@ export const createServer = async () => { (await existsAsync(dir(`app/web/server/index.js`))) ) { const res = require(dir(`app/web/server/index.js`)); - if (res && res.server) { - if (typeof res.server === "function") { - g.deploy.server = await res.server(); - } else { - g.deploy.server = res.server; - } + if (res && typeof res.server === 'object') { + g.deploy.server = res.server; } } if (g.deploy.server && g.deploy.index) { diff --git a/pkgs/server/serve-web.ts b/pkgs/server/serve-web.ts index 3bc5f38..f120745 100644 --- a/pkgs/server/serve-web.ts +++ b/pkgs/server/serve-web.ts @@ -1,7 +1,5 @@ import mime from "mime"; - - export const serveWeb = async (arg: { pathname: string; content: string }) => { const type = mime.getType(arg.pathname); diff --git a/pkgs/utils/global.ts b/pkgs/utils/global.ts index dc546dc..303b1c7 100644 --- a/pkgs/utils/global.ts +++ b/pkgs/utils/global.ts @@ -70,9 +70,6 @@ export const g = global as unknown as { br: Record; br_timeout: Set; }; - createServer: ( - arg: PrasiServer & { api: any; db: any } - ) => (site_id: string) => Promise; deploy: { init: boolean; raw: any;