Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- Added ability to deactivate stdout and stderr #689

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cli/src/managers/console.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class ConsoleManager {
this.write(message + "\n");
}

writeTable(table: string[][]) {
console.table(table);
}

read(): string {
return process.stdin.read() as string;
}
Expand Down
19 changes: 12 additions & 7 deletions packages/cli/src/managers/shell.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class ShellManager {
directory?: string,
streamStdout?: boolean,
maxBuffer?: number,
outputStdout?: boolean,
outputStderr?: boolean,
}): Promise<string> {
return new Promise<string>((resolve, reject) => {
const env = process.env;
Expand All @@ -20,25 +22,28 @@ export class ShellManager {
const streamStdout = options?.streamStdout ?? false;
const directory = options?.directory;

const outputStdout = options?.outputStdout ?? true;
const outputStderr = options?.outputStderr ?? true;

if(directory) {
const absoluteDirectory = this.pathManager.getPathRelativeToCurrentExecutionDirectory(directory);
finalCommand = "cd " + absoluteDirectory + " && " + command;
}

this.consoleManager.writeLine(finalCommand);
outputStdout && this.consoleManager.writeLine(finalCommand);

if(streamStdout) {
const child = spawn(finalCommand, [], { shell: true, env });
child.stdout.on('data', (data) => {
this.consoleManager.writeLine(`${data}`);
outputStdout && this.consoleManager.writeLine(`${data}`);
});

child.stderr.on('data', (data) => {
this.consoleManager.writeLine(`Stderr: ${data}`);
outputStderr && this.consoleManager.writeLine(`Stderr: ${data}`);
});

child.on('close', (code) => {
this.consoleManager.writeLine(`Command exited with code ${code}`);
outputStdout && this.consoleManager.writeLine(`Command exited with code ${code}`);

if(code !== 0) {
return reject(code);
Expand All @@ -50,16 +55,16 @@ export class ShellManager {

return exec(finalCommand, {env, maxBuffer: options?.maxBuffer}, (error, stdout, stderr) => {
if (error && error.code) {
this.consoleManager.writeLine("Error: " + error.message);
outputStderr && this.consoleManager.writeLine("Error: " + error.message);
return reject(error);
}

if (stderr) {
this.consoleManager.writeLine("Stderr: " + stderr);
outputStderr && this.consoleManager.writeLine("Stderr: " + stderr);
return resolve(stderr);
}

this.consoleManager.writeLine(stdout);
outputStdout && this.consoleManager.writeLine(stdout);
return resolve(stdout);
})
})
Expand Down
Loading