Files
Genarrative/scripts/spacetime-logs-local.sh
T
kdletters 387a1c26e3 完成全量检查整改
清理已跟踪原始日志与个人本地配置
修复前后端有效门禁、测试和构建问题
补齐生产 Jenkins、SpacetimeDB 本地命令与文档约束
收紧图片编辑器状态、附件与媒体引用测试
排除已下线旧创作入口测试并清理 warning
2026-07-17 16:56:46 +08:00

124 lines
2.8 KiB
Bash
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 -euo pipefail
usage() {
cat <<'EOF'
用法:
npm run dev:spacetime:logs
npm run dev:spacetime:logs -- --follow
./scripts/spacetime-logs-local.sh --lines 500 --output logs/spacetime/local.log
说明:
1. 从本地 SpacetimeDB 通过 spacetime logs 提取模块日志到本地文件。
2. 默认读取 spacetime.local.json 的 database 字段,默认 server 为 http://127.0.0.1:3101。
3. 默认输出到 logs/spacetime/<database>-<timestamp>.log--follow 会持续追加并同步写到终端。
4. 始终显式传入 server URL,不读取或改写 spacetime CLI 的个人 root 配置。
EOF
}
require_command() {
local command_name="$1"
if ! command -v "${command_name}" >/dev/null 2>&1; then
echo "[stdb:logs] 缺少命令: ${command_name}" >&2
exit 1
fi
}
read_local_spacetime_database() {
local config_path="${REPO_ROOT}/spacetime.local.json"
if [[ ! -f "${config_path}" ]]; then
return
fi
node -e '
const fs = require("fs");
const path = process.argv[1];
try {
const value = JSON.parse(fs.readFileSync(path, "utf8")).database;
if (typeof value === "string" && value.trim()) {
process.stdout.write(value.trim());
}
} catch (error) {
process.stderr.write(`[stdb:logs] ignore invalid spacetime.local.json: ${error.message}\n`);
}
' "${config_path}"
}
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
DATABASE=""
SPACETIME_SERVER="http://127.0.0.1:3101"
LINES="200"
OUTPUT=""
FOLLOW=0
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
--database)
DATABASE="${2:?缺少 --database 的值}"
shift 2
;;
--server)
SPACETIME_SERVER="${2:?缺少 --server 的值}"
shift 2
;;
--lines|-n)
LINES="${2:?缺少 --lines 的值}"
shift 2
;;
--output|-o)
OUTPUT="${2:?缺少 --output 的值}"
shift 2
;;
--follow|-f)
FOLLOW=1
shift
;;
*)
echo "[stdb:logs] 未知参数: $1" >&2
usage >&2
exit 1
;;
esac
done
require_command node
require_command spacetime
if [[ -z "${DATABASE//[[:space:]]/}" ]]; then
DATABASE="$(read_local_spacetime_database)"
fi
if [[ -z "${DATABASE//[[:space:]]/}" ]]; then
DATABASE="genarrative-dev"
fi
if [[ -z "${OUTPUT//[[:space:]]/}" ]]; then
LOG_DIR="${REPO_ROOT}/logs/spacetime"
mkdir -p "${LOG_DIR}"
TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
OUTPUT="${LOG_DIR}/${DATABASE}-${TIMESTAMP}.log"
else
mkdir -p "$(dirname -- "${OUTPUT}")"
fi
ARGS=(logs "${DATABASE}" --server "${SPACETIME_SERVER}" -n "${LINES}")
if [[ "${FOLLOW}" -eq 1 ]]; then
ARGS+=(-f)
fi
echo "[stdb:logs] database: ${DATABASE}"
echo "[stdb:logs] server: ${SPACETIME_SERVER}"
echo "[stdb:logs] output: ${OUTPUT}"
spacetime "${ARGS[@]}" | tee -a "${OUTPUT}"