From 61b90e2dba84d989ad623388ca69c5d55b2dbf88 Mon Sep 17 00:00:00 2001 From: "qcqcqc@wsl" Date: Fri, 31 Oct 2025 15:19:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=9F=E6=88=90=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/cli.js | 25 ++++++++++++++++++++++++- src/cli.ts | 32 +++++++++++++++++++++++++++++++- template/docker-compose.yaml | 21 ++++++++++++++++++++- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/dist/cli.js b/dist/cli.js index aa89d08..c5e36d8 100644 --- a/dist/cli.js +++ b/dist/cli.js @@ -526,6 +526,27 @@ function copyConfiguration(projectDir, imageBase, name, pwd2) { fs3.copyFileSync(srcPath, destPath); } } +function writeInitCommandScript(projectDir, imageBase, name, pwd2) { + const distDir = path3.join(pwd2, "dist", "server", imageBase); + if (!fs3.existsSync(distDir)) { + fs3.mkdirSync(distDir, { recursive: true }); + } + const scriptPath = path3.join(distDir, "init.sh"); + const scriptContent = `#!/bin/sh + +docker load -i ${imageBase}-${name}.tar + +docker rm ${imageBase}-${name}-init || true + +docker run -it \\ +--name ${imageBase}-${name}-init \\ +-v $(pwd)/configuration:/app/${imageBase}/configuration \\ +${imageBase}:${name} \\ +npm run server:init +`; + fs3.writeFileSync(scriptPath, scriptContent, { mode: 493 }); + console.log(` \u5DF2\u751F\u6210init\u811A\u672C: ${scriptPath}`); +} function writeStartCommandScript(projectDir, imageBase, name, pwd2) { const distDir = path3.join(pwd2, "dist", "server", imageBase); if (!fs3.existsSync(distDir)) { @@ -546,7 +567,7 @@ ${imageBase}:${name} \\ pm2-runtime start pm2.dev.json `; fs3.writeFileSync(scriptPath, scriptContent, { mode: 493 }); - console.log(`\u2705 \u5DF2\u751F\u6210\u542F\u52A8\u811A\u672C: ${scriptPath}`); + console.log(` \u5DF2\u751F\u6210\u542F\u52A8\u811A\u672C: ${scriptPath}`); } function main() { const args = parseCliArgs(); @@ -610,6 +631,7 @@ function packageBackend(projectDir, options) { saveDockerImage(imageBase, name, pwd); copyConfiguration(projectDir, imageBase, name, pwd); writeStartCommandScript(projectDir, imageBase, name, pwd); + writeInitCommandScript(projectDir, imageBase, name, pwd); console.log("\n\u955C\u50CF\u6784\u5EFA\u6210\u529F\uFF01"); } finally { fs3.rmSync(path3.join(pwd, "Dockerfile"), { force: true }); @@ -669,6 +691,7 @@ function packageInit(projectDir, options) { } } function copyTemplateFiles(dist) { + const __dirname = path3.dirname(new URL(import.meta.url).pathname); const templateDir = path3.join(__dirname, "../template"); function copyDir(srcDir, destDir) { if (!fs3.existsSync(destDir)) { diff --git a/src/cli.ts b/src/cli.ts index 6c0a55b..e68acf7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -200,6 +200,34 @@ function copyConfiguration( } } +function writeInitCommandScript( + projectDir: string, + imageBase: string, + name: string, + pwd: string +): void { + const distDir = path.join(pwd, "dist", "server", imageBase); + if (!fs.existsSync(distDir)) { + fs.mkdirSync(distDir, { recursive: true }); + } + + const scriptPath = path.join(distDir, "init.sh"); + const scriptContent = `#!/bin/sh + +docker load -i ${imageBase}-${name}.tar + +docker rm ${imageBase}-${name}-init || true + +docker run -it \\ +--name ${imageBase}-${name}-init \\ +-v $(pwd)/configuration:/app/${imageBase}/configuration \\ +${imageBase}:${name} \\ +npm run server:init +`; + fs.writeFileSync(scriptPath, scriptContent, { mode: 0o755 }); + console.log(` 已生成init脚本: ${scriptPath}`); +} + function writeStartCommandScript( projectDir: string, imageBase: string, @@ -226,7 +254,7 @@ ${imageBase}:${name} \\ pm2-runtime start pm2.dev.json `; fs.writeFileSync(scriptPath, scriptContent, { mode: 0o755 }); - console.log(`✅ 已生成启动脚本: ${scriptPath}`); + console.log(` 已生成启动脚本: ${scriptPath}`); } /** @@ -333,6 +361,7 @@ function packageBackend(projectDir: string, options: BuildOptions): void { saveDockerImage(imageBase, name, pwd); copyConfiguration(projectDir, imageBase, name, pwd); writeStartCommandScript(projectDir, imageBase, name, pwd); + writeInitCommandScript(projectDir, imageBase, name, pwd); console.log("\n镜像构建成功!"); } finally { @@ -416,6 +445,7 @@ function packageInit(projectDir: string, options: BuildOptions): void { function copyTemplateFiles( dist: string, ) { + const __dirname = path.dirname(new URL(import.meta.url).pathname); const templateDir = path.join(__dirname, "../template"); // 递归复制 template 目录下的所有文件到 dist 目录 function copyDir(srcDir: string, destDir: string) { diff --git a/template/docker-compose.yaml b/template/docker-compose.yaml index d35a79f..7dbf40a 100644 --- a/template/docker-compose.yaml +++ b/template/docker-compose.yaml @@ -43,6 +43,24 @@ services: volumes: - minio_data:/data + # ======================= + # MinIO 初始化 + # ======================= + minio-init: + image: minio/mc:latest + container_name: minio_init_container + depends_on: + - minio + network_mode: host + entrypoint: > + /bin/sh -c " + sleep 5 && + mc alias set local http://127.0.0.1:9000 minioadmin minioadmin && + mc mb local/my-bucket || echo 'bucket exists' && + mc policy set public local/my-bucket + " + restart: "no" + # ======================= # Nginx 网关 # ======================= @@ -52,12 +70,13 @@ services: restart: always network_mode: host volumes: - - ./conf.d:/etc/nginx/conf.d:ro + - ./nginx/conf.d:/etc/nginx/conf.d:ro - ./html:/usr/share/nginx/html:ro depends_on: - mysql - redis - minio + - minio-init # ======================= # 数据卷