Skip to content

Commit 472efcd

Browse files
committed
feat(cli,core,utils): replace basic logs with new logger
1 parent 9876d94 commit 472efcd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+313
-424
lines changed

packages/cli/src/lib/autorun/autorun-command.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { bold, gray } from 'ansis';
1+
import ansis from 'ansis';
22
import type { ArgumentsCamelCase, CommandModule } from 'yargs';
33
import {
44
type CollectOptions,
55
type UploadOptions,
66
collectAndPersistReports,
77
upload,
88
} from '@code-pushup/core';
9-
import { ui } from '@code-pushup/utils';
9+
import { logger } from '@code-pushup/utils';
1010
import { CLI_NAME } from '../constants.js';
1111
import {
1212
collectSuccessfulLog,
@@ -23,8 +23,8 @@ export function yargsAutorunCommandObject() {
2323
command,
2424
describe: 'Shortcut for running collect followed by upload',
2525
handler: async <T>(args: ArgumentsCamelCase<T>) => {
26-
ui().logger.log(bold(CLI_NAME));
27-
ui().logger.info(gray(`Run ${command}...`));
26+
logger.info(ansis.bold(CLI_NAME));
27+
logger.debug(`Running ${ansis.bold(command)} command`);
2828
const options = args as unknown as AutorunOptions;
2929

3030
// we need to ensure `json` is part of the formats as we want to upload
@@ -51,7 +51,7 @@ export function yargsAutorunCommandObject() {
5151
uploadSuccessfulLog(report.url);
5252
}
5353
} else {
54-
ui().logger.warning('Upload skipped because configuration is not set.');
54+
logger.warn('Upload skipped because configuration is not set.');
5555
renderIntegratePortalHint();
5656
}
5757
},

packages/cli/src/lib/collect/collect-command.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { bold, gray } from 'ansis';
1+
import ansis from 'ansis';
22
import type { ArgumentsCamelCase, CommandModule } from 'yargs';
33
import {
44
type CollectAndPersistReportsOptions,
55
collectAndPersistReports,
66
} from '@code-pushup/core';
7-
import { link, ui } from '@code-pushup/utils';
7+
import { link, logger, ui } from '@code-pushup/utils';
88
import { CLI_NAME } from '../constants.js';
99
import {
1010
collectSuccessfulLog,
@@ -18,8 +18,8 @@ export function yargsCollectCommandObject(): CommandModule {
1818
describe: 'Run Plugins and collect results',
1919
handler: async <T>(args: ArgumentsCamelCase<T>) => {
2020
const options = args as unknown as CollectAndPersistReportsOptions;
21-
ui().logger.log(bold(CLI_NAME));
22-
ui().logger.info(gray(`Run ${command}...`));
21+
logger.info(ansis.bold(CLI_NAME));
22+
logger.debug(`Running ${ansis.bold(command)} command`);
2323

2424
await collectAndPersistReports(options);
2525
collectSuccessfulLog();
@@ -40,12 +40,13 @@ export function yargsCollectCommandObject(): CommandModule {
4040
}
4141

4242
export function renderUploadAutorunHint(): void {
43+
// TODO: replace @poppinss/cliui
4344
ui()
4445
.sticker()
45-
.add(bold.gray('💡 Visualize your reports'))
46+
.add(ansis.bold.gray('💡 Visualize your reports'))
4647
.add('')
4748
.add(
48-
`${gray('❯')} npx code-pushup upload - ${gray(
49+
`${ansis.gray('❯')} npx code-pushup upload - ${ansis.gray(
4950
'Run upload to upload the created report to the server',
5051
)}`,
5152
)
@@ -55,7 +56,7 @@ export function renderUploadAutorunHint(): void {
5556
)}`,
5657
)
5758
.add(
58-
`${gray('❯')} npx code-pushup autorun - ${gray('Run collect & upload')}`,
59+
`${ansis.gray('❯')} npx code-pushup autorun - ${ansis.gray('Run collect & upload')}`,
5960
)
6061
.add(
6162
` ${link(

packages/cli/src/lib/compare/compare-command.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { bold, gray } from 'ansis';
1+
import ansis from 'ansis';
22
import type { CommandModule } from 'yargs';
33
import { type CompareOptions, compareReportFiles } from '@code-pushup/core';
44
import type { PersistConfig, UploadConfig } from '@code-pushup/models';
5-
import { ui } from '@code-pushup/utils';
5+
import { logger } from '@code-pushup/utils';
66
import { CLI_NAME } from '../constants.js';
77
import { yargsCompareOptionsDefinition } from '../implementation/compare.options.js';
88

@@ -13,8 +13,8 @@ export function yargsCompareCommandObject() {
1313
describe: 'Compare 2 report files and create a diff file',
1414
builder: yargsCompareOptionsDefinition(),
1515
handler: async (args: unknown) => {
16-
ui().logger.log(bold(CLI_NAME));
17-
ui().logger.info(gray(`Run ${command}...`));
16+
logger.info(ansis.bold(CLI_NAME));
17+
logger.debug(`Running ${ansis.bold(command)} command`);
1818

1919
const options = args as CompareOptions & {
2020
persist: Required<PersistConfig>;
@@ -28,9 +28,9 @@ export function yargsCompareCommandObject() {
2828
{ before, after, label },
2929
);
3030

31-
ui().logger.info(
31+
logger.info(
3232
`Reports diff written to ${outputPaths
33-
.map(path => bold(path))
33+
.map(path => ansis.bold(path))
3434
.join(' and ')}`,
3535
);
3636
},

packages/cli/src/lib/compare/compare-command.unit.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { bold } from 'ansis';
1+
import ansis from 'ansis';
22
import { compareReportFiles } from '@code-pushup/core';
33
import {
44
DEFAULT_PERSIST_FILENAME,
55
DEFAULT_PERSIST_FORMAT,
66
DEFAULT_PERSIST_OUTPUT_DIR,
77
} from '@code-pushup/models';
8-
import { ui } from '@code-pushup/utils';
8+
import { logger } from '@code-pushup/utils';
99
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants.js';
1010
import { yargsCli } from '../yargs-cli.js';
1111
import { yargsCompareCommandObject } from './compare-command.js';
@@ -78,11 +78,10 @@ describe('compare-command', () => {
7878
commands: [yargsCompareCommandObject()],
7979
}).parseAsync();
8080

81-
expect(ui()).toHaveLogged(
82-
'info',
83-
`Reports diff written to ${bold(
81+
expect(logger.info).toHaveBeenCalledWith(
82+
`Reports diff written to ${ansis.bold(
8483
'.code-pushup/report-diff.json',
85-
)} and ${bold('.code-pushup/report-diff.md')}`,
84+
)} and ${ansis.bold('.code-pushup/report-diff.md')}`,
8685
);
8786
});
8887
});

packages/cli/src/lib/history/history-command.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { bold, gray } from 'ansis';
1+
import ansis from 'ansis';
22
import type { CommandModule } from 'yargs';
33
import { type HistoryOptions, history } from '@code-pushup/core';
44
import {
55
type LogResult,
66
getCurrentBranchOrTag,
77
getHashes,
88
getSemverTags,
9+
logger,
910
safeCheckout,
10-
ui,
1111
} from '@code-pushup/utils';
1212
import { CLI_NAME } from '../constants.js';
1313
import { yargsFilterOptionsDefinition } from '../implementation/filter.options.js';
@@ -17,8 +17,8 @@ import { normalizeHashOptions } from './utils.js';
1717

1818
const command = 'history';
1919
async function handler(args: unknown) {
20-
ui().logger.info(bold(CLI_NAME));
21-
ui().logger.info(gray(`Run ${command}`));
20+
logger.info(ansis.bold(CLI_NAME));
21+
logger.debug(`Running ${ansis.bold(command)} command`);
2222

2323
const currentBranch = await getCurrentBranchOrTag();
2424
const { targetBranch: rawTargetBranch, ...opt } = args as HistoryCliOptions &
@@ -50,7 +50,7 @@ async function handler(args: unknown) {
5050
results.map(({ hash }) => hash),
5151
);
5252

53-
ui().logger.log(`Reports: ${reports.length}`);
53+
logger.info(`Reports: ${reports.length}`);
5454
} finally {
5555
// go back to initial branch
5656
await safeCheckout(currentBranch);

packages/cli/src/lib/implementation/filter.middleware.unit.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CategoryConfig, PluginConfig } from '@code-pushup/models';
2-
import { ui } from '@code-pushup/utils';
2+
import { logger } from '@code-pushup/utils';
33
import {
44
filterMiddleware,
55
filterSkippedCategories,
@@ -315,14 +315,12 @@ describe('filterMiddleware', () => {
315315
] as CategoryConfig[],
316316
});
317317

318-
expect(ui()).toHaveNthLogged(
318+
expect(logger.info).toHaveBeenNthCalledWith(
319319
1,
320-
'info',
321320
'The --skipPlugins argument removed the following categories: c1, c2.',
322321
);
323-
expect(ui()).toHaveNthLogged(
322+
expect(logger.info).toHaveBeenNthCalledWith(
324323
2,
325-
'info',
326324
'The --onlyPlugins argument removed the following categories: c1, c2.',
327325
);
328326
});

packages/cli/src/lib/implementation/global.utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import yargs from 'yargs';
2-
import { toArray, ui } from '@code-pushup/utils';
2+
import { logger, stringifyError, toArray } from '@code-pushup/utils';
33
import { OptionValidationError } from './validate-filter-options.utils.js';
44

55
export function filterKebabCaseKeys<T extends Record<string, unknown>>(
@@ -36,11 +36,11 @@ export function logErrorBeforeThrow<T extends (...args: any[]) => any>(
3636
return await fn(...args);
3737
} catch (error) {
3838
if (error instanceof OptionValidationError) {
39-
ui().logger.error(error.message);
39+
logger.error(error.message);
4040
await new Promise(resolve => process.stdout.write('', resolve));
4141
yargs().exit(1, error);
4242
} else {
43-
console.error(error);
43+
logger.error(stringifyError(error));
4444
await new Promise(resolve => process.stdout.write('', resolve));
4545
throw error;
4646
}

packages/cli/src/lib/implementation/global.utils.unit.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it, vi } from 'vitest';
2-
import { ui } from '@code-pushup/utils';
2+
import { logger } from '@code-pushup/utils';
33
import { filterKebabCaseKeys, logErrorBeforeThrow } from './global.utils.js';
44
import { OptionValidationError } from './validate-filter-options.utils.js';
55

@@ -64,7 +64,7 @@ describe('logErrorBeforeThrow', () => {
6464
} catch {
6565
/* suppress */
6666
}
67-
expect(ui()).toHaveLogged('error', 'Option validation failed');
67+
expect(logger.error).toHaveBeenCalledWith('Option validation failed');
6868
});
6969

7070
it('should rethrow errors other than OptionValidationError', async () => {

packages/cli/src/lib/implementation/logging.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import { bold, gray } from 'ansis';
2-
import { link, ui } from '@code-pushup/utils';
1+
import ansis from 'ansis';
2+
import { link, logger, ui } from '@code-pushup/utils';
33

44
export function renderConfigureCategoriesHint(): void {
5-
ui().logger.info(
6-
gray(
7-
`💡 Configure categories to see the scores in an overview table. See: ${link(
8-
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md',
9-
)}`,
10-
),
5+
logger.debug(
6+
`💡 Configure categories to see the scores in an overview table. See: ${link(
7+
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md',
8+
)}`,
9+
{ force: true },
1110
);
1211
}
1312
export function uploadSuccessfulLog(url: string): void {
14-
ui().logger.success('Upload successful!');
15-
ui().logger.success(link(url));
13+
logger.info(ansis.green('Upload successful!'));
14+
logger.info(link(url));
1615
}
1716

1817
export function collectSuccessfulLog(): void {
19-
ui().logger.success('Collecting report successful!');
18+
logger.info(ansis.green('Collecting report successful!'));
2019
}
2120

2221
export function renderIntegratePortalHint(): void {
22+
// TODO: replace @poppinss/cliui
2323
ui()
2424
.sticker()
25-
.add(bold.gray('💡 Integrate the portal'))
25+
.add(ansis.bold.gray('💡 Integrate the portal'))
2626
.add('')
2727
.add(
28-
`${gray('❯')} Upload a report to the server - ${gray(
28+
`${ansis.gray('❯')} Upload a report to the server - ${ansis.gray(
2929
'npx code-pushup upload',
3030
)}`,
3131
)
@@ -35,12 +35,12 @@ export function renderIntegratePortalHint(): void {
3535
)}`,
3636
)
3737
.add(
38-
`${gray('❯')} ${gray('Portal Integration')} - ${link(
38+
`${ansis.gray('❯')} ${ansis.gray('Portal Integration')} - ${link(
3939
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#portal-integration',
4040
)}`,
4141
)
4242
.add(
43-
`${gray('❯')} ${gray('Upload Command')} - ${link(
43+
`${ansis.gray('❯')} ${ansis.gray('Upload Command')} - ${link(
4444
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#portal-integration',
4545
)}`,
4646
)

packages/cli/src/lib/implementation/set-verbose.middleware.int.test.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)