Skip to content

Commit 36e563a

Browse files
committed
refactor(ci): break up large executeCliCommand function, add jsdocs
1 parent 5da7421 commit 36e563a

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

packages/ci/src/lib/cli/exec.ts

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,25 @@ import {
88
} from '@code-pushup/utils';
99
import type { CommandContext } from './context.js';
1010

11+
/**
12+
* Executes Code PushUp CLI command and logs output in a way that's more readable in CI.
13+
* @param args Arguments for Code PushUp CLI
14+
* @param context Command context
15+
* @param options Optional information on whether all persist formats are set (if known)
16+
*/
1117
export async function executeCliCommand(
1218
args: string[],
1319
context: CommandContext,
1420
options?: { hasFormats: boolean },
1521
): Promise<void> {
16-
// eslint-disable-next-line functional/no-let
17-
let output = '';
18-
19-
const logRaw = (message: string) => {
20-
if (!context.silent) {
21-
if (!output) {
22-
logger.newline();
23-
}
24-
logger.info(message, { noIndent: true, noLineBreak: true });
25-
}
26-
output += message;
27-
};
28-
29-
const logEnd = () => {
30-
if (!context.silent && output) {
31-
logger.newline();
32-
}
33-
};
22+
const { logOutputChunk, logOutputEnd, logSilencedOutput } =
23+
createLogCallbacks(context);
3424

3525
const observer: ProcessObserver = {
36-
onStdout: logRaw,
37-
onStderr: logRaw,
38-
onComplete: logEnd,
39-
onError: logEnd,
26+
onStdout: logOutputChunk,
27+
onStderr: logOutputChunk,
28+
onComplete: logOutputEnd,
29+
onError: logOutputEnd,
4030
};
4131

4232
const config: ProcessConfig = {
@@ -54,13 +44,7 @@ export async function executeCliCommand(
5444
await executeProcess(config);
5545
} catch (error) {
5646
// ensure output of failed process is always logged for debugging
57-
if (context.silent) {
58-
logger.newline();
59-
logger.info(output, { noIndent: true });
60-
if (!output.endsWith('\n')) {
61-
logger.newline();
62-
}
63-
}
47+
logSilencedOutput();
6448
throw error;
6549
}
6650
});
@@ -69,6 +53,39 @@ export async function executeCliCommand(
6953
}
7054
}
7155

56+
function createLogCallbacks(context: Pick<CommandContext, 'silent'>) {
57+
// eslint-disable-next-line functional/no-let
58+
let output = '';
59+
60+
const logOutputChunk = (message: string) => {
61+
if (!context.silent) {
62+
if (!output) {
63+
logger.newline();
64+
}
65+
logger.info(message, { noIndent: true, noLineBreak: true });
66+
}
67+
output += message;
68+
};
69+
70+
const logOutputEnd = () => {
71+
if (!context.silent && output) {
72+
logger.newline();
73+
}
74+
};
75+
76+
const logSilencedOutput = () => {
77+
if (context.silent) {
78+
logger.newline();
79+
logger.info(output, { noIndent: true });
80+
if (!output.endsWith('\n')) {
81+
logger.newline();
82+
}
83+
}
84+
};
85+
86+
return { logOutputChunk, logOutputEnd, logSilencedOutput };
87+
}
88+
7289
function combineArgs(
7390
args: string[],
7491
context: CommandContext,

0 commit comments

Comments
 (0)