Skip to content

Commit da99447

Browse files
committed
refactor(utils): make execute process log compact
1 parent c677fa2 commit da99447

File tree

3 files changed

+95
-8
lines changed

3 files changed

+95
-8
lines changed

packages/utils/src/lib/execute-process.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { gray } from 'ansis';
21
import {
32
type ChildProcess,
43
type ChildProcessByStdio,
@@ -7,7 +6,8 @@ import {
76
spawn,
87
} from 'node:child_process';
98
import type { Readable, Writable } from 'node:stream';
10-
import { ui } from './logging.js';
9+
import { formatCommandLog } from './format-command-log.js';
10+
import { isVerbose, ui } from './logging.js';
1111
import { calcDuration } from './reports/utils.js';
1212

1313
/**
@@ -150,12 +150,11 @@ export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
150150
const date = new Date().toISOString();
151151
const start = performance.now();
152152

153-
const logCommand = [command, ...(args || [])].join(' ');
154-
ui().logger.log(
155-
gray(
156-
`Executing command:\n${logCommand}\nIn working directory:\n${cfg.cwd ?? process.cwd()}`,
157-
),
158-
);
153+
if (isVerbose()) {
154+
ui().logger.log(
155+
formatCommandLog(command, args, `${cfg.cwd ?? process.cwd()}`),
156+
);
157+
}
159158

160159
return new Promise((resolve, reject) => {
161160
// shell:true tells Windows to use shell command for spawning a child process
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import path from 'node:path';
2+
import { describe, expect, it } from 'vitest';
3+
import { removeColorCodes } from '@code-pushup/test-utils';
4+
import { formatCommandLog } from './format-command-log.js';
5+
6+
describe('formatCommandLog', () => {
7+
it('should format simple command', () => {
8+
const result = removeColorCodes(
9+
formatCommandLog('npx', ['command', '--verbose']),
10+
);
11+
12+
expect(result).toBe('$ npx command --verbose');
13+
});
14+
15+
it('should format simple command with explicit process.cwd()', () => {
16+
const result = removeColorCodes(
17+
formatCommandLog('npx', ['command', '--verbose'], process.cwd()),
18+
);
19+
20+
expect(result).toBe('$ npx command --verbose');
21+
});
22+
23+
it('should format simple command with relative cwd', () => {
24+
const result = removeColorCodes(
25+
formatCommandLog('npx', ['command', '--verbose'], './wololo'),
26+
);
27+
28+
expect(result).toBe(`wololo $ npx command --verbose`);
29+
});
30+
31+
it('should format simple command with absolute non-current path converted to relative', () => {
32+
const result = removeColorCodes(
33+
formatCommandLog(
34+
'npx',
35+
['command', '--verbose'],
36+
path.join(process.cwd(), 'tmp'),
37+
),
38+
);
39+
expect(result).toBe('tmp $ npx command --verbose');
40+
});
41+
42+
it('should format simple command with relative cwd in parent folder', () => {
43+
const result = removeColorCodes(
44+
formatCommandLog('npx', ['command', '--verbose'], '..'),
45+
);
46+
47+
expect(result).toBe(`.. $ npx command --verbose`);
48+
});
49+
50+
it('should format simple command using relative path to parent directory', () => {
51+
const result = removeColorCodes(
52+
formatCommandLog(
53+
'npx',
54+
['command', '--verbose'],
55+
path.dirname(process.cwd()),
56+
),
57+
);
58+
59+
expect(result).toBe('.. $ npx command --verbose');
60+
});
61+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import ansis from 'ansis';
2+
import path from 'node:path';
3+
4+
/**
5+
* Formats a command string with optional cwd prefix and ANSI colors.
6+
*
7+
* @param {string} command - The command to execute.
8+
* @param {string[]} args - Array of command arguments.
9+
* @param {string} [cwd] - Optional current working directory for the command.
10+
* @returns {string} - ANSI-colored formatted command string.
11+
*/
12+
export function formatCommandLog(
13+
command: string,
14+
args: string[] = [],
15+
cwd: string = process.cwd(),
16+
): string {
17+
const relativeDir = path.relative(process.cwd(), cwd);
18+
19+
return [
20+
...(relativeDir && relativeDir !== '.'
21+
? [ansis.italic(ansis.gray(relativeDir))]
22+
: []),
23+
ansis.yellow('$'),
24+
ansis.gray(command),
25+
ansis.gray(args.map(arg => arg).join(' ')),
26+
].join(' ');
27+
}

0 commit comments

Comments
 (0)