Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import 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]);}