Skip to content

Commit

Permalink
feat(core): add log lines grouping for GH Actions (#21357)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Feb 2, 2024
1 parent 6e4bf8c commit 7739ce0
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ export async function createRunOneDynamicOutputRenderer({

lifeCycle.printTaskTerminalOutput = (task, cacheStatus, terminalOutput) => {
if (task.target.project === initiatingProject) {
output.logCommand(task.id, cacheStatus);
process.stdout.write(terminalOutput);
output.logCommandOutput(task.id, cacheStatus, terminalOutput);
} else {
tasksToTerminalOutputs[task.id] = terminalOutput;
}
Expand Down Expand Up @@ -254,8 +253,11 @@ export async function createRunOneDynamicOutputRenderer({
clearRenderInterval();
renderDependentTargets(false);
output.addVerticalSeparator('red');
output.logCommand(t.task.id, t.status);
process.stdout.write(tasksToTerminalOutputs[t.task.id]);
output.logCommandOutput(
t.task.id,
t.status,
tasksToTerminalOutputs[t.task.id]
);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ export class EmptyTerminalOutputLifeCycle implements LifeCycle {
cacheStatus === 'skipped'
) {
const args = getPrintableCommandArgsForTask(task);
output.logCommand(args.join(' '), cacheStatus);
process.stdout.write(terminalOutput);
output.logCommandOutput(args.join(' '), cacheStatus, terminalOutput);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export class InvokeRunnerTerminalOutputLifeCycle implements LifeCycle {
terminalOutput: string
) {
const args = getPrintableCommandArgsForTask(task);
output.logCommand(args.join(' '), cacheStatus);
process.stdout.write(terminalOutput);
output.logCommandOutput(args.join(' '), cacheStatus, terminalOutput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export class StaticRunManyTerminalOutputLifeCycle implements LifeCycle {
terminalOutput: string
) {
const args = getPrintableCommandArgsForTask(task);
output.logCommand(args.join(' '), cacheStatus);
process.stdout.write(terminalOutput);
output.logCommandOutput(args.join(' '), cacheStatus, terminalOutput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ export class StaticRunOneTerminalOutputLifeCycle implements LifeCycle {
task.target.project === this.initiatingProject
) {
const args = getPrintableCommandArgsForTask(task);
output.logCommand(args.join(' '), status);
process.stdout.write(terminalOutput);
output.logCommandOutput(args.join(' '), status, terminalOutput);
}
}
}
52 changes: 47 additions & 5 deletions packages/nx/src/utils/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import * as readline from 'readline';
import { isCI } from './is-ci';
import { TaskStatus } from '../tasks-runner/tasks-runner';

const GH_GROUP_PREFIX = '::group::';
const GH_GROUP_SUFFIX = '::endgroup::';

export interface CLIErrorMessageConfig {
title: string;
bodyLines?: string[];
Expand Down Expand Up @@ -228,15 +231,54 @@ class CLIOutput {

logCommand(message: string, taskStatus?: TaskStatus) {
this.addNewline();
const commandOutput =
chalk.dim('> ') + this.formatCommand(this.normalizeMessage(message));
const commandOutputWithStatus = this.addTaskStatus(
taskStatus,
commandOutput
this.writeToStdOut(this.getCommandWithStatus(message, taskStatus));
this.addNewline();
this.addNewline();
}

logCommandOutput(message: string, taskStatus: TaskStatus, output: string) {
let commandOutputWithStatus = this.getCommandWithStatus(
message,
taskStatus
);
if (process.env.GITHUB_ACTIONS) {
const icon = this.getStatusIcon(taskStatus);
commandOutputWithStatus = `${GH_GROUP_PREFIX}${icon} ${commandOutputWithStatus}`;
}

this.addNewline();
this.writeToStdOut(commandOutputWithStatus);
this.addNewline();
this.addNewline();
this.writeToStdOut(output);

if (process.env.GITHUB_ACTIONS) {
this.writeToStdOut(GH_GROUP_SUFFIX);
}
}

private getCommandWithStatus(
message: string,
taskStatus: TaskStatus
): string {
const commandOutput =
chalk.dim('> ') + this.formatCommand(this.normalizeMessage(message));
return this.addTaskStatus(taskStatus, commandOutput);
}

private getStatusIcon(taskStatus: TaskStatus) {
switch (taskStatus) {
case 'success':
return '✔️';
case 'failure':
return '❌';
case 'skipped':
case 'local-cache-kept-existing':
return '⏩';
case 'local-cache':
case 'remote-cache':
return '🔁';
}
}

private normalizeMessage(message: string) {
Expand Down

0 comments on commit 7739ce0

Please sign in to comment.