From cd9e4a1d20304b646a9a1ce99e98cbee136196c4 Mon Sep 17 00:00:00 2001 From: Charmunks Date: Sat, 6 Dec 2025 16:19:08 -0500 Subject: [PATCH] hackatime autosetup --- code-server-setup.sh | 12 ++++++++++++ src/utils/spaces.js | 20 +++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/code-server-setup.sh b/code-server-setup.sh index c80f3dd..0ea74e9 100644 --- a/code-server-setup.sh +++ b/code-server-setup.sh @@ -1,9 +1,12 @@ #!/bin/bash # runs inside of created code-server containers, installs essential dev tools by default +# Usage: ./code-server-setup.sh [HACKATIME_API_KEY] set -e +HACKATIME_API_KEY="${1:-}" + echo "Starting development environment setup..." echo "Updating package manager..." @@ -98,6 +101,15 @@ gem install rails sudo npm install pnpm +# Set up Hackatime if API key is provided +if [ -n "$HACKATIME_API_KEY" ]; then + echo "⏱️ Setting up Hackatime..." + export HACKATIME_API_KEY="$HACKATIME_API_KEY" + export HACKATIME_API_URL="https://hackatime.hackclub.com/api/hackatime/v1" + export SUCCESS_URL="https://hackatime.hackclub.com//success.txt" + curl -sSL https://hackatime.hackclub.com/hackatime/setup.sh | bash +fi + # Final cleanup echo "🧹 Cleaning up..." sudo apt autoremove -y diff --git a/src/utils/spaces.js b/src/utils/spaces.js index fdaef5c..9e13187 100644 --- a/src/utils/spaces.js +++ b/src/utils/spaces.js @@ -101,8 +101,9 @@ export const createContainer = async (password, type, authorization) => { const setupScriptPath = path.join(__dirname, "../../code-server-setup.sh"); const setupScript = fs.readFileSync(setupScriptPath, "utf8"); + const hackatimeApiKey = user.hackatime_api_key || ""; const exec = await container.exec({ - Cmd: ["bash", "-c", `cat > /tmp/setup.sh << 'EOF'\n${setupScript}\nEOF\nchmod +x /tmp/setup.sh && /tmp/setup.sh > /app/postinstall.log 2>&1`], + Cmd: ["bash", "-c", `cat > /tmp/setup.sh << 'EOF'\n${setupScript}\nEOF\nchmod +x /tmp/setup.sh && /tmp/setup.sh '${hackatimeApiKey}' > /app/postinstall.log 2>&1`], AttachStdout: true, AttachStderr: true, }); @@ -221,6 +222,23 @@ export const startContainer = async (spaceId, authorization) => { await container.inspect(); await container.start(); + + if (space.type === "code-server" && user.hackatime_api_key) { + try { + console.log("Setting up Hackatime for code-server container..."); + const hackatimeApiKey = user.hackatime_api_key; + const exec = await container.exec({ + Cmd: ["bash", "-c", `export HACKATIME_API_KEY='${hackatimeApiKey}' && export HACKATIME_API_URL="https://hackatime.hackclub.com/api/hackatime/v1" && export SUCCESS_URL="https://hackatime.hackclub.com//success.txt" && curl -sSL https://hackatime.hackclub.com/hackatime/setup.sh | bash`], + AttachStdout: true, + AttachStderr: true, + }); + const stream = await exec.start(); + stream.pipe(process.stdout); + console.log("Hackatime setup executed successfully"); + } catch (hackatimeErr) { + console.error("Failed to set up Hackatime (container will still start):", hackatimeErr); + } + } // Update running status and started_at timestamp in database await pg('spaces')