This commit is contained in:
Rizky 2024-03-07 11:09:44 +07:00
parent a9fbf2d3ca
commit 9ea7e373d8
5 changed files with 51 additions and 57 deletions

View File

@ -9,6 +9,8 @@ RUN curl -sL https://deb.nodesource.com/setup_20.x | bash -
RUN apt-get update RUN apt-get update
RUN apt-get install nodejs -yq RUN apt-get install nodejs -yq
RUN PATH="/usr/lib/node_modules/npm/bin:$PATH"
COPY dockerzip . COPY dockerzip .
RUN unzip -o dockerzip RUN unzip -o dockerzip
RUN bun install RUN bun install

View File

@ -1,53 +1,60 @@
import { $ } from "execa"; import { $ } from "execa";
import fs from "fs"; import fs from "fs";
import { import { copyAsync, dirAsync, existsAsync, removeAsync } from "fs-jetpack";
copyAsync,
dirAsync,
existsAsync,
removeAsync
} from "fs-jetpack";
import path from "path"; import path from "path";
const dir = { const g = global as any;
path(...allpath: any[]) { if (!g.dockerPrepared) {
return path.join(process.cwd(), ...allpath); g.dockerPrepared = true;
}, const dir = {
read(dirPath: string, baseDir?: string[]): Record<string, string> { path(...allpath: any[]) {
const result: Record<string, string> = {}; return path.join(
process.cwd(),
...allpath.map((e) => (Array.isArray(e) ? e.join("") : e))
);
},
read(dirPath: string, baseDir?: string[]): Record<string, string> {
const result: Record<string, string> = {};
const contents = fs.readdirSync(dirPath); const contents = fs.readdirSync(dirPath);
for (const item of contents) { for (const item of contents) {
const itemPath = path.join(dirPath, item); const itemPath = path.join(dirPath, item);
const stats = fs.statSync(itemPath); const stats = fs.statSync(itemPath);
if (stats.isFile()) { if (stats.isFile()) {
// const content = fs.readFileSync(itemPath, "utf-8"); // const content = fs.readFileSync(itemPath, "utf-8");
result[[...(baseDir || []), item].join("/")] = ""; result[[...(baseDir || []), item].join("/")] = "";
} else if (stats.isDirectory()) { } else if (stats.isDirectory()) {
if (item !== "node_modules" && item !== ".git") { if (item !== "node_modules" && item !== ".git") {
const subdirResult = dir.read(itemPath, [...(baseDir || []), item]); const subdirResult = dir.read(itemPath, [...(baseDir || []), item]);
Object.assign(result, subdirResult); 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; await $({ cwd: dir.path("_tmp_docker") })`zip -r ../docker .`;
}, await $`mv docker.zip dockerzip`;
}; await removeAsync(dir.path("_tmp_docker"));
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 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"));
} }

View File

@ -59,12 +59,6 @@ export const createServer = async () => {
await scan(dir(`app/srv/api`)); await scan(dir(`app/srv/api`));
await scan(dir(`pkgs/api`)); await scan(dir(`pkgs/api`));
g.createServer = (arg) => {
return async (site_id: string) => {
return arg;
};
};
g.server = Bun.serve({ g.server = Bun.serve({
port: g.port, port: g.port,
maxRequestBodySize: 1024 * 1024 * 128, maxRequestBodySize: 1024 * 1024 * 128,
@ -131,12 +125,8 @@ export const createServer = async () => {
(await existsAsync(dir(`app/web/server/index.js`))) (await existsAsync(dir(`app/web/server/index.js`)))
) { ) {
const res = require(dir(`app/web/server/index.js`)); const res = require(dir(`app/web/server/index.js`));
if (res && res.server) { if (res && typeof res.server === 'object') {
if (typeof res.server === "function") { g.deploy.server = res.server;
g.deploy.server = await res.server();
} else {
g.deploy.server = res.server;
}
} }
} }
if (g.deploy.server && g.deploy.index) { if (g.deploy.server && g.deploy.index) {

View File

@ -1,7 +1,5 @@
import mime from "mime"; import mime from "mime";
export const serveWeb = async (arg: { pathname: string; content: string }) => { export const serveWeb = async (arg: { pathname: string; content: string }) => {
const type = mime.getType(arg.pathname); const type = mime.getType(arg.pathname);

View File

@ -70,9 +70,6 @@ export const g = global as unknown as {
br: Record<string, Uint8Array>; br: Record<string, Uint8Array>;
br_timeout: Set<string>; br_timeout: Set<string>;
}; };
createServer: (
arg: PrasiServer & { api: any; db: any }
) => (site_id: string) => Promise<PrasiServer & { api: any; db: any }>;
deploy: { deploy: {
init: boolean; init: boolean;
raw: any; raw: any;