fix(cli): resolve Bun compatibility issue with WriteStream stdio redirection

This commit is contained in:
yiling
2026-06-20 12:49:05 +08:00
parent 463cd61cfd
commit d4a1eff715
2 changed files with 18 additions and 25 deletions
+5 -19
View File
@@ -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<typeof import('node:fs')>()
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 () => {
+13 -6
View File
@@ -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<void>): Promise<void> {
export async function handleDaemonCommands(
appName: string,
runFn: () => Promise<void>,
): Promise<void> {
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