From 1d8ce5a16c83373cd4c34308580f0e833ff3b839 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Wed, 8 Jul 2026 15:03:28 +0800 Subject: [PATCH] feat: add one-click preview and deploy scripts --- package.json | 3 ++- scripts/blog.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100755 scripts/blog.sh diff --git a/package.json b/package.json index 97e45f8..4bb930e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "scripts": { "build": "hexo generate", "clean": "hexo clean", - "deploy": "hexo deploy", + "preview": "bash scripts/blog.sh preview", + "deploy": "bash scripts/blog.sh deploy", "server": "hexo server" }, "hexo": { diff --git a/scripts/blog.sh b/scripts/blog.sh new file mode 100755 index 0000000..d560642 --- /dev/null +++ b/scripts/blog.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$ROOT_DIR" + +PORT="${PORT:-4000}" +HOST="${HOST:-127.0.0.1}" +BRANCH="${BRANCH:-main}" + +usage() { + cat <<'EOF' +用法: + ./scripts/blog.sh preview # 一键预览环境 + ./scripts/blog.sh deploy # 一键构建 + 提交 + 推送到自建 git + +可选环境变量: + PORT=4000 预览端口 + HOST=127.0.0.1 预览监听地址 + BRANCH=main 推送分支 +EOF +} + +preview() { + echo "[preview] clean + start hexo server on ${HOST}:${PORT}" + npx hexo clean + exec npx hexo server -i "${HOST}" -p "${PORT}" +} + +deploy() { + echo "[deploy] clean + generate" + npx hexo clean + npx hexo generate + + echo "[deploy] git add" + git add -A + + if git diff --cached --quiet; then + echo "[deploy] 没有可提交的改动,跳过 commit/push" + exit 0 + fi + + if git diff --cached --quiet --ignore-submodules --exit-code; then + : # keep shellcheck calm + fi + + if ! git rev-parse --verify HEAD >/dev/null 2>&1; then + git commit -m "chore: deploy blog" + else + git commit -m "chore: deploy blog $(date '+%F %T')" + fi + + echo "[deploy] push origin ${BRANCH}" + git push origin "${BRANCH}" +} + +case "${1:-}" in + preview) preview ;; + deploy) deploy ;; + -h|--help|help|"") usage ;; + *) + echo "未知命令: $1" >&2 + usage + exit 1 + ;; +esac