Skip to content

Commit f0b4e42

Browse files
authored
Merge pull request #15 from NoTaskStudios/fix/command-streaming
Fix/command streaming
2 parents 1a6dc17 + ba21b46 commit f0b4e42

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

eslint.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default defineConfig([
3333
"@typescript-eslint/no-explicit-any": "off",
3434
"@typescript-eslint/strict-boolean-expressions": "off",
3535
"@typescript-eslint/no-floating-promises": "error",
36-
"@typescript-eslint/prefer-nullish-coalescing": "error",
36+
"@typescript-eslint/prefer-nullish-coalescing": "warn",
3737
"@typescript-eslint/prefer-optional-chain": "error",
3838
"@typescript-eslint/no-unnecessary-type-assertion": "error",
3939
"@typescript-eslint/no-unnecessary-condition": "warn",

src/unityEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "fs-extra";
33
import path from "path";
44
import { ProjectInfo, TestMode, UnityBuildTarget, UnityEditorInfo } from "./types/unity.js";
55
import { CommandOptions, CommandResult, executeCommand } from "./utils/commandExecutor.js";
6-
import { redactSensitiveArgs } from "utils/security.js";
6+
import { redactSensitiveArgs } from "./utils/security.js";
77

88
/**
99
* UnityEditor class provides a comprehensive interface for interacting with the Unity game engine editor

src/unityHub.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,16 @@ class UnityHub {
234234
throw new Error("No module IDs provided.");
235235
}
236236

237-
const { stdout, stderr } = await this.execUnityHubCommand(args, {
237+
const { stderr } = await this.execUnityHubCommand(args, {
238238
reject: false,
239+
onStderr: (data: string) => {
240+
console.warn(`Unity Hub stderr: ${data}`);
241+
},
242+
onStdout: (data: string) => {
243+
console.debug(`Unity Hub stdout: ${data}`);
244+
},
239245
});
240246

241-
console.debug(`Add module command output: ${stdout}`);
242-
243247
if (stderr) {
244248
console.warn(`Add module command warning/error: ${stderr}`);
245249
}
@@ -286,6 +290,12 @@ class UnityHub {
286290

287291
const { stdout, stderr } = await this.execUnityHubCommand(args, {
288292
reject: false,
293+
onStderr: (data: string) => {
294+
console.warn(`Unity Hub stderr: ${data}`);
295+
},
296+
onStdout: (data: string) => {
297+
console.debug(`Unity Hub stdout: ${data}`);
298+
},
289299
});
290300

291301
if (stderr) {

src/utils/commandExecutor.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Options, execa } from "execa";
33
export interface CommandOptions extends Options {
44
reject?: boolean;
55
timeout?: number;
6+
onStdout?: (data: string) => void;
7+
onStderr?: (data: string) => void;
68
env?: Record<string, string>;
79
cwd?: string;
810
}
@@ -20,26 +22,44 @@ export async function executeCommand(
2022
options: CommandOptions = {}
2123
): Promise<CommandResult> {
2224
try {
25+
const streamOutput = options.onStdout || options.onStderr;
26+
2327
const subprocess = execa(executable, args, {
2428
reject: options.reject ?? false,
2529
timeout: options.timeout,
2630
env: options.env,
2731
cwd: options.cwd,
2832
encoding: "utf8",
33+
buffer: !streamOutput,
2934
});
3035

31-
/*
32-
// Pipe the output to the parent process
33-
// This is commented out to avoid cluttering the output
34-
// Uncomment if you want to see the output in real-time
35-
if (subprocess.stdout) {
36-
subprocess.stdout.pipe(process.stdout);
37-
}
36+
if (streamOutput) {
37+
if (subprocess.stdout) {
38+
subprocess.stdout.on("data", (data: Buffer) => {
39+
const lines = data.toString().split(/\r?\n/);
40+
for (const line of lines) {
41+
if (line.trim()) {
42+
if (options.onStdout) {
43+
options.onStdout(line);
44+
}
45+
}
46+
}
47+
});
48+
}
3849

39-
if (subprocess.stderr) {
40-
subprocess.stderr.pipe(process.stderr);
50+
if (subprocess.stderr) {
51+
subprocess.stderr.on("data", (data: Buffer) => {
52+
const lines = data.toString().split(/\r?\n/);
53+
for (const line of lines) {
54+
if (line.trim()) {
55+
if (options.onStderr) {
56+
options.onStderr(line);
57+
}
58+
}
59+
}
60+
});
61+
}
4162
}
42-
*/
4363

4464
const { stdout, stderr, exitCode } = await subprocess;
4565

0 commit comments

Comments
 (0)