@@ -8,15 +8,56 @@ import {
88} from '@code-pushup/utils' ;
99import 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+ */
1117export async function executeCliCommand (
1218 args : string [ ] ,
1319 context : CommandContext ,
1420 options ?: { hasFormats : boolean } ,
1521) : Promise < void > {
22+ const { logOutputChunk, logOutputEnd, logSilencedOutput } =
23+ createLogCallbacks ( context ) ;
24+
25+ const observer : ProcessObserver = {
26+ onStdout : logOutputChunk ,
27+ onStderr : logOutputChunk ,
28+ onComplete : logOutputEnd ,
29+ onError : logOutputEnd ,
30+ } ;
31+
32+ const config : ProcessConfig = {
33+ command : context . bin ,
34+ args : combineArgs ( args , context , options ) ,
35+ cwd : context . directory ,
36+ observer,
37+ silent : true ,
38+ } ;
39+ const bin = serializeCommandWithArgs ( config ) ;
40+
41+ try {
42+ await logger . command ( bin , async ( ) => {
43+ try {
44+ await executeProcess ( config ) ;
45+ } catch ( error ) {
46+ // ensure output of failed process is always logged for debugging
47+ logSilencedOutput ( ) ;
48+ throw error ;
49+ }
50+ } ) ;
51+ } finally {
52+ logger . newline ( ) ;
53+ }
54+ }
55+
56+ function createLogCallbacks ( context : Pick < CommandContext , 'silent' > ) {
1657 // eslint-disable-next-line functional/no-let
1758 let output = '' ;
1859
19- const logRaw = ( message : string ) => {
60+ const logOutputChunk = ( message : string ) => {
2061 if ( ! context . silent ) {
2162 if ( ! output ) {
2263 logger . newline ( ) ;
@@ -26,43 +67,23 @@ export async function executeCliCommand(
2667 output += message ;
2768 } ;
2869
29- const logEnd = ( ) => {
70+ const logOutputEnd = ( ) => {
3071 if ( ! context . silent && output ) {
3172 logger . newline ( ) ;
3273 }
3374 } ;
3475
35- const observer : ProcessObserver = {
36- onStdout : logRaw ,
37- onStderr : logRaw ,
38- onComplete : logEnd ,
39- onError : logEnd ,
40- } ;
41-
42- const config : ProcessConfig = {
43- command : context . bin ,
44- args : combineArgs ( args , context , options ) ,
45- cwd : context . directory ,
46- observer,
47- silent : true ,
48- } ;
49- const bin = serializeCommandWithArgs ( config ) ;
50-
51- await logger . command ( bin , async ( ) => {
52- try {
53- await executeProcess ( config ) ;
54- } catch ( error ) {
55- // ensure output of failed process is always logged for debugging
56- if ( context . silent ) {
76+ const logSilencedOutput = ( ) => {
77+ if ( context . silent ) {
78+ logger . newline ( ) ;
79+ logger . info ( output , { noIndent : true } ) ;
80+ if ( ! output . endsWith ( '\n' ) ) {
5781 logger . newline ( ) ;
58- logger . info ( output , { noIndent : true } ) ;
59- if ( ! output . endsWith ( '\n' ) ) {
60- logger . newline ( ) ;
61- }
6282 }
63- throw error ;
6483 }
65- } ) ;
84+ } ;
85+
86+ return { logOutputChunk, logOutputEnd, logSilencedOutput } ;
6687}
6788
6889function combineArgs (
0 commit comments