FROM node:18-alpine AS builder

WORKDIR /app

# 复制package.json和package-lock.json
COPY package*.json ./

# 安装依赖
RUN npm config set registry https://registry.npmmirror.com
RUN npm cache clean --force
RUN npm install

# 复制所有源代码
COPY . .

# 构建前端应用
RUN npm run build

# 生产环境镜像
FROM node:18-alpine

WORKDIR /app

# 复制构建后的前端文件
COPY --from=builder /app/dist /app/dist

# 复制后端文件
COPY --from=builder /app/server /app/server
COPY --from=builder /app/package*.json /app/

# 只安装生产环境依赖
RUN npm config set registry https://registry.npmmirror.com
RUN npm cache clean --force
RUN npm install --only=production

# 设置环境变量
ENV PORT=3010
ENV NODE_ENV=production

# 暴露端口
EXPOSE 3010

# 启动命令
CMD ["node", "server/index.js"]