Skip to content

Commit e6ac40d

Browse files
committed
refactor: add colored formatting
1 parent 052e18c commit e6ac40d

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

packages/nx-plugin/src/executors/cli/executor.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { executeProcess } from '../../internal/execute-process.js';
33
import {
44
createCliCommandObject,
55
createCliCommandString,
6+
formatCommandLog,
7+
objectToCliArgs,
68
} from '../internal/cli.js';
79
import { normalizeContext } from '../internal/context.js';
810
import type { AutorunCommandExecutorOptions } from './schema.js';
@@ -19,10 +21,11 @@ export default async function runAutorunExecutor(
1921
context: ExecutorContext,
2022
): Promise<ExecutorOutput> {
2123
const normalizedContext = normalizeContext(context);
22-
const { env, bin, ...mergedOptions } = mergeExecutorOptions(
23-
context.target?.options,
24-
terminalAndExecutorOptions,
25-
);
24+
const {
25+
env,
26+
bin = '@code-pushup/cli',
27+
...mergedOptions
28+
} = mergeExecutorOptions(context.target?.options, terminalAndExecutorOptions);
2629
const cliArgumentObject = parseAutorunExecutorOptions(
2730
mergedOptions,
2831
normalizedContext,
@@ -33,12 +36,20 @@ export default async function runAutorunExecutor(
3336
bin,
3437
args: cliArgumentObject,
3538
});
39+
const coloredCommandString = formatCommandLog(
40+
'npx',
41+
[
42+
bin,
43+
...objectToCliArgs({ _: command ? [command] : [], ...cliArgumentObject }),
44+
],
45+
env,
46+
);
3647
if (verbose) {
3748
logger.info(`Run CLI executor ${command ?? ''}`);
38-
logger.info(`Command: ${commandString}`);
49+
logger.info(`Command: ${coloredCommandString}`);
3950
}
4051
if (dryRun) {
41-
logger.warn(`DryRun execution of: ${commandString}`);
52+
logger.warn(`DryRun execution of: ${coloredCommandString}`);
4253
} else {
4354
try {
4455
await executeProcess({

packages/nx-plugin/src/executors/internal/cli.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { logger } from '@nx/devkit';
2+
import ansis from 'ansis';
23
import type { ProcessConfig } from '../../internal/execute-process.js';
34

45
export function createCliCommandString(options?: {
@@ -12,6 +13,26 @@ export function createCliCommandString(options?: {
1213
)}`;
1314
}
1415

16+
export function formatCommandLog(
17+
command: string,
18+
args: string[] = [],
19+
env?: Record<string, string>,
20+
): string {
21+
const logElements: string[] = [];
22+
if (env) {
23+
const envVars = Object.entries(env).map(
24+
([key, value]) =>
25+
`${ansis.green(key)}="${ansis.blueBright(value.replaceAll('"', ''))}"`,
26+
);
27+
logElements.push(...envVars);
28+
}
29+
logElements.push(ansis.cyan(command));
30+
if (args.length > 0) {
31+
logElements.push(ansis.white(args.join(' ')));
32+
}
33+
return logElements.join(' ');
34+
}
35+
1536
export function createCliCommandObject(options?: {
1637
args?: Record<string, unknown>;
1738
command?: string;

0 commit comments

Comments
 (0)