update at 2026-03-21 18:44:12

This commit is contained in:
douboer@gmail.com
2026-03-21 18:44:12 +08:00
parent f9d715157f
commit 89b1f97a6f
52 changed files with 3510 additions and 562 deletions

View File

@@ -0,0 +1,115 @@
#!/usr/bin/env sh
set -eu
ROOT_DIR="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)"
CALENDAR_DIR="$ROOT_DIR/calendar"
INTERVAL_MINUTES=60
RUN_ONCE=false
print_usage() {
cat <<'EOF'
用法:
sh scripts/refresh-kindle-backgrounds.sh [选项]
作用:
1. 构建 calendar Web 产物
2. 生成主题背景图到 calendar/kindle-backgrounds/
3. 默认每 60 分钟重跑一次;加 --once 只跑一轮
选项:
--interval-minutes <n> 自动生成间隔,默认 60 分钟
--once 只生成一轮
-h, --help 查看帮助
示例:
sh scripts/refresh-kindle-backgrounds.sh --once
sh scripts/refresh-kindle-backgrounds.sh --interval-minutes 30
EOF
}
log() {
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
}
validate_positive_integer() {
value=$1
name=$2
case "$value" in
''|*[!0-9]*)
echo "$name 必须是正整数:$value" >&2
exit 1
;;
esac
if [ "$value" -le 0 ]; then
echo "$name 必须大于 0$value" >&2
exit 1
fi
}
refresh_once() {
log "开始生成 Kindle 背景图到 $CALENDAR_DIR/kindle-backgrounds"
(
cd "$CALENDAR_DIR"
npm run export:themes
)
log "背景图生成完成"
}
sleep_until_next_run() {
interval_seconds=$((INTERVAL_MINUTES * 60))
now_epoch=$(date '+%s')
next_epoch=$(( ((now_epoch / interval_seconds) + 1) * interval_seconds ))
sleep_seconds=$((next_epoch - now_epoch))
if [ "$sleep_seconds" -le 0 ]; then
sleep_seconds=$interval_seconds
fi
log "等待 ${sleep_seconds} 秒后执行下一轮生成"
sleep "$sleep_seconds"
}
while [ "$#" -gt 0 ]; do
case "$1" in
--interval-minutes)
shift
INTERVAL_MINUTES=${1:?"missing interval minutes"}
;;
--once)
RUN_ONCE=true
;;
-h|--help)
print_usage
exit 0
;;
*)
echo "未知参数: $1" >&2
echo >&2
print_usage >&2
exit 1
;;
esac
shift
done
validate_positive_integer "$INTERVAL_MINUTES" "interval-minutes"
while true; do
if refresh_once; then
:
else
status=$?
log "本轮背景图生成失败,退出码:$status"
if [ "$RUN_ONCE" = true ]; then
exit "$status"
fi
fi
if [ "$RUN_ONCE" = true ]; then
break
fi
sleep_until_next_run
done