Skip to content

Commit 233ac79

Browse files
authored
fix(cli): replace execFile with spawn for improved SSH handling (#23)
1 parent 940fb88 commit 233ac79

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

cli/src/commands/login.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { execFile } from 'node:child_process'
1+
import { spawn } from 'node:child_process'
22
import fs from 'node:fs'
33
import http from 'node:http'
44
import https from 'node:https'
@@ -17,14 +17,14 @@ export async function login(positionals: string[], _flags: Record<string, string
1717

1818
const server = ssh.split('@').pop()!
1919

20-
const spin = spinner(`Connecting to ${ssh}...`)
20+
logInfo(`Connecting to ${ssh}...`)
2121
const jwt = await sshMintJwt(ssh)
2222
if (!jwt) {
23-
spin.stop()
2423
logError(`Connection failed — check that you can ssh to ${ssh}`)
2524
process.exit(1)
2625
}
2726

27+
const spin = spinner(`Resolving zero API on ${server}...`)
2828
const host = await resolveApiUrl(server, jwt)
2929
spin.stop()
3030
if (!host) {
@@ -42,8 +42,14 @@ const SSH_COMMAND =
4242

4343
function sshExec(ssh: string, command: string): Promise<{ stdout: string; ok: boolean }> {
4444
return new Promise((resolve) => {
45-
execFile('ssh', [ssh, command], { timeout: 30_000 }, (err, stdout) => {
46-
if (err) {
45+
const child = spawn('ssh', [ssh, command], { stdio: ['inherit', 'pipe', 'inherit'] })
46+
let stdout = ''
47+
child.stdout.on('data', (chunk) => {
48+
stdout += chunk.toString()
49+
})
50+
child.on('error', () => resolve({ stdout: '', ok: false }))
51+
child.on('close', (code) => {
52+
if (code !== 0) {
4753
resolve({ stdout: '', ok: false })
4854
return
4955
}

0 commit comments

Comments
 (0)