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

- Improve how errors are handled in the shell.manager #693

Merged
merged 2 commits into from
Apr 17, 2024
Merged
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
25 changes: 15 additions & 10 deletions packages/cli/src/managers/shell.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ export class ShellManager {
const outputDuration = options?.outputDuration ?? true;
const outputTimeBeforeExecutingCommand = options?.outputTimeBeforeExecutingCommand ?? true;

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

const start = new Date();

if(outputTimeBeforeExecutingCommand) {
if (outputTimeBeforeExecutingCommand) {
this.consoleManager.writeLine(start.toISOString() + ": " + finalCommand);
} else {
outputStdout && this.consoleManager.writeLine(finalCommand);
}


if(streamStdout) {
const child = spawn(finalCommand, [], { shell: true, env });
if (streamStdout) {
const child = spawn(finalCommand, [], {shell: true, env});
child.stdout.on('data', (data) => {
outputStdout && this.consoleManager.writeLine(`${data}`);
});
Expand All @@ -59,12 +59,12 @@ export class ShellManager {
child.on('close', (code) => {
outputStdout && this.consoleManager.writeLine(`Command exited with code ${code}`);

if(code !== 0) {
if (code !== 0) {
return reject(code);
}

// Output the duration in human readable format
if(outputDuration) {
if (outputDuration) {
const end = new Date();
const duration = end.getTime() - start.getTime();
this.consoleManager.writeLine(`Executed in: ${this.dateUtil.formatDuration(duration)}`);
Expand All @@ -77,22 +77,27 @@ export class ShellManager {
return exec(finalCommand, {env, maxBuffer: options?.maxBuffer}, (error, stdout, stderr) => {
if (error && error.code) {
outputStderr && this.consoleManager.writeLine("Error: " + error.message);
return reject(error);
}

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

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

// Output the duration in human readable format
if(outputDuration) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't output the duration anymore ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, there was a merge issue.

if (outputDuration) {
const end = new Date();
const duration = end.getTime() - start.getTime();
this.consoleManager.writeLine(`Executed in: ${this.dateUtil.formatDuration(duration)}`);
}

outputStdout && this.consoleManager.writeLine(stdout);
if (error && error.code) {
return reject(error);
} else if (stderr) {
return resolve(stderr);
}

return resolve(stdout);
})
})
Expand Down
Loading