refactor: extract installScript helper function

This commit is contained in:
Chris Wanstrath 2026-03-12 07:45:16 -07:00
parent ac745d02f6
commit 3fc59774a5

View File

@ -144,6 +144,15 @@ async function installTooling(cached: boolean, log?: (msg: string) => void): Pro
await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${"cp ~/.local/bin/bun ~/.local/bin/claude ~/.local/bin/neofetch /sandlot/.cache/ && cp /tmp/nvim.tar.gz /sandlot/.cache/nvim.tar.gz"}`.nothrow().quiet() await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${"cp ~/.local/bin/bun ~/.local/bin/claude ~/.local/bin/neofetch /sandlot/.cache/ && cp /tmp/nvim.tar.gz /sandlot/.cache/nvim.tar.gz"}`.nothrow().quiet()
} }
/** Write a script to a temp file, copy it into ~/.local/bin/ in the container, then clean up. */
async function installScript(home: string, name: string, content: string): Promise<void> {
const tmp = `${home}/.sandlot/.${name}.tmp`
await Bun.write(tmp, content)
await $`chmod +x ${tmp}`.quiet()
await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${`cp /sandlot/.${name}.tmp ~/.local/bin/${name}`}`.quiet()
await Bun.file(tmp).unlink()
}
/** Configure git identity, API key helper, activity hook, and Claude settings. */ /** Configure git identity, API key helper, activity hook, and Claude settings. */
async function configureEnvironment(home: string, apiKey: string): Promise<void> { async function configureEnvironment(home: string, apiKey: string): Promise<void> {
const gitName = (await $`git config user.name`.quiet().text()).trim() const gitName = (await $`git config user.name`.quiet().text()).trim()
@ -157,9 +166,7 @@ async function configureEnvironment(home: string, apiKey: string): Promise<void>
Stop: [{ hooks: [{ type: "command", command: `${activityBin} idle` }] }], Stop: [{ hooks: [{ type: "command", command: `${activityBin} idle` }] }],
PostToolUseFailure: [{ hooks: [{ type: "command", command: `${activityBin} idle` }] }], PostToolUseFailure: [{ hooks: [{ type: "command", command: `${activityBin} idle` }] }],
} }
// Status line: show current branch in the Claude Code powerline const statusLine = { type: "command", command: `/home/${USER}/.local/bin/sandlot-statusline` }
const statusLineBin = `/home/${USER}/.local/bin/sandlot-statusline`
const statusLine = { type: "command", command: statusLineBin }
const settingsJson = JSON.stringify({ apiKeyHelper: "~/.claude/api-key-helper.sh", skipDangerousModePermissionPrompt: true, hooks, statusLine }) const settingsJson = JSON.stringify({ apiKeyHelper: "~/.claude/api-key-helper.sh", skipDangerousModePermissionPrompt: true, hooks, statusLine })
const claudeJson = JSON.stringify({ hasCompletedOnboarding: true, effortCalloutDismissed: true, projects: { "/": { hasTrustDialogAccepted: true } } }) const claudeJson = JSON.stringify({ hasCompletedOnboarding: true, effortCalloutDismissed: true, projects: { "/": { hasTrustDialogAccepted: true } } })
@ -171,30 +178,8 @@ async function configureEnvironment(home: string, apiKey: string): Promise<void>
await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${"mkdir -p ~/.claude && cp /sandlot/.api-key-helper.tmp ~/.claude/api-key-helper.sh"}`.quiet() await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${"mkdir -p ~/.claude && cp /sandlot/.api-key-helper.tmp ~/.claude/api-key-helper.sh"}`.quiet()
await Bun.file(tmp).unlink() await Bun.file(tmp).unlink()
// Install activity tracking hook script await installScript(home, "sandlot-activity", `#!/bin/bash\nP="\${CLAUDE_PROJECT_DIR%/}"\necho "$1" > "$(dirname "$P")/.activity-$(basename "$P")"\n`)
const activityTmp = `${home}/.sandlot/.sandlot-activity.tmp` await installScript(home, "sandlot-statusline", `#!/bin/bash\ninput=$(cat)\nbranch=$(echo "$input" | grep -oP '"branch"\\s*:\\s*"\\K[^"]+' | head -1)\n[ -n "$branch" ] && printf '\\033[36m\u2387 %s\\033[0m\\n' "$branch"\n`)
await Bun.write(activityTmp, [
'#!/bin/bash',
'P="${CLAUDE_PROJECT_DIR%/}"',
'echo "$1" > "$(dirname "$P")/.activity-$(basename "$P")"',
'',
].join('\n'))
await $`chmod +x ${activityTmp}`.quiet()
await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${"cp /sandlot/.sandlot-activity.tmp ~/.local/bin/sandlot-activity"}`.quiet()
await Bun.file(activityTmp).unlink()
// Install statusline script (shows branch in Claude Code powerline)
const statuslineTmp = `${home}/.sandlot/.sandlot-statusline.tmp`
await Bun.write(statuslineTmp, [
'#!/bin/bash',
'input=$(cat)',
'branch=$(echo "$input" | grep -oP \'"branch"\\s*:\\s*"\\K[^"]+\' | head -1)',
'[ -n "$branch" ] && printf \'\\033[36m⎇ %s\\033[0m\\n\' "$branch"',
'',
].join('\n'))
await $`chmod +x ${statuslineTmp}`.quiet()
await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${"cp /sandlot/.sandlot-statusline.tmp ~/.local/bin/sandlot-statusline"}`.quiet()
await Bun.file(statuslineTmp).unlink()
await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${` await $`container exec --user ${USER} ${CONTAINER_NAME} bash -c ${`
mkdir -p ~/.claude mkdir -p ~/.claude