|
| 1 | +import type * as CommandExecutor from "@effect/platform/CommandExecutor" |
| 2 | +import type { PlatformError } from "@effect/platform/Error" |
| 3 | +import * as FileSystem from "@effect/platform/FileSystem" |
| 4 | +import * as Path from "@effect/platform/Path" |
| 5 | +import { Duration, Effect, pipe, Schedule } from "effect" |
| 6 | + |
| 7 | +import { |
| 8 | + type CreateCommand, |
| 9 | + defaultTemplateConfig, |
| 10 | + deriveRepoSlug, |
| 11 | + type TemplateConfig |
| 12 | +} from "@effect-template/lib/core/domain" |
| 13 | +import type { SpawnCommand } from "@effect-template/lib/core/spawn-domain" |
| 14 | +import { runCommandCapture, runCommandExitCode } from "@effect-template/lib/shell/command-runner" |
| 15 | +import { readProjectConfig } from "@effect-template/lib/shell/config" |
| 16 | +import { CommandFailedError, SpawnProjectDirError, SpawnSetupError } from "@effect-template/lib/shell/errors" |
| 17 | +import { createProject } from "@effect-template/lib/usecases/actions" |
| 18 | +import { findSshPrivateKey } from "@effect-template/lib/usecases/path-helpers" |
| 19 | +import { getContainerIpIfInsideContainer } from "@effect-template/lib/usecases/projects-core" |
| 20 | + |
| 21 | +import { spawnAttachTmux } from "./tmux.js" |
| 22 | + |
| 23 | +const SPAWNDOCK_REPO_URL = "https://github.com/SpawnDock/tma-project" |
| 24 | +const SPAWNDOCK_REPO_REF = "main" |
| 25 | + |
| 26 | +// remoteCommand = undefined → probe mode (ssh -T BatchMode + "true"), string → execute mode |
| 27 | +const buildSshArgs = ( |
| 28 | + template: TemplateConfig, |
| 29 | + sshKey: string | null, |
| 30 | + ipAddress: string | undefined, |
| 31 | + remoteCommand?: string |
| 32 | +): ReadonlyArray<string> => { |
| 33 | + const host = ipAddress ?? "localhost" |
| 34 | + const port = ipAddress ? 22 : template.sshPort |
| 35 | + const args: Array<string> = [] |
| 36 | + if (sshKey !== null) { |
| 37 | + args.push("-i", sshKey) |
| 38 | + } |
| 39 | + if (remoteCommand === undefined) { |
| 40 | + args.push("-T", "-o", "ConnectTimeout=2", "-o", "ConnectionAttempts=1") |
| 41 | + } |
| 42 | + args.push( |
| 43 | + "-o", |
| 44 | + "BatchMode=yes", |
| 45 | + "-o", |
| 46 | + "LogLevel=ERROR", |
| 47 | + "-o", |
| 48 | + "StrictHostKeyChecking=no", |
| 49 | + "-o", |
| 50 | + "UserKnownHostsFile=/dev/null", |
| 51 | + "-p", |
| 52 | + String(port), |
| 53 | + `${template.sshUser}@${host}`, |
| 54 | + remoteCommand ?? "true" |
| 55 | + ) |
| 56 | + return args |
| 57 | +} |
| 58 | + |
| 59 | +const waitForSshReady = ( |
| 60 | + template: TemplateConfig, |
| 61 | + sshKey: string | null, |
| 62 | + ipAddress?: string |
| 63 | +): Effect.Effect<void, CommandFailedError | PlatformError, CommandExecutor.CommandExecutor> => { |
| 64 | + const host = ipAddress ?? "localhost" |
| 65 | + const port = ipAddress ? 22 : template.sshPort |
| 66 | + const probe = Effect.gen(function*(_) { |
| 67 | + const exitCode = yield* _( |
| 68 | + runCommandExitCode({ |
| 69 | + cwd: process.cwd(), |
| 70 | + command: "ssh", |
| 71 | + args: buildSshArgs(template, sshKey, ipAddress) |
| 72 | + }) |
| 73 | + ) |
| 74 | + if (exitCode !== 0) { |
| 75 | + return yield* _(Effect.fail(new CommandFailedError({ command: "ssh wait", exitCode }))) |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + return pipe( |
| 80 | + Effect.log(`Waiting for SSH on ${host}:${port} ...`), |
| 81 | + Effect.zipRight( |
| 82 | + Effect.retry( |
| 83 | + probe, |
| 84 | + pipe( |
| 85 | + Schedule.spaced(Duration.seconds(2)), |
| 86 | + Schedule.intersect(Schedule.recurs(30)) |
| 87 | + ) |
| 88 | + ) |
| 89 | + ), |
| 90 | + Effect.tap(() => Effect.log("SSH is ready.")) |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +const parseProjectDir = (output: string): string | null => { |
| 95 | + const match = /SpawnDock project created at (.+)/.exec(output) |
| 96 | + return match?.[1]?.trim() ?? null |
| 97 | +} |
| 98 | + |
| 99 | +const buildSpawnCreateCommand = (outDir: string): CreateCommand => { |
| 100 | + const repoSlug = deriveRepoSlug(SPAWNDOCK_REPO_URL) |
| 101 | + const containerName = `dg-${repoSlug}` |
| 102 | + const serviceName = `dg-${repoSlug}` |
| 103 | + const volumeName = `dg-${repoSlug}-home` |
| 104 | + |
| 105 | + return { |
| 106 | + _tag: "Create", |
| 107 | + config: { |
| 108 | + ...defaultTemplateConfig, |
| 109 | + repoUrl: SPAWNDOCK_REPO_URL, |
| 110 | + repoRef: SPAWNDOCK_REPO_REF, |
| 111 | + containerName, |
| 112 | + serviceName, |
| 113 | + volumeName |
| 114 | + }, |
| 115 | + outDir, |
| 116 | + runUp: true, |
| 117 | + force: false, |
| 118 | + forceEnv: false, |
| 119 | + waitForClone: true, |
| 120 | + openSsh: false |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// CHANGE: orchestrate spawn-dock spawn — creates container, runs @spawn-dock/create, opens tmux+opencode |
| 125 | +// WHY: provide one-command bootstrap from a Telegram bot pairing token |
| 126 | +// REF: spawn-command |
| 127 | +// PURITY: SHELL |
| 128 | +// EFFECT: Effect<void, SpawnProjectDirError | SpawnSetupError | ..., CommandExecutor | FileSystem | Path> |
| 129 | +// INVARIANT: container is started before SSH connection; tmux session opens after successful bootstrap |
| 130 | +// COMPLEXITY: O(1) + docker + ssh |
| 131 | +export const spawnProject = (command: SpawnCommand) => |
| 132 | + Effect.gen(function*(_) { |
| 133 | + const fs = yield* _(FileSystem.FileSystem) |
| 134 | + const path = yield* _(Path.Path) |
| 135 | + |
| 136 | + yield* _(Effect.log("Creating SpawnDock container...")) |
| 137 | + const syntheticCreate = buildSpawnCreateCommand(command.outDir) |
| 138 | + yield* _(createProject(syntheticCreate)) |
| 139 | + |
| 140 | + const resolvedOutDir = path.resolve(command.outDir) |
| 141 | + const projectConfig = yield* _(readProjectConfig(resolvedOutDir)) |
| 142 | + const template = projectConfig.template |
| 143 | + |
| 144 | + const containerIpRaw = yield* _( |
| 145 | + getContainerIpIfInsideContainer(fs, process.cwd(), template.containerName).pipe( |
| 146 | + Effect.map((ip) => ip ?? ""), |
| 147 | + Effect.orElse(() => Effect.succeed("")) |
| 148 | + ) |
| 149 | + ) |
| 150 | + const ipAddress: string | undefined = containerIpRaw.length > 0 ? containerIpRaw : undefined |
| 151 | + |
| 152 | + const sshKey = yield* _(findSshPrivateKey(fs, path, process.cwd())) |
| 153 | + |
| 154 | + yield* _(waitForSshReady(template, sshKey, ipAddress)) |
| 155 | + |
| 156 | + const createCmd = `npx -y @spawn-dock/create@beta --token ${command.token}` |
| 157 | + yield* _(Effect.log("Running @spawn-dock/create inside container...")) |
| 158 | + |
| 159 | + const output = yield* _( |
| 160 | + runCommandCapture( |
| 161 | + { |
| 162 | + cwd: process.cwd(), |
| 163 | + command: "ssh", |
| 164 | + args: buildSshArgs(template, sshKey, ipAddress, createCmd) |
| 165 | + }, |
| 166 | + [0], |
| 167 | + (exitCode) => new SpawnSetupError({ exitCode }) |
| 168 | + ) |
| 169 | + ) |
| 170 | + |
| 171 | + const projectDir = parseProjectDir(output) |
| 172 | + if (projectDir === null) { |
| 173 | + return yield* _(Effect.fail(new SpawnProjectDirError({ output }))) |
| 174 | + } |
| 175 | + |
| 176 | + yield* _(Effect.log(`Project bootstrapped at ${projectDir}`)) |
| 177 | + yield* _(spawnAttachTmux(template, projectDir, sshKey)) |
| 178 | + }) |
0 commit comments