feat: 生成启动脚本

This commit is contained in:
qcqcqc@wsl 2025-10-31 15:19:27 +08:00
parent cc263193d0
commit 61b90e2dba
3 changed files with 75 additions and 3 deletions

25
dist/cli.js vendored
View File

@ -526,6 +526,27 @@ function copyConfiguration(projectDir, imageBase, name, pwd2) {
fs3.copyFileSync(srcPath, destPath); 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) { function writeStartCommandScript(projectDir, imageBase, name, pwd2) {
const distDir = path3.join(pwd2, "dist", "server", imageBase); const distDir = path3.join(pwd2, "dist", "server", imageBase);
if (!fs3.existsSync(distDir)) { if (!fs3.existsSync(distDir)) {
@ -546,7 +567,7 @@ ${imageBase}:${name} \\
pm2-runtime start pm2.dev.json pm2-runtime start pm2.dev.json
`; `;
fs3.writeFileSync(scriptPath, scriptContent, { mode: 493 }); 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() { function main() {
const args = parseCliArgs(); const args = parseCliArgs();
@ -610,6 +631,7 @@ function packageBackend(projectDir, options) {
saveDockerImage(imageBase, name, pwd); saveDockerImage(imageBase, name, pwd);
copyConfiguration(projectDir, imageBase, name, pwd); copyConfiguration(projectDir, imageBase, name, pwd);
writeStartCommandScript(projectDir, imageBase, name, pwd); writeStartCommandScript(projectDir, imageBase, name, pwd);
writeInitCommandScript(projectDir, imageBase, name, pwd);
console.log("\n\u955C\u50CF\u6784\u5EFA\u6210\u529F\uFF01"); console.log("\n\u955C\u50CF\u6784\u5EFA\u6210\u529F\uFF01");
} finally { } finally {
fs3.rmSync(path3.join(pwd, "Dockerfile"), { force: true }); fs3.rmSync(path3.join(pwd, "Dockerfile"), { force: true });
@ -669,6 +691,7 @@ function packageInit(projectDir, options) {
} }
} }
function copyTemplateFiles(dist) { function copyTemplateFiles(dist) {
const __dirname = path3.dirname(new URL(import.meta.url).pathname);
const templateDir = path3.join(__dirname, "../template"); const templateDir = path3.join(__dirname, "../template");
function copyDir(srcDir, destDir) { function copyDir(srcDir, destDir) {
if (!fs3.existsSync(destDir)) { if (!fs3.existsSync(destDir)) {

View File

@ -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( function writeStartCommandScript(
projectDir: string, projectDir: string,
imageBase: string, imageBase: string,
@ -226,7 +254,7 @@ ${imageBase}:${name} \\
pm2-runtime start pm2.dev.json pm2-runtime start pm2.dev.json
`; `;
fs.writeFileSync(scriptPath, scriptContent, { mode: 0o755 }); 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); saveDockerImage(imageBase, name, pwd);
copyConfiguration(projectDir, imageBase, name, pwd); copyConfiguration(projectDir, imageBase, name, pwd);
writeStartCommandScript(projectDir, imageBase, name, pwd); writeStartCommandScript(projectDir, imageBase, name, pwd);
writeInitCommandScript(projectDir, imageBase, name, pwd);
console.log("\n镜像构建成功"); console.log("\n镜像构建成功");
} finally { } finally {
@ -416,6 +445,7 @@ function packageInit(projectDir: string, options: BuildOptions): void {
function copyTemplateFiles( function copyTemplateFiles(
dist: string, dist: string,
) { ) {
const __dirname = path.dirname(new URL(import.meta.url).pathname);
const templateDir = path.join(__dirname, "../template"); const templateDir = path.join(__dirname, "../template");
// 递归复制 template 目录下的所有文件到 dist 目录 // 递归复制 template 目录下的所有文件到 dist 目录
function copyDir(srcDir: string, destDir: string) { function copyDir(srcDir: string, destDir: string) {

View File

@ -43,6 +43,24 @@ services:
volumes: volumes:
- minio_data:/data - 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 网关 # Nginx 网关
# ======================= # =======================
@ -52,12 +70,13 @@ services:
restart: always restart: always
network_mode: host network_mode: host
volumes: volumes:
- ./conf.d:/etc/nginx/conf.d:ro - ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./html:/usr/share/nginx/html:ro - ./html:/usr/share/nginx/html:ro
depends_on: depends_on:
- mysql - mysql
- redis - redis
- minio - minio
- minio-init
# ======================= # =======================
# 数据卷 # 数据卷