From d4a1eff715d38252b83c26c6bde0c71743e0d9ff Mon Sep 17 00:00:00 2001 From: yiling Date: Sat, 20 Jun 2026 12:49:05 +0800 Subject: [PATCH] fix(cli): resolve Bun compatibility issue with WriteStream stdio redirection --- packages/core/src/utils/daemon.test.ts | 24 +++++------------------- packages/core/src/utils/daemon.ts | 19 +++++++++++++------ 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/packages/core/src/utils/daemon.test.ts b/packages/core/src/utils/daemon.test.ts index 1a281435..4bfc6287 100644 --- a/packages/core/src/utils/daemon.test.ts +++ b/packages/core/src/utils/daemon.test.ts @@ -1,12 +1,5 @@ import { describe, expect, it, vi, beforeEach, afterEach, type MockInstance } from 'vitest' -import { - existsSync, - writeFileSync, - readFileSync, - rmSync, - mkdirSync, - createWriteStream, -} from 'node:fs' +import { existsSync, writeFileSync, readFileSync, rmSync, mkdirSync, openSync } from 'node:fs' import { join } from 'node:path' import { handleDaemonCommands } from './daemon' import { spawn } from 'node:child_process' @@ -33,19 +26,12 @@ vi.mock('node:child_process', () => { } }) -// Mock node:fs to mock createWriteStream for ESM safety +// Mock node:fs to mock openSync for ESM safety vi.mock('node:fs', async (importOriginal) => { const actual = await importOriginal() - const { Writable } = await import('node:stream') return { ...actual, - createWriteStream: vi.fn(() => { - return new Writable({ - write(_chunk: unknown, _encoding: string, callback: (error?: Error | null) => void) { - callback() - }, - }) - }), + openSync: vi.fn(() => 42), } }) @@ -68,7 +54,7 @@ describe('handleDaemonCommands', () => { mockKill = vi.spyOn(process, 'kill').mockImplementation(() => true) vi.mocked(spawn).mockClear() - vi.mocked(createWriteStream).mockClear() + vi.mocked(openSync).mockClear() }) afterEach(() => { @@ -105,7 +91,7 @@ describe('handleDaemonCommands', () => { expect(runFn).not.toHaveBeenCalled() expect(mockKill).toHaveBeenCalledWith(12345, 'SIGTERM') expect(mockExit).toHaveBeenCalledWith(0) - expect(createWriteStream).not.toHaveBeenCalled() + expect(openSync).not.toHaveBeenCalled() }) it('daemonizes the process if -d is passed', async () => { diff --git a/packages/core/src/utils/daemon.ts b/packages/core/src/utils/daemon.ts index c8d16865..25b08dd6 100644 --- a/packages/core/src/utils/daemon.ts +++ b/packages/core/src/utils/daemon.ts @@ -8,7 +8,7 @@ import { watch, statSync, createReadStream, - createWriteStream, + openSync, } from 'node:fs' import { join } from 'node:path' import { homedir } from 'node:os' @@ -97,7 +97,9 @@ function startDaemon(appName: string, pidFile: string, logFile: string): void { if (!isNaN(pid)) { try { process.kill(pid, 0) - console.error(`${appName} is already running (PID: ${pid}). Use '${appName} stop' or '${appName} restart'.`) + console.error( + `${appName} is already running (PID: ${pid}). Use '${appName} stop' or '${appName} restart'.`, + ) process.exit(1) } catch { try { @@ -110,13 +112,13 @@ function startDaemon(appName: string, pidFile: string, logFile: string): void { } console.log(`Starting ${appName} in background...`) - const logStream = createWriteStream(logFile, { flags: 'a' }) + const logFd = openSync(logFile, 'a') const childArgs = process.argv.slice(1).filter((arg) => arg !== '-d' && arg !== 'restart') const child = spawn(process.argv[0], childArgs, { detached: true, - stdio: ['ignore', logStream, logStream], + stdio: ['ignore', logFd, logFd], env: { ...process.env, SHUMAI_DAEMONIZED: 'true', @@ -173,7 +175,10 @@ function viewLogs(appName: string, logFile: string): void { process.on('SIGTERM', cleanup) } -export async function handleDaemonCommands(appName: string, runFn: () => Promise): Promise { +export async function handleDaemonCommands( + appName: string, + runFn: () => Promise, +): Promise { const pidFile = join(PID_DIR, `${appName}.pid`) const logFile = join(LOG_DIR, `${appName}.log`) @@ -216,7 +221,9 @@ export async function handleDaemonCommands(appName: string, runFn: () => Promise if (!isNaN(pid)) { try { process.kill(pid, 0) - console.error(`${appName} is already running (PID: ${pid}). Use '${appName} stop' or '${appName} restart'.`) + console.error( + `${appName} is already running (PID: ${pid}). Use '${appName} stop' or '${appName} restart'.`, + ) process.exit(1) } catch { // Process is dead, clean up PID file