Repository navigation
Expand file tree
/
Copy pathProcess.ts
More file actions
66 lines (64 loc) · 2.23 KB
/
Copy pathProcess.ts
File metadata and controls
66 lines (64 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import type { ExecOptions, ExecResult, ExecStream } from '@interfaces/index'
import { Readable } from 'node:stream'
import {
createDataHandlers,
createKillFunction,
createProcessPromiseWrapper,
createProcessState,
setupDataHandlers,
type DataHandlers,
type ProcessState
} from '@utils/index'
/**
* Creates a buffered process that collects all output before resolving.
* @param command - Command string to execute
* @param options - Execution options including timeout, cwd, and env
* @returns Object with promise and kill function
*/
export function createBufferedProcess(command: string, options: ExecOptions): ExecResult {
const state: ProcessState = createProcessState(command, options)
const kill: (signal?: string | number) => void = createKillFunction(state)
const promise: Promise<ExecResult> = createProcessPromiseWrapper(state, options, kill)
return {
stdout: '',
stderr: '',
exitCode: 0,
kill,
then: promise.then.bind(promise),
catch: promise.catch.bind(promise),
finally: promise.finally.bind(promise)
} as ExecResult & Promise<ExecResult>
}
/**
* Creates a streaming process that provides real-time output.
* @param command - Command string to execute
* @param options - Execution options including timeout, cwd, and env
* @returns Stream object with output iterable, promise, and kill function
*/
export function createStreamProcess(command: string, options: ExecOptions): ExecStream {
const state: ProcessState = createProcessState(command, options)
const outputStream: Readable = new Readable({
read(): void {
// This will be controlled by the data events
}
})
const handlers: DataHandlers = createDataHandlers('stream', outputStream)
setupDataHandlers(state.child, handlers)
state.child.on('close', (): void => {
outputStream.push(null)
})
const outputIterable: AsyncIterable<Buffer> = {
async *[Symbol.asyncIterator](): AsyncGenerator<Buffer> {
for await (const chunk of outputStream) {
yield chunk
}
}
}
const kill: (signal?: string | number) => void = createKillFunction(state)
const promise: Promise<ExecResult> = createProcessPromiseWrapper(state, options, kill)
return {
output: outputIterable,
promise,
kill
}
}