Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/cli/src/lib/autorun/autorun-command.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { bold, gray } from 'ansis';
import ansis from 'ansis';
import type { ArgumentsCamelCase, CommandModule } from 'yargs';
import {
type CollectOptions,
type UploadOptions,
collectAndPersistReports,
upload,
} from '@code-pushup/core';
import { ui } from '@code-pushup/utils';
import { logger } from '@code-pushup/utils';
import { CLI_NAME } from '../constants.js';
import {
collectSuccessfulLog,
Expand All @@ -23,8 +23,8 @@ export function yargsAutorunCommandObject() {
command,
describe: 'Shortcut for running collect followed by upload',
handler: async <T>(args: ArgumentsCamelCase<T>) => {
ui().logger.log(bold(CLI_NAME));
ui().logger.info(gray(`Run ${command}...`));
logger.info(ansis.bold(CLI_NAME));
logger.debug(`Running ${ansis.bold(command)} command`);
const options = args as unknown as AutorunOptions;

// we need to ensure `json` is part of the formats as we want to upload
Expand All @@ -51,7 +51,7 @@ export function yargsAutorunCommandObject() {
uploadSuccessfulLog(report.url);
}
} else {
ui().logger.warning('Upload skipped because configuration is not set.');
logger.warn('Upload skipped because configuration is not set.');
renderIntegratePortalHint();
}
},
Expand Down
15 changes: 8 additions & 7 deletions packages/cli/src/lib/collect/collect-command.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bold, gray } from 'ansis';
import ansis from 'ansis';
import type { ArgumentsCamelCase, CommandModule } from 'yargs';
import {
type CollectAndPersistReportsOptions,
collectAndPersistReports,
} from '@code-pushup/core';
import { link, ui } from '@code-pushup/utils';
import { link, logger, ui } from '@code-pushup/utils';
import { CLI_NAME } from '../constants.js';
import {
collectSuccessfulLog,
Expand All @@ -18,8 +18,8 @@ export function yargsCollectCommandObject(): CommandModule {
describe: 'Run Plugins and collect results',
handler: async <T>(args: ArgumentsCamelCase<T>) => {
const options = args as unknown as CollectAndPersistReportsOptions;
ui().logger.log(bold(CLI_NAME));
ui().logger.info(gray(`Run ${command}...`));
logger.info(ansis.bold(CLI_NAME));
logger.debug(`Running ${ansis.bold(command)} command`);

await collectAndPersistReports(options);
collectSuccessfulLog();
Expand All @@ -40,12 +40,13 @@ export function yargsCollectCommandObject(): CommandModule {
}

export function renderUploadAutorunHint(): void {
// TODO: replace @poppinss/cliui
ui()
.sticker()
.add(bold.gray('💡 Visualize your reports'))
.add(ansis.bold.gray('💡 Visualize your reports'))
.add('')
.add(
`${gray('❯')} npx code-pushup upload - ${gray(
`${ansis.gray('❯')} npx code-pushup upload - ${ansis.gray(
'Run upload to upload the created report to the server',
)}`,
)
Expand All @@ -55,7 +56,7 @@ export function renderUploadAutorunHint(): void {
)}`,
)
.add(
`${gray('❯')} npx code-pushup autorun - ${gray('Run collect & upload')}`,
`${ansis.gray('❯')} npx code-pushup autorun - ${ansis.gray('Run collect & upload')}`,
)
.add(
` ${link(
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/lib/compare/compare-command.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { bold, gray } from 'ansis';
import ansis from 'ansis';
import type { CommandModule } from 'yargs';
import { type CompareOptions, compareReportFiles } from '@code-pushup/core';
import type { PersistConfig, UploadConfig } from '@code-pushup/models';
import { ui } from '@code-pushup/utils';
import { logger } from '@code-pushup/utils';
import { CLI_NAME } from '../constants.js';
import { yargsCompareOptionsDefinition } from '../implementation/compare.options.js';

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

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

ui().logger.info(
logger.info(
`Reports diff written to ${outputPaths
.map(path => bold(path))
.map(path => ansis.bold(path))
.join(' and ')}`,
);
},
Expand Down
11 changes: 5 additions & 6 deletions packages/cli/src/lib/compare/compare-command.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { bold } from 'ansis';
import ansis from 'ansis';
import { compareReportFiles } from '@code-pushup/core';
import {
DEFAULT_PERSIST_FILENAME,
DEFAULT_PERSIST_FORMAT,
DEFAULT_PERSIST_OUTPUT_DIR,
} from '@code-pushup/models';
import { ui } from '@code-pushup/utils';
import { logger } from '@code-pushup/utils';
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants.js';
import { yargsCli } from '../yargs-cli.js';
import { yargsCompareCommandObject } from './compare-command.js';
Expand Down Expand Up @@ -78,11 +78,10 @@ describe('compare-command', () => {
commands: [yargsCompareCommandObject()],
}).parseAsync();

expect(ui()).toHaveLogged(
'info',
`Reports diff written to ${bold(
expect(logger.info).toHaveBeenCalledWith(
`Reports diff written to ${ansis.bold(
'.code-pushup/report-diff.json',
)} and ${bold('.code-pushup/report-diff.md')}`,
)} and ${ansis.bold('.code-pushup/report-diff.md')}`,
);
});
});
10 changes: 5 additions & 5 deletions packages/cli/src/lib/history/history-command.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { bold, gray } from 'ansis';
import ansis from 'ansis';
import type { CommandModule } from 'yargs';
import { type HistoryOptions, history } from '@code-pushup/core';
import {
type LogResult,
getCurrentBranchOrTag,
getHashes,
getSemverTags,
logger,
safeCheckout,
ui,
} from '@code-pushup/utils';
import { CLI_NAME } from '../constants.js';
import { yargsFilterOptionsDefinition } from '../implementation/filter.options.js';
Expand All @@ -17,8 +17,8 @@ import { normalizeHashOptions } from './utils.js';

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

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

ui().logger.log(`Reports: ${reports.length}`);
logger.info(`Reports: ${reports.length}`);
} finally {
// go back to initial branch
await safeCheckout(currentBranch);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CategoryConfig, PluginConfig } from '@code-pushup/models';
import { ui } from '@code-pushup/utils';
import { logger } from '@code-pushup/utils';
import {
filterMiddleware,
filterSkippedCategories,
Expand Down Expand Up @@ -315,14 +315,12 @@ describe('filterMiddleware', () => {
] as CategoryConfig[],
});

expect(ui()).toHaveNthLogged(
expect(logger.info).toHaveBeenNthCalledWith(
1,
'info',
'The --skipPlugins argument removed the following categories: c1, c2.',
);
expect(ui()).toHaveNthLogged(
expect(logger.info).toHaveBeenNthCalledWith(
2,
'info',
'The --onlyPlugins argument removed the following categories: c1, c2.',
);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/lib/implementation/global.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import yargs from 'yargs';
import { toArray, ui } from '@code-pushup/utils';
import { logger, stringifyError, toArray } from '@code-pushup/utils';
import { OptionValidationError } from './validate-filter-options.utils.js';

export function filterKebabCaseKeys<T extends Record<string, unknown>>(
Expand Down Expand Up @@ -36,11 +36,11 @@ export function logErrorBeforeThrow<T extends (...args: any[]) => any>(
return await fn(...args);
} catch (error) {
if (error instanceof OptionValidationError) {
ui().logger.error(error.message);
logger.error(error.message);
await new Promise(resolve => process.stdout.write('', resolve));
yargs().exit(1, error);
} else {
console.error(error);
logger.error(stringifyError(error));
await new Promise(resolve => process.stdout.write('', resolve));
throw error;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/implementation/global.utils.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest';
import { ui } from '@code-pushup/utils';
import { logger } from '@code-pushup/utils';
import { filterKebabCaseKeys, logErrorBeforeThrow } from './global.utils.js';
import { OptionValidationError } from './validate-filter-options.utils.js';

Expand Down Expand Up @@ -64,7 +64,7 @@ describe('logErrorBeforeThrow', () => {
} catch {
/* suppress */
}
expect(ui()).toHaveLogged('error', 'Option validation failed');
expect(logger.error).toHaveBeenCalledWith('Option validation failed');
});

it('should rethrow errors other than OptionValidationError', async () => {
Expand Down
30 changes: 15 additions & 15 deletions packages/cli/src/lib/implementation/logging.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { bold, gray } from 'ansis';
import { link, ui } from '@code-pushup/utils';
import ansis from 'ansis';
import { link, logger, ui } from '@code-pushup/utils';

export function renderConfigureCategoriesHint(): void {
ui().logger.info(
gray(
`💡 Configure categories to see the scores in an overview table. See: ${link(
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md',
)}`,
),
logger.debug(
`💡 Configure categories to see the scores in an overview table. See: ${link(
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md',
)}`,
{ force: true },
);
}
export function uploadSuccessfulLog(url: string): void {
ui().logger.success('Upload successful!');
ui().logger.success(link(url));
logger.info(ansis.green('Upload successful!'));
logger.info(link(url));
}

export function collectSuccessfulLog(): void {
ui().logger.success('Collecting report successful!');
logger.info(ansis.green('Collecting report successful!'));
}

export function renderIntegratePortalHint(): void {
// TODO: replace @poppinss/cliui
ui()
.sticker()
.add(bold.gray('💡 Integrate the portal'))
.add(ansis.bold.gray('💡 Integrate the portal'))
.add('')
.add(
`${gray('❯')} Upload a report to the server - ${gray(
`${ansis.gray('❯')} Upload a report to the server - ${ansis.gray(
'npx code-pushup upload',
)}`,
)
Expand All @@ -35,12 +35,12 @@ export function renderIntegratePortalHint(): void {
)}`,
)
.add(
`${gray('❯')} ${gray('Portal Integration')} - ${link(
`${ansis.gray('❯')} ${ansis.gray('Portal Integration')} - ${link(
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#portal-integration',
)}`,
)
.add(
`${gray('❯')} ${gray('Upload Command')} - ${link(
`${ansis.gray('❯')} ${ansis.gray('Upload Command')} - ${link(
'https://github.com/code-pushup/cli/blob/main/packages/cli/README.md#portal-integration',
)}`,
)
Expand Down

This file was deleted.

33 changes: 16 additions & 17 deletions packages/cli/src/lib/implementation/set-verbose.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import type { GlobalOptions } from '@code-pushup/core';
import type { CoreConfig } from '@code-pushup/models';
import { coerceBooleanValue } from '@code-pushup/utils';
import { coerceBooleanValue, logger } from '@code-pushup/utils';
import type { FilterOptions } from './filter.model.js';
import type { GeneralCliOptions } from './global.model';

/**
*
* | CP_VERBOSE value | CLI `--verbose` flag | Effect |
* |------------------|-----------------------------|------------|
* | true | Not provided | enabled |
* | false | Not provided | disabled |
* | Not provided | Not provided | disabled |
* | Not provided | Explicitly set (true) | enabled |
* | true | Explicitly set (true) | enabled |
* | false | Explicitly set (true) | enabled |
* | true | Explicitly negated (false) | disabled |
* | false | Explicitly negated (false) | disabled |
* | CP_VERBOSE value | CLI `--verbose` flag | Effect |
* |------------------|----------------------|--------|
* | true | - | true |
* | false | - | false |
* | - | - | false |
* | - | true | true |
* | - | false | false |
* | true | true | true |
* | false | true | true |
* | true | false | false |
* | false | false | false |
*
* @param originalProcessArgs
*/
export function setVerboseMiddleware<
T extends GeneralCliOptions & CoreConfig & FilterOptions & GlobalOptions,
>(originalProcessArgs: T): T {
const envVerbose = coerceBooleanValue(process.env['CP_VERBOSE']);
const cliVerbose = coerceBooleanValue(originalProcessArgs.verbose);
const verboseEffect = cliVerbose ?? envVerbose ?? false;

// eslint-disable-next-line functional/immutable-data
process.env['CP_VERBOSE'] = `${verboseEffect}`;
if (cliVerbose != null) {
logger.setVerbose(cliVerbose);
}

return {
...originalProcessArgs,
verbose: verboseEffect,
verbose: logger.isVerbose(),
};
}
Loading