Skip to content

Commit 6f134e6

Browse files
committed
feat(ci): use new shared logger, remove option
1 parent 9721413 commit 6f134e6

20 files changed

+53
-133
lines changed

packages/ci/README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ Optionally, you can override default options for further customization:
106106
| `silent` | `boolean` | `false` | Hides logs from CLI commands (errors will be printed) |
107107
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
108108
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
109-
| `logger` | `Logger` | `console` | Logger for reporting progress and encountered problems |
110109
| `skipComment` | `boolean` | `false` | Toggles if comparison comment is posted to PR |
111110
| `configPatterns` | `ConfigPatterns \| null` | `null` | Additional configuration which enables [faster CI runs](#faster-ci-runs-with-configpatterns) |
112111
| `searchCommits` | `boolean \| number` | `false` | If base branch has no cached report in portal, [extends search up to 100 recent commits](#search-latest-commits-for-previous-report) |
@@ -115,15 +114,6 @@ Optionally, you can override default options for further customization:
115114

116115
[^2]: The `{task}` pattern is replaced with the `task` value, so the default behaviour is to list projects using `npx nx show projects --with-target=code-pushup --json`. The `nxProjectsFilter` options gives Nx users the flexibility to filter projects in alternative ways supported by the Nx CLI (e.g. `--affected`, `--projects`, `--exclude`, `--type`) - refer to [options in Nx docs](https://nx.dev/nx-api/nx/documents/show#options) for details.
117116

118-
The `Logger` object has the following required properties:
119-
120-
| Property | Type | Description |
121-
| :------- | :-------------------------- | :----------------- |
122-
| `error` | `(message: string) => void` | Prints error log |
123-
| `warn` | `(message: string) => void` | Prints warning log |
124-
| `info` | `(message: string) => void` | Prints info log |
125-
| `debug` | `(message: string) => void` | Prints debug log |
126-
127117
## Standalone mode
128118

129119
By default, it is assumed that Code PushUp is set up to run on the whole repo with one command (_standalone mode_).

packages/ci/src/lib/cli/commands/collect.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
2-
import { executeProcess, isVerbose } from '@code-pushup/utils';
2+
import { executeProcess } from '@code-pushup/utils';
33
import type { CommandContext } from '../context.js';
44

55
export async function runCollect(
@@ -9,7 +9,6 @@ export async function runCollect(
99
await executeProcess({
1010
command: bin,
1111
args: [
12-
...(isVerbose() ? ['--verbose'] : []),
1312
...(config ? [`--config=${config}`] : []),
1413
...(hasFormats
1514
? []

packages/ci/src/lib/cli/commands/compare.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
2-
import { executeProcess, isVerbose } from '@code-pushup/utils';
2+
import { executeProcess } from '@code-pushup/utils';
33
import type { CommandContext } from '../context.js';
44

55
export async function runCompare(
@@ -10,7 +10,6 @@ export async function runCompare(
1010
command: bin,
1111
args: [
1212
'compare',
13-
...(isVerbose() ? ['--verbose'] : []),
1413
...(config ? [`--config=${config}`] : []),
1514
...(hasFormats
1615
? []

packages/ci/src/lib/cli/commands/merge-diffs.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
DEFAULT_PERSIST_FILENAME,
44
DEFAULT_PERSIST_OUTPUT_DIR,
55
} from '@code-pushup/models';
6-
import { executeProcess, isVerbose } from '@code-pushup/utils';
6+
import { executeProcess } from '@code-pushup/utils';
77
import type { CommandContext } from '../context.js';
88

99
export async function runMergeDiffs(
@@ -17,7 +17,6 @@ export async function runMergeDiffs(
1717
command: bin,
1818
args: [
1919
'merge-diffs',
20-
...(isVerbose() ? ['--verbose'] : []),
2120
...files.map(file => `--files=${file}`),
2221
...(config ? [`--config=${config}`] : []),
2322
`--persist.outputDir=${outputDir}`,

packages/ci/src/lib/cli/commands/print-config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { rm } from 'node:fs/promises';
22
import path from 'node:path';
33
import {
44
executeProcess,
5-
isVerbose,
65
readJsonFile,
76
stringifyError,
87
} from '@code-pushup/utils';
@@ -31,7 +30,6 @@ export async function runPrintConfig({
3130
args: [
3231
...(config ? [`--config=${config}`] : []),
3332
'print-config',
34-
...(isVerbose() ? ['--verbose'] : []),
3533
`--output=${outputPath}`,
3634
],
3735
cwd: directory,

packages/ci/src/lib/comment.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { readFile } from 'node:fs/promises';
2-
import type { Logger, ProviderAPIClient } from './models.js';
2+
import { logger } from '@code-pushup/utils';
3+
import type { ProviderAPIClient } from './models.js';
34

45
export async function commentOnPR(
56
mdPath: string,
67
api: ProviderAPIClient,
7-
logger: Logger,
88
): Promise<number> {
99
const markdown = await readFile(mdPath, 'utf8');
1010
const identifier = `<!-- generated by @code-pushup/ci -->`;
1111
const body = truncateBody(
1212
`${markdown}\n\n${identifier}\n`,
1313
api.maxCommentChars,
14-
logger,
1514
);
1615

1716
const comments = await api.listComments();
@@ -37,7 +36,7 @@ export async function commentOnPR(
3736
return createdComment.id;
3837
}
3938

40-
function truncateBody(body: string, max: number, logger: Logger): string {
39+
function truncateBody(body: string, max: number): string {
4140
const truncateWarning = '...*[Comment body truncated]*';
4241
if (body.length > max) {
4342
logger.warn(`Comment body is too long. Truncating to ${max} characters.`);

packages/ci/src/lib/comment.unit.test.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { vol } from 'memfs';
22
import { writeFile } from 'node:fs/promises';
33
import path from 'node:path';
44
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
5+
import { logger } from '@code-pushup/utils';
56
import { commentOnPR } from './comment.js';
6-
import type { Comment, Logger, ProviderAPIClient } from './models.js';
7+
import type { Comment, ProviderAPIClient } from './models.js';
78

89
describe('commentOnPR', () => {
910
const diffText = '# Code PushUp\n\nNo changes to report.\n';
@@ -28,13 +29,6 @@ describe('commentOnPR', () => {
2829
listComments: vi.fn(),
2930
} satisfies ProviderAPIClient;
3031

31-
const logger: Logger = {
32-
error: vi.fn(),
33-
warn: vi.fn(),
34-
info: vi.fn(),
35-
debug: vi.fn(),
36-
};
37-
3832
beforeEach(() => {
3933
vol.fromJSON({ [diffFile]: diffText }, MEMFS_VOLUME);
4034
api.listComments.mockResolvedValue([]);
@@ -43,7 +37,7 @@ describe('commentOnPR', () => {
4337
it('should create new comment if none existing', async () => {
4438
api.listComments.mockResolvedValue([]);
4539

46-
await expect(commentOnPR(diffPath, api, logger)).resolves.toBe(comment.id);
40+
await expect(commentOnPR(diffPath, api)).resolves.toBe(comment.id);
4741

4842
expect(api.listComments).toHaveBeenCalled();
4943
expect(api.createComment).toHaveBeenCalledWith(comment.body);
@@ -53,7 +47,7 @@ describe('commentOnPR', () => {
5347
it("should create new comment if existing comments don't match", async () => {
5448
api.listComments.mockResolvedValue([otherComment]);
5549

56-
await expect(commentOnPR(diffPath, api, logger)).resolves.toBe(comment.id);
50+
await expect(commentOnPR(diffPath, api)).resolves.toBe(comment.id);
5751

5852
expect(api.listComments).toHaveBeenCalled();
5953
expect(api.createComment).toHaveBeenCalledWith(comment.body);
@@ -63,7 +57,7 @@ describe('commentOnPR', () => {
6357
it('should update previous comment if it matches', async () => {
6458
api.listComments.mockResolvedValue([comment]);
6559

66-
await expect(commentOnPR(diffPath, api, logger)).resolves.toBe(comment.id);
60+
await expect(commentOnPR(diffPath, api)).resolves.toBe(comment.id);
6761

6862
expect(api.listComments).toHaveBeenCalled();
6963
expect(api.createComment).not.toHaveBeenCalled();
@@ -73,7 +67,7 @@ describe('commentOnPR', () => {
7367
it('should update previous comment which matches and ignore other comments', async () => {
7468
api.listComments.mockResolvedValue([otherComment, comment]);
7569

76-
await expect(commentOnPR(diffPath, api, logger)).resolves.toBe(comment.id);
70+
await expect(commentOnPR(diffPath, api)).resolves.toBe(comment.id);
7771

7872
expect(api.listComments).toHaveBeenCalled();
7973
expect(api.createComment).not.toHaveBeenCalled();
@@ -86,7 +80,7 @@ describe('commentOnPR', () => {
8680
.join('\n');
8781
await writeFile(diffPath, longDiffText);
8882

89-
await expect(commentOnPR(diffPath, api, logger)).resolves.toBe(comment.id);
83+
await expect(commentOnPR(diffPath, api)).resolves.toBe(comment.id);
9084

9185
expect(api.createComment).toHaveBeenCalledWith(
9286
expect.stringContaining('...*[Comment body truncated]*'),

packages/ci/src/lib/create-execution-observer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ProcessObserver, isVerbose } from '@code-pushup/utils';
1+
import { type ProcessObserver, logger } from '@code-pushup/utils';
22

33
export function createExecutionObserver(
44
{
@@ -9,11 +9,11 @@ export function createExecutionObserver(
99
): ProcessObserver {
1010
return {
1111
onStderr: stderr => {
12-
console.warn(stderr);
12+
logger.warn(stderr);
1313
},
14-
...((!silent || isVerbose()) && {
14+
...((!silent || logger.isVerbose()) && {
1515
onStdout: stdout => {
16-
console.info(stdout);
16+
logger.info(stdout);
1717
},
1818
}),
1919
};

packages/ci/src/lib/models.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export type Options = {
1818
silent?: boolean;
1919
debug?: boolean;
2020
detectNewIssues?: boolean;
21-
logger?: Logger;
2221
skipComment?: boolean;
2322
configPatterns?: ConfigPatterns | null;
2423
searchCommits?: boolean | number;
@@ -66,16 +65,6 @@ export type GitBranch = {
6665
sha: string;
6766
};
6867

69-
/**
70-
* Logger instance (e.g. `console`) for reporting progress and problems
71-
*/
72-
export type Logger = {
73-
error: (message: string) => void;
74-
warn: (message: string) => void;
75-
info: (message: string) => void;
76-
debug: (message: string) => void;
77-
};
78-
7968
/**
8069
* Code PushUp config patterns which hold for every project in monorepo.
8170
* Providing this information upfront makes CI runs faster (skips print-config).

packages/ci/src/lib/monorepo/list-projects.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { glob } from 'glob';
22
import path from 'node:path';
3+
import { logger } from '@code-pushup/utils';
34
import { createExecutionObserver } from '../create-execution-observer.js';
4-
import type { Logger, Settings } from '../models.js';
5+
import type { Settings } from '../models.js';
56
import { detectMonorepoTool } from './detect-tool.js';
67
import { getToolHandler } from './handlers/index.js';
78
import { listPackages } from './packages.js';
@@ -24,7 +25,6 @@ export type RunManyCommand = (
2425
export async function listMonorepoProjects(
2526
settings: Settings,
2627
): Promise<MonorepoProjects> {
27-
const logger = settings.logger;
2828
const options = createMonorepoHandlerOptions(settings);
2929

3030
const tool = await resolveMonorepoTool(settings, options);
@@ -50,15 +50,13 @@ export async function listMonorepoProjects(
5050
patterns: settings.projects,
5151
cwd: options.cwd,
5252
bin: settings.bin,
53-
logger,
5453
});
5554
return { tool, projects };
5655
}
5756

5857
const projects = await listProjectsByNpmPackages({
5958
cwd: options.cwd,
6059
bin: settings.bin,
61-
logger,
6260
});
6361
return { tool, projects };
6462
}
@@ -71,7 +69,6 @@ async function resolveMonorepoTool(
7169
// shouldn't happen, handled by caller
7270
throw new Error('Monorepo mode not enabled');
7371
}
74-
const logger = settings.logger;
7572

7673
if (typeof settings.monorepo === 'string') {
7774
logger.info(`Using monorepo tool "${settings.monorepo}" from inputs`);
@@ -108,9 +105,8 @@ async function listProjectsByGlobs(args: {
108105
patterns: string[];
109106
cwd: string;
110107
bin: string;
111-
logger: Logger;
112108
}): Promise<ProjectConfig[]> {
113-
const { patterns, cwd, bin, logger } = args;
109+
const { patterns, cwd, bin } = args;
114110

115111
const directories = await glob(
116112
patterns.map(pattern => pattern.replace(/\/$/, '/')),
@@ -134,9 +130,8 @@ async function listProjectsByGlobs(args: {
134130
async function listProjectsByNpmPackages(args: {
135131
cwd: string;
136132
bin: string;
137-
logger: Logger;
138133
}): Promise<ProjectConfig[]> {
139-
const { cwd, bin, logger } = args;
134+
const { cwd, bin } = args;
140135

141136
const packages = await listPackages(cwd);
142137

0 commit comments

Comments
 (0)