import type { CliConfig, ContainerRuntime } from './config.js'; import { readCommand, runInteractive, spawnInherited, tryReadCommand } from './utils/shell.js'; function composeArgs(config: CliConfig, opts?: { cloudflare?: boolean }): string[] { const args = [...config.runtime.compose, '-f', config.composeFile]; if (opts?.cloudflare) { args.push('-f', config.cloudflareComposeFile); } return args; } // Resolves a hostname the browser can actually reach the substrate at. On macOS we prefer // the mDNS (.local) name so Caddy's cert and any LAN client keep matching. function getSubstrateHost(): string { if (process.platform === 'darwin') { const scutilName = tryReadCommand('scutil', ['--get', 'LocalHostName']); if (scutilName) return `${scutilName}.local`; const shortName = readCommand('hostname', ['-s']); return `${shortName}.local`; } return tryReadCommand('hostname', ['-f']) || readCommand('hostname', []); } export function runtimeCheck(runtime: ContainerRuntime): { ok: boolean; version?: string; error?: string } { try { const raw = readCommand(runtime.cmd, ['--version']); const version = raw.replace(/^[^0-9]*/, '').split(',')[0].trim() || raw; readCommand(runtime.cmd, ['info']); return { ok: true, version }; } catch { return { ok: false, error: `${runtime.label} not found or not running (command: ${runtime.cmd})` }; } } export function composeCheck(runtime: ContainerRuntime): { ok: boolean; version?: string } { try { const version = readCommand(runtime.cmd, [...runtime.compose, 'version', '--short']); return { ok: true, version }; } catch { // Older podman/podman-compose don't support `--short`; fall back to a loose version probe. try { const raw = readCommand(runtime.cmd, [...runtime.compose, 'version']); const match = raw.match(/\d+\.\d+\.\d+/); return { ok: true, version: match?.[0] || raw.split('\n')[0].trim() }; } catch { return { ok: false }; } } } export function composeBuild(config: CliConfig, noCache = false, opts?: { cloudflare?: boolean }): void { const args = [...composeArgs(config, opts), 'build']; if (noCache) args.push('--no-cache'); runInteractive(config.runtime.cmd, args, { cwd: config.substrateDir }); } export function composeUp(config: CliConfig, opts?: { cloudflare?: boolean }): void { const host = getSubstrateHost(); const args = [...composeArgs(config, opts), 'up', '-d']; runInteractive(config.runtime.cmd, args, { cwd: config.substrateDir, env: { ...process.env, SUBSTRATE_HOST: host }, }); } export function composeRestart(config: CliConfig, opts?: { cloudflare?: boolean }): void { composeDown(config, opts); composeUp(config, opts); } export function composeDown(config: CliConfig, opts?: { cloudflare?: boolean }): void { runInteractive(config.runtime.cmd, [...composeArgs(config, opts), 'down'], { cwd: config.substrateDir, }); } export function composeLogs( config: CliConfig, opts?: { cloudflare?: boolean; follow?: boolean; service?: string; tail?: number }, ): void { const args = [...composeArgs(config, opts), 'logs']; const follow = opts?.follow !== false; const tail = opts?.tail ?? 100; if (follow) args.push('-f'); args.push('--tail', String(tail)); if (opts?.service) { args.push(opts.service); } spawnInherited(config.runtime.cmd, args, { cwd: config.substrateDir }); } export function composeShell(config: CliConfig): void { runInteractive(config.runtime.cmd, [...composeArgs(config), 'exec', 'substrate', 'sh'], { cwd: config.substrateDir, }); } export function composePs(config: CliConfig): string { return readCommand(config.runtime.cmd, [ ...composeArgs(config), 'ps', '--format', 'table {{.Name}}\\t{{.Status}}\\t{{.Ports}}', ], config.substrateDir); } export function trustCaddyCert(config: CliConfig): void { if (process.platform !== 'darwin') { throw new Error('Auto-trust is only supported on macOS'); } const certPath = `${config.substrateDir}/caddy-root.crt`; runInteractive(config.runtime.cmd, [ ...config.runtime.compose, '-f', config.composeFile, 'cp', 'caddy:/data/caddy/pki/authorities/local/root.crt', certPath, ], { cwd: config.substrateDir }); runInteractive('sudo', [ 'security', 'add-trusted-cert', '-d', '-r', 'trustRoot', '-k', '/Library/Keychains/System.keychain', certPath, ]); runInteractive('rm', ['-f', certPath]); }