skillhub/cli/src/platform/updater.ts
dongmucat 87dbf2686b fix(cli): resolve Windows CI test failures across three platform issues
- Use Bun.which() with process.execPath fallback in run-cli helper to
  resolve bun executable on Windows (fixes 22 integration tests)
- Normalize paths in credentials-store test for Windows backslash compat
- Use shell: true on Windows in updater spawn for proper exe resolution
2026-04-29 16:03:09 +08:00

45 lines
1.5 KiB
TypeScript

import { spawn } from 'node:child_process'
import type { ChildProcess, SpawnOptions } from 'node:child_process'
export interface UpdaterRunResult {
success: boolean
output: string
}
/**
* Execute an update command without relying on sh -c.
* Splits the command string into program + args for cross-platform compatibility.
*/
export async function runUpdateCommand(command: string): Promise<UpdaterRunResult> {
try {
const [program, ...args] = command.split(/\s+/)
if (!program) {
return { success: false, output: 'empty update command' }
}
// On Windows, use shell: true to let the OS resolve the executable
// This handles both .exe and .cmd extensions automatically
const spawnOptions: SpawnOptions = {
stdio: ['ignore', 'pipe', 'pipe'],
...(process.platform === 'win32' && { shell: true })
}
return await new Promise<UpdaterRunResult>((resolve) => {
const proc: ChildProcess = spawn(program, args, spawnOptions)
const chunks: Buffer[] = []
proc.stdout?.on('data', (chunk: Buffer) => chunks.push(Buffer.from(chunk)))
proc.stderr?.on('data', (chunk: Buffer) => chunks.push(Buffer.from(chunk)))
proc.on('error', (error: Error) => resolve({ success: false, output: error.message }))
proc.on('close', (code: number | null) => resolve({
success: code === 0,
output: Buffer.concat(chunks).toString('utf-8')
}))
})
} catch (error) {
return {
success: false,
output: error instanceof Error ? error.message : String(error)
}
}
}