Files
platforms/scripts/build-frontend.sh

103 lines
2.2 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
OUTPUT_ROOT="${PROJECT_ROOT}/output"
require_command() {
local command_name="$1"
if ! command -v "${command_name}" >/dev/null 2>&1; then
echo "缺少命令:${command_name}" >&2
exit 1
fi
}
build_project() {
local project_name="$1"
local project_dir="$2"
local package_manager="$3"
local build_output_dir="$4"
local output_name="$5"
local target_dir="${OUTPUT_ROOT}/${output_name}"
if [[ ! -f "${project_dir}/package.json" ]]; then
echo "未找到 ${project_name} 的 package.json${project_dir}" >&2
exit 1
fi
if [[ ! -d "${project_dir}/node_modules" ]]; then
echo "${project_name} 依赖尚未安装,请先在 ${project_dir} 安装依赖。" >&2
exit 1
fi
echo
echo "开始构建:${project_name}"
(
cd "${project_dir}"
case "${package_manager}" in
pnpm)
pnpm build
;;
npm)
npm run build
;;
*)
echo "不支持的包管理器:${package_manager}" >&2
exit 1
;;
esac
)
echo "构建完成:${project_name}"
if [[ ! -d "${project_dir}/${build_output_dir}" ]]; then
echo "${project_name} 构建产物不存在:${project_dir}/${build_output_dir}" >&2
exit 1
fi
if [[ "${target_dir}" != "${OUTPUT_ROOT}/"* ]]; then
echo "拒绝清理非预期输出目录:${target_dir}" >&2
exit 1
fi
rm -rf "${target_dir}"
mkdir -p "${target_dir}"
cp -R "${project_dir}/${build_output_dir}/." "${target_dir}/"
echo "产物已汇总:${target_dir}"
}
require_command pnpm
require_command npm
mkdir -p "${OUTPUT_ROOT}"
build_project \
"平台总后台" \
"${PROJECT_ROOT}/frontend/platform_admin" \
pnpm \
dist \
platform
build_project \
"气站管理后台" \
"${PROJECT_ROOT}/frontend/gas_admin" \
pnpm \
dist \
gas
build_project \
"配送点管理后台" \
"${PROJECT_ROOT}/frontend/delivery_admin" \
pnpm \
dist \
delivery
build_project \
"和气门户首页" \
"${PROJECT_ROOT}/frontend/site" \
npm \
dist/client \
site
echo
echo "四个前端项目全部构建完成,产物目录:${OUTPUT_ROOT}"