Skip to content

Commit 52266a7

Browse files
committed
feat(cli): include logo and version in initial log
1 parent eb23ed4 commit 52266a7

File tree

15 files changed

+82
-34
lines changed

15 files changed

+82
-34
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ansis from 'ansis';
21
import type { ArgumentsCamelCase, CommandModule } from 'yargs';
32
import {
43
type CollectOptions,
@@ -7,9 +6,9 @@ import {
76
upload,
87
} from '@code-pushup/core';
98
import { logger } from '@code-pushup/utils';
10-
import { CLI_NAME } from '../constants.js';
119
import {
1210
collectSuccessfulLog,
11+
printCliCommand,
1312
renderConfigureCategoriesHint,
1413
renderIntegratePortalHint,
1514
uploadSuccessfulLog,
@@ -23,8 +22,8 @@ export function yargsAutorunCommandObject() {
2322
command,
2423
describe: 'Shortcut for running collect followed by upload',
2524
handler: async <T>(args: ArgumentsCamelCase<T>) => {
26-
logger.info(ansis.bold(CLI_NAME));
27-
logger.debug(`Running ${ansis.bold(command)} command`);
25+
printCliCommand(command);
26+
2827
const options = args as unknown as AutorunOptions;
2928

3029
// we need to ensure `json` is part of the formats as we want to upload

packages/cli/src/lib/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { commands } from './commands.js';
2-
import { CLI_NAME, CLI_SCRIPT_NAME } from './constants.js';
2+
import { CLI_DISPLAY_NAME, CLI_SCRIPT_NAME } from './constants.js';
33
import { middlewares } from './middlewares.js';
44
import { groups, options } from './options.js';
55
import { yargsCli } from './yargs-cli.js';
66

77
export const cli = (args: string[]) =>
88
yargsCli(args, {
9-
usageMessage: CLI_NAME,
9+
usageMessage: CLI_DISPLAY_NAME,
1010
scriptName: CLI_SCRIPT_NAME,
1111
options,
1212
groups,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
formatAsciiSticker,
1010
logger,
1111
} from '@code-pushup/utils';
12-
import { CLI_NAME } from '../constants.js';
1312
import {
1413
collectSuccessfulLog,
14+
printCliCommand,
1515
renderConfigureCategoriesHint,
1616
} from '../implementation/logging.js';
1717

@@ -21,9 +21,9 @@ export function yargsCollectCommandObject(): CommandModule {
2121
command,
2222
describe: 'Run Plugins and collect results',
2323
handler: async <T>(args: ArgumentsCamelCase<T>) => {
24+
printCliCommand(command);
25+
2426
const options = args as unknown as CollectAndPersistReportsOptions;
25-
logger.info(ansis.bold(CLI_NAME));
26-
logger.debug(`Running ${ansis.bold(command)} command`);
2727

2828
await collectAndPersistReports(options);
2929
collectSuccessfulLog();

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type { CommandModule } from 'yargs';
33
import { type CompareOptions, compareReportFiles } from '@code-pushup/core';
44
import type { PersistConfig, UploadConfig } from '@code-pushup/models';
55
import { logger } from '@code-pushup/utils';
6-
import { CLI_NAME } from '../constants.js';
76
import { yargsCompareOptionsDefinition } from '../implementation/compare.options.js';
7+
import { printCliCommand } from '../implementation/logging.js';
88

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

1918
const options = args as CompareOptions & {
2019
persist: Required<PersistConfig>;

packages/cli/src/lib/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export const CLI_NAME = 'Code PushUp CLI';
1+
export const CLI_DISPLAY_NAME = 'Code PushUp CLI';
22
export const CLI_SCRIPT_NAME = 'code-pushup';

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ansis from 'ansis';
21
import type { CommandModule } from 'yargs';
32
import { type HistoryOptions, history } from '@code-pushup/core';
43
import {
@@ -9,16 +8,15 @@ import {
98
logger,
109
safeCheckout,
1110
} from '@code-pushup/utils';
12-
import { CLI_NAME } from '../constants.js';
1311
import { yargsFilterOptionsDefinition } from '../implementation/filter.options.js';
12+
import { printCliCommand } from '../implementation/logging.js';
1413
import type { HistoryCliOptions } from './history.model.js';
1514
import { yargsHistoryOptionsDefinition } from './history.options.js';
1615
import { normalizeHashOptions } from './utils.js';
1716

1817
const command = 'history';
1918
async function handler(args: unknown) {
20-
logger.info(ansis.bold(CLI_NAME));
21-
logger.debug(`Running ${ansis.bold(command)} command`);
19+
printCliCommand(command);
2220

2321
const currentBranch = await getCurrentBranchOrTag();
2422
const { targetBranch: rawTargetBranch, ...opt } = args as HistoryCliOptions &
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { ArgumentsCamelCase } from 'yargs';
2+
import { logger } from '@code-pushup/utils';
3+
import { logIntroMiddleware } from './log-intro.middleware';
4+
5+
describe('logIntroMiddleware', () => {
6+
it('should print logo, name and version', () => {
7+
logIntroMiddleware({ $0: 'code-pushup', _: ['collect'] });
8+
expect(logger.info).toHaveBeenCalledWith(
9+
expect.stringMatching(/<> Code PushUp CLI v\d+\.\d+\.\d+/),
10+
);
11+
});
12+
13+
it('should not change arguments', () => {
14+
const args: ArgumentsCamelCase = { $0: 'code-pushup', _: ['collect'] };
15+
expect(logIntroMiddleware(args)).toBe(args);
16+
});
17+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import ansis from 'ansis';
2+
import type { ArgumentsCamelCase } from 'yargs';
3+
import { CODE_PUSHUP_UNICODE_LOGO, logger } from '@code-pushup/utils';
4+
import { CLI_DISPLAY_NAME } from '../constants.js';
5+
import { getVersion } from './version.js';
6+
7+
export function logIntroMiddleware(
8+
args: ArgumentsCamelCase,
9+
): ArgumentsCamelCase {
10+
// prevent interference with JSON output
11+
if (args._.includes('print-config') && !('output' in args)) {
12+
return args;
13+
}
14+
logger.info(
15+
ansis.bold.blue(
16+
`${CODE_PUSHUP_UNICODE_LOGO} ${CLI_DISPLAY_NAME} v${getVersion()}`,
17+
),
18+
);
19+
return args;
20+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import {
55
logger,
66
} from '@code-pushup/utils';
77

8+
export function printCliCommand(command: string): void {
9+
logger.debug(`Running ${ansis.bold(command)} command\n`);
10+
}
11+
812
export function renderConfigureCategoriesHint(): void {
913
logger.debug(
1014
`💡 Configure categories to see the scores in an overview table. See: ${formatAsciiLink(
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createRequire } from 'node:module';
2+
3+
export function getVersion(): string {
4+
const packageJson = createRequire(import.meta.url)(
5+
'../../../package.json',
6+
) as typeof import('../../../package.json');
7+
return packageJson.version;
8+
}

0 commit comments

Comments
 (0)