exbrain/vault-template/scripts/on-session-end.sh
Masahiro Chaen 2c2343e081 fix: resolve all errors from full repository audit
- README/README_JP: fix X bookmark schedule "22:00" → "every 4 hours"
- README: standardize skill name /auto-min → /auto-mins
- sync-x-bookmarks.sh: fix subshell variable scope (NEW_COUNT always 0)
  Use process substitution instead of pipe to preserve counter
- CLAUDE.md: add missing frontmatter (violated own schema rules)
- clips/_index.md: replace personal test data with clean template
- on-file-change.sh: replace bare except with specific exceptions
- on-session-end.sh: use context managers for file handles

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 23:29:08 +09:00

65 lines
1.8 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
# on-session-end.sh — Claude Code Stop hook
# セッション終了時にdaily noteにサマリーを追記する
# 呼び出し元: ~/.claude/settings.json → hooks.Stop (async: true)
set +e
VAULT_DIR="$HOME/vault"
DAILY_DIR="$VAULT_DIR/daily"
LOG="$VAULT_DIR/.sync.log"
TODAY=$(date +%Y-%m-%d)
DAILY_FILE="$DAILY_DIR/$TODAY.md"
NOW=$(date +%H:%M)
echo "[$(date '+%Y-%m-%d %H:%M:%S')] on-session-end" >> "$LOG"
# daily noteが存在しない場合は作成
if [ ! -f "$DAILY_FILE" ]; then
WEEKDAY=$(TODAY="$TODAY" python3 -c "
import os, datetime
d = datetime.date.fromisoformat(os.environ['TODAY'])
print(d.strftime('%A'))
")
printf -- "---\ndate: %s\nweekday: %s\n---\n\n## Schedule\n\n## Log\n\n## Thoughts\n\n## Links\n" "$TODAY" "$WEEKDAY" > "$DAILY_FILE"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Created daily note: $DAILY_FILE" >> "$LOG"
fi
# stdinからJSONを読み取るhookから渡される
INPUT=$(cat 2>/dev/null || true)
CWD=$(echo "${INPUT:-{}}" | python3 -c "import sys,json;print(json.load(sys.stdin).get('cwd',''))" 2>/dev/null || echo "")
LABEL=$([ -n "$CWD" ] && basename "$CWD" || echo "unknown")
ENTRY="- $NOW セッション終了 (cwd: $LABEL)"
# ## Log セクションに追記環境変数経由でPythonに渡す — シェル補間を避ける)
export DAILY_FILE ENTRY
python3 -c '
import os
f = os.environ["DAILY_FILE"]
e = os.environ["ENTRY"]
with open(f) as fh:
lines = fh.readlines()
out = []
in_log = False
done = False
for l in lines:
if l.strip() == "## Log":
in_log = True
out.append(l)
continue
if in_log and not done and l.startswith("## "):
out.append(e + "\n\n")
done = True
out.append(l)
if in_log and not done:
out.append(e + "\n\n")
with open(f, "w") as fh:
fh.writelines(out)
' 2>/dev/null || echo "$ENTRY" >> "$DAILY_FILE"
exit 0