Skip to content

Commit 09dbe30

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

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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(
29+
`${path.join(process.cwd(), 'wololo')} $ npx command --verbose`,
30+
);
31+
});
32+
33+
it('should format simple command with absolute cwd', () => {
34+
const result = removeColorCodes(
35+
formatCommandLog(
36+
'npx',
37+
['command', '--verbose'],
38+
'/tmp/path/to/somewhere',
39+
),
40+
);
41+
42+
expect(result).toBe(
43+
`${path.resolve('/tmp/path/to/somewhere')} $ npx command --verbose`,
44+
);
45+
});
46+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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,
16+
): string {
17+
const currentDir = process.cwd();
18+
const execDir = cwd ? path.resolve(cwd) : currentDir;
19+
20+
return [
21+
...(path.relative(currentDir, execDir)
22+
? [ansis.italic(ansis.gray(execDir))]
23+
: []),
24+
ansis.yellow('$'),
25+
ansis.cyan(command),
26+
ansis.dim(args.map(arg => arg).join(' ')),
27+
].join(' ');
28+
}

0 commit comments

Comments
 (0)