Skip to content

Commit 2eb0cc8

Browse files
committed
feat(plugin-coverage): log initializer and nx helper steps
1 parent a3fc6d0 commit 2eb0cc8

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const COVERAGE_PLUGIN_SLUG = 'coverage';
2+
export const COVERAGE_PLUGIN_TITLE = 'Code coverage';

packages/plugin-coverage/src/lib/coverage-plugin.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
type PluginConfig,
66
validate,
77
} from '@code-pushup/models';
8-
import { capitalize } from '@code-pushup/utils';
8+
import { capitalize, logger, pluralizeToken } from '@code-pushup/utils';
99
import {
1010
type CoveragePluginConfig,
1111
type CoverageType,
1212
coveragePluginConfigSchema,
1313
} from './config.js';
14+
import { COVERAGE_PLUGIN_SLUG, COVERAGE_PLUGIN_TITLE } from './constants.js';
15+
import { formatMetaLog } from './format.js';
1416
import { createRunnerFunction } from './runner/runner.js';
1517
import { coverageDescription, coverageTypeWeightMapper } from './utils.js';
1618

@@ -58,15 +60,21 @@ export async function coveragePlugin(
5860
})),
5961
};
6062

63+
logger.info(
64+
formatMetaLog(
65+
`Created ${pluralizeToken('audit', audits.length)} (${coverageConfig.coverageTypes.join('/')} coverage) and 1 group`,
66+
),
67+
);
68+
6169
const packageJson = createRequire(import.meta.url)(
6270
'../../package.json',
6371
) as typeof import('../../package.json');
6472

6573
const scoreTargets = coverageConfig.scoreTargets;
6674

6775
return {
68-
slug: 'coverage',
69-
title: 'Code coverage',
76+
slug: COVERAGE_PLUGIN_SLUG,
77+
title: COVERAGE_PLUGIN_TITLE,
7078
icon: 'folder-coverage-open',
7179
description: 'Official Code PushUp code coverage plugin.',
7280
docsUrl: 'https://www.npmjs.com/package/@code-pushup/coverage-plugin/',
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { pluginMetaLogFormatter } from '@code-pushup/utils';
2+
import { COVERAGE_PLUGIN_TITLE } from './constants.js';
3+
4+
export const formatMetaLog = pluginMetaLogFormatter(COVERAGE_PLUGIN_TITLE);

packages/plugin-coverage/src/lib/nx/coverage-paths.ts

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,15 @@ import type {
77
import type { JestExecutorOptions } from '@nx/jest/src/executors/jest/schema';
88
import type { VitestExecutorOptions } from '@nx/vite/executors';
99
import path from 'node:path';
10-
import { importModule, logger, stringifyError } from '@code-pushup/utils';
10+
import {
11+
importModule,
12+
logger,
13+
pluralize,
14+
pluralizeToken,
15+
stringifyError,
16+
} from '@code-pushup/utils';
1117
import type { CoverageResult } from '../config.js';
18+
import { formatMetaLog } from '../format.js';
1219

1320
/**
1421
* Resolves the cached project graph for the current Nx workspace.
@@ -21,9 +28,8 @@ async function resolveCachedProjectGraph() {
2128
try {
2229
return readCachedProjectGraph();
2330
} catch (error) {
24-
logger.info(
25-
`Could not read cached project graph, falling back to async creation.
26-
${stringifyError(error)}`,
31+
logger.warn(
32+
`Could not read cached project graph, falling back to async creation - ${stringifyError(error)}`,
2733
);
2834
return await createProjectGraphAsync({ exitOnError: false });
2935
}
@@ -36,29 +42,52 @@ async function resolveCachedProjectGraph() {
3642
export async function getNxCoveragePaths(
3743
targets: string[] = ['test'],
3844
): Promise<CoverageResult[]> {
39-
logger.debug('💡 Gathering coverage from the following nx projects:');
40-
4145
const { nodes } = await resolveCachedProjectGraph();
4246

43-
const coverageResults = await Promise.all(
44-
targets.map(async target => {
45-
const relevantNodes = Object.values(nodes).filter(graph =>
46-
hasNxTarget(graph, target),
47-
);
48-
49-
return await Promise.all(
50-
relevantNodes.map<Promise<CoverageResult>>(async ({ name, data }) => {
51-
const coveragePaths = await getCoveragePathsForTarget(data, target);
52-
logger.debug(`- ${name}: ${target}`);
53-
return coveragePaths;
54-
}),
55-
);
56-
}),
47+
const coverageResultsPerTarget = Object.fromEntries(
48+
await Promise.all(
49+
targets.map(async (target): Promise<[string, CoverageResult[]]> => {
50+
const relevantNodes = Object.values(nodes).filter(graph =>
51+
hasNxTarget(graph, target),
52+
);
53+
54+
return [
55+
target,
56+
await Promise.all(
57+
relevantNodes.map(({ data }) =>
58+
getCoveragePathsForTarget(data, target),
59+
),
60+
),
61+
];
62+
}),
63+
),
5764
);
5865

59-
logger.debug('');
66+
const coverageResults = Object.values(coverageResultsPerTarget).flat();
67+
68+
logger.info(
69+
formatMetaLog(
70+
`Inferred ${pluralizeToken('coverage report', coverageResults.length)} from Nx projects with ${pluralize('target', targets.length)} ${
71+
targets.length === 1
72+
? targets[0]
73+
: Object.entries(coverageResultsPerTarget)
74+
.map(([target, results]) => `${target} (${results.length})`)
75+
.join(' and ')
76+
}`,
77+
),
78+
);
79+
logger.debug(
80+
formatMetaLog(
81+
coverageResults
82+
.map(
83+
result =>
84+
`• ${typeof result === 'string' ? result : result.resultsPath}`,
85+
)
86+
.join('\n'),
87+
),
88+
);
6089

61-
return coverageResults.flat();
90+
return coverageResults;
6291
}
6392

6493
function hasNxTarget(

0 commit comments

Comments
 (0)