(
dir: string,
port?: number,
)
| 8579 | } |
| 8580 | |
| 8581 | async function startServerAtDirAndGetPort( |
| 8582 | dir: string, |
| 8583 | port?: number, |
| 8584 | ): Promise<{ port: number } | { error: string }> { |
| 8585 | const absDir = resolve(expandTilde(dir)); |
| 8586 | const lockDir = getLocalDir(absDir); |
| 8587 | |
| 8588 | const existingUi = readUiLock(lockDir); |
| 8589 | if (existingUi && existingUi.port > 0) { |
| 8590 | return { port: existingUi.port }; |
| 8591 | } |
| 8592 | |
| 8593 | const [cmd, ...baseArgs] = localOpCliArgs; |
| 8594 | const buildArgs = (cliCmd: 'ui' | 'start'): string[] => { |
| 8595 | const portFlag = |
| 8596 | port !== undefined |
| 8597 | ? cliCmd === 'ui' |
| 8598 | ? ['--port', String(port)] |
| 8599 | : ['--ui-port', String(port)] |
| 8600 | : []; |
| 8601 | return [...baseArgs, cliCmd, ...portFlag]; |
| 8602 | }; |
| 8603 | |
| 8604 | const spawnAndAwaitUi = async ( |
| 8605 | cliCmd: 'ui' | 'start', |
| 8606 | ): Promise<{ port: number } | { error: string; exited: boolean }> => { |
| 8607 | const child = spawn(cmd, buildArgs(cliCmd), { |
| 8608 | cwd: absDir, |
| 8609 | detached: true, |
| 8610 | stdio: ['ignore', 'ignore', 'pipe'], |
| 8611 | env: { ...process.env, OK_LOCK_KIND: 'interactive' }, |
| 8612 | }); |
| 8613 | |
| 8614 | const stderrChunks: Buffer[] = []; |
| 8615 | child.stderr?.on('data', (chunk: Buffer) => { |
| 8616 | stderrChunks.push(chunk); |
| 8617 | log.warn( |
| 8618 | { cwd: absDir, cliCmd, msg: chunk.toString('utf-8').trim() }, |
| 8619 | '[local-op/clone] child stderr', |
| 8620 | ); |
| 8621 | }); |
| 8622 | |
| 8623 | let earlyExitCode: number | null = null; |
| 8624 | let earlyExitSignal: NodeJS.Signals | null = null; |
| 8625 | let spawnErrorMessage: string | null = null; |
| 8626 | child.on('exit', (code, signal) => { |
| 8627 | earlyExitCode = code ?? -1; |
| 8628 | earlyExitSignal = signal ?? null; |
| 8629 | }); |
| 8630 | child.on('error', (err) => { |
| 8631 | spawnErrorMessage = err.message; |
| 8632 | earlyExitCode = -1; |
| 8633 | log.error( |
| 8634 | { cwd: absDir, cliCmd, err: err.message }, |
| 8635 | '[local-op/clone] failed to spawn child', |
| 8636 | ); |
| 8637 | }); |
| 8638 |
no test coverage detected