11import type { TemplateConfig } from "../domain.js"
22
33const claudeAuthRootContainerPath = ( sshUser : string ) : string => `/home/${ sshUser } /.docker-git/.orch/auth/claude`
4+ const claudeHomeContainerPath = ( sshUser : string ) : string => `/home/${ sshUser } /.claude`
45
56const renderClaudeAuthConfig = ( config : TemplateConfig ) : string =>
67 String
3233export CLAUDE_CONFIG_DIR
3334
3435mkdir -p "$CLAUDE_CONFIG_DIR" || true
36+ CLAUDE_HOME_DIR="${ claudeHomeContainerPath ( config . sshUser ) } "
37+ CLAUDE_HOME_JSON="/home/${ config . sshUser } /.claude.json"
38+ mkdir -p "$CLAUDE_HOME_DIR" || true
39+
40+ docker_git_link_claude_file() {
41+ local source_path="$1"
42+ local link_path="$2"
43+
44+ # Preserve user-created regular files and seed config dir once.
45+ if [[ -e "$link_path" && ! -L "$link_path" ]]; then
46+ if [[ -f "$link_path" && ! -e "$source_path" ]]; then
47+ cp "$link_path" "$source_path" || true
48+ chmod 0600 "$source_path" || true
49+ fi
50+ return 0
51+ fi
52+
53+ ln -sfn "$source_path" "$link_path" || true
54+ }
55+
56+ docker_git_link_claude_home_file() {
57+ local relative_path="$1"
58+ local source_path="$CLAUDE_CONFIG_DIR/$relative_path"
59+ local link_path="$CLAUDE_HOME_DIR/$relative_path"
60+ docker_git_link_claude_file "$source_path" "$link_path"
61+ }
62+
63+ docker_git_link_claude_home_file ".oauth-token"
64+ docker_git_link_claude_home_file ".config.json"
65+ docker_git_link_claude_home_file ".claude.json"
66+ docker_git_link_claude_home_file ".credentials.json"
67+ docker_git_link_claude_file "$CLAUDE_CONFIG_DIR/.claude.json" "$CLAUDE_HOME_JSON"
3568
3669CLAUDE_TOKEN_FILE="$CLAUDE_CONFIG_DIR/.oauth-token"
70+ CLAUDE_CREDENTIALS_FILE="$CLAUDE_CONFIG_DIR/.credentials.json"
3771docker_git_refresh_claude_oauth_token() {
3872 local token=""
73+ if [[ -s "$CLAUDE_CREDENTIALS_FILE" ]]; then
74+ unset CLAUDE_CODE_OAUTH_TOKEN || true
75+ return 0
76+ fi
3977 if [[ -f "$CLAUDE_TOKEN_FILE" ]]; then
4078 token="$(tr -d '\r\n' < "$CLAUDE_TOKEN_FILE")"
4179 fi
42- export CLAUDE_CODE_OAUTH_TOKEN="$token"
80+ if [[ -n "$token" ]]; then
81+ export CLAUDE_CODE_OAUTH_TOKEN="$token"
82+ else
83+ unset CLAUDE_CODE_OAUTH_TOKEN || true
84+ fi
4385}
4486
4587docker_git_refresh_claude_oauth_token`
@@ -89,7 +131,7 @@ docker_git_ensure_claude_cli`
89131
90132const renderClaudeMcpPlaywrightConfig = ( ) : string =>
91133 String . raw `# Claude Code: keep Playwright MCP config in sync with container settings
92- CLAUDE_SETTINGS_FILE="$CLAUDE_CONFIG_DIR/.claude.json"
134+ CLAUDE_SETTINGS_FILE="${ "$" } {CLAUDE_HOME_JSON:-$ CLAUDE_CONFIG_DIR/.claude.json} "
93135docker_git_sync_claude_playwright_mcp() {
94136 CLAUDE_SETTINGS_FILE="$CLAUDE_SETTINGS_FILE" MCP_PLAYWRIGHT_ENABLE="$MCP_PLAYWRIGHT_ENABLE" node - <<'NODE'
95137const fs = require("node:fs")
@@ -248,8 +290,11 @@ set -euo pipefail
248290CLAUDE_REAL_BIN="__CLAUDE_REAL_BIN__"
249291CLAUDE_CONFIG_DIR="${ "$" } {CLAUDE_CONFIG_DIR:-$HOME/.claude}"
250292CLAUDE_TOKEN_FILE="$CLAUDE_CONFIG_DIR/.oauth-token"
293+ CLAUDE_CREDENTIALS_FILE="$CLAUDE_CONFIG_DIR/.credentials.json"
251294
252- if [[ -f "$CLAUDE_TOKEN_FILE" ]]; then
295+ if [[ -s "$CLAUDE_CREDENTIALS_FILE" ]]; then
296+ unset CLAUDE_CODE_OAUTH_TOKEN || true
297+ elif [[ -f "$CLAUDE_TOKEN_FILE" ]]; then
253298 CLAUDE_CODE_OAUTH_TOKEN="$(tr -d '\r\n' < "$CLAUDE_TOKEN_FILE")"
254299 export CLAUDE_CODE_OAUTH_TOKEN
255300else
@@ -270,7 +315,10 @@ printf "export CLAUDE_CONFIG_DIR=%q\n" "$CLAUDE_CONFIG_DIR" >> "$CLAUDE_PROFILE"
270315printf "export CLAUDE_AUTO_SYSTEM_PROMPT=%q\n" "$CLAUDE_AUTO_SYSTEM_PROMPT" >> "$CLAUDE_PROFILE"
271316cat <<'EOF' >> "$CLAUDE_PROFILE"
272317CLAUDE_TOKEN_FILE="${ "$" } {CLAUDE_CONFIG_DIR:-$HOME/.claude}/.oauth-token"
273- if [[ -f "$CLAUDE_TOKEN_FILE" ]]; then
318+ CLAUDE_CREDENTIALS_FILE="${ "$" } {CLAUDE_CONFIG_DIR:-$HOME/.claude}/.credentials.json"
319+ if [[ -s "$CLAUDE_CREDENTIALS_FILE" ]]; then
320+ unset CLAUDE_CODE_OAUTH_TOKEN || true
321+ elif [[ -f "$CLAUDE_TOKEN_FILE" ]]; then
274322 export CLAUDE_CODE_OAUTH_TOKEN="$(tr -d '\r\n' < "$CLAUDE_TOKEN_FILE")"
275323else
276324 unset CLAUDE_CODE_OAUTH_TOKEN || true
@@ -280,7 +328,7 @@ chmod 0644 "$CLAUDE_PROFILE" || true
280328
281329docker_git_upsert_ssh_env "CLAUDE_AUTH_LABEL" "$CLAUDE_AUTH_LABEL"
282330docker_git_upsert_ssh_env "CLAUDE_CONFIG_DIR" "$CLAUDE_CONFIG_DIR"
283- docker_git_upsert_ssh_env "CLAUDE_CODE_OAUTH_TOKEN" "$CLAUDE_CODE_OAUTH_TOKEN"
331+ docker_git_upsert_ssh_env "CLAUDE_CODE_OAUTH_TOKEN" "${ "$" } { CLAUDE_CODE_OAUTH_TOKEN:-} "
284332docker_git_upsert_ssh_env "CLAUDE_AUTO_SYSTEM_PROMPT" "$CLAUDE_AUTO_SYSTEM_PROMPT"`
285333
286334export const renderEntrypointClaudeConfig = ( config : TemplateConfig ) : string =>
0 commit comments