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
2 changes: 1 addition & 1 deletion e2e/cli-e2e/tests/__snapshots__/help.e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Commands:
[default]
code-pushup autorun Shortcut for running collect followed by upload

code-pushup collect Run Plugins and collect results
code-pushup collect Run plugins and collect results
code-pushup upload Upload report results to the portal
code-pushup history Collect reports for commit history
code-pushup compare Compare 2 report files and create a diff file
Expand Down
4 changes: 2 additions & 2 deletions e2e/cli-e2e/tests/collect.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('CLI collect', () => {

const md = await readTextFile(path.join(dummyOutputDir, 'report.md'));

expect(md).toContain('# Code PushUp Report');
expect(md).toContain('# Code PushUp report');
expect(md).toContain(dummyPluginTitle);
expect(md).toContain(dummyAuditTitle);
});
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('CLI collect', () => {

expect(code).toBe(0);

expect(stdout).toContain('Code PushUp Report');
expect(stdout).toContain('Code PushUp report');
expect(stdout).not.toContain('Generated reports');
expect(stdout).toContain(dummyPluginTitle);
expect(stdout).toContain(dummyAuditTitle);
Expand Down
35 changes: 7 additions & 28 deletions e2e/cli-e2e/tests/print-config.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ describe('CLI print-config', () => {
it.each(extensions)(
'should load .%s config file with correct arguments',
async ext => {
const { code, stdout } = await executeProcess({
const { code } = await executeProcess({
command: 'npx',
args: [
'@code-pushup/cli',
'print-config',
'--output=config.json',
`--config=${configFilePath(ext)}`,
'--tsconfig=tsconfig.base.json',
'--persist.outputDir=output-dir',
Expand All @@ -56,7 +57,11 @@ describe('CLI print-config', () => {

expect(code).toBe(0);

expect(JSON.parse(stdout)).toEqual(
const output = await readFile(
path.join(testFileDummySetup, 'config.json'),
'utf8',
);
expect(JSON.parse(output)).toEqual(
expect.objectContaining({
config: expect.stringContaining(`code-pushup.config.${ext}`),
tsconfig: 'tsconfig.base.json',
Expand All @@ -71,30 +76,4 @@ describe('CLI print-config', () => {
);
},
);

it('should print config to output file', async () => {
const { code, stdout } = await executeProcess({
command: 'npx',
args: ['@code-pushup/cli', 'print-config', '--output=config.json'],
cwd: testFileDummySetup,
});

expect(code).toBe(0);

const output = await readFile(
path.join(testFileDummySetup, 'config.json'),
'utf8',
);
expect(JSON.parse(output)).toEqual(
expect.objectContaining({
plugins: [
expect.objectContaining({
slug: 'dummy-plugin',
title: 'Dummy Plugin',
}),
],
}),
);
expect(stdout).not.toContain('dummy-plugin');
});
});
2 changes: 1 addition & 1 deletion packages/ci/mocks/fixtures/outputs/report-after.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Code PushUp Report
# Code PushUp report

## 🛡️ Audits

Expand Down
2 changes: 1 addition & 1 deletion packages/ci/mocks/fixtures/outputs/report-before.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Code PushUp Report
# Code PushUp report

## 🛡️ Audits

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,9 @@ Print the resolved configuration.

In addition to the [Common Command Options](#common-command-options), the following options are recognized by the `print-config` command:

| Option | Required | Type | Description |
| -------------- | :------: | -------- | -------------------------------------------------------- |
| **`--output`** | no | `string` | Path to output file to print config (default is stdout). |
| Option | Required | Type | Description |
| -------------- | :------: | -------- | ------------------------------------ |
| **`--output`** | yes | `string` | Path to output file to print config. |

#### `merge-diffs` command

Expand Down
8 changes: 4 additions & 4 deletions packages/cli/docs/custom-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Execute the CLI with `npx code-pushup collect` and you should the following outp
<summary> <b>stdout of CLI for the above code</b> (collapsed for brevity) </summary>

```sh
Code PushUp Report - @code-pushup/core@x.y.z
Code PushUp report

My plugin audits
● My audit 0
Expand Down Expand Up @@ -240,7 +240,7 @@ Now we can execute the CLI with `npx code-pushup collect` and see a similar outp
<summary> <b>stdout of CLI for the above code</b> (collapsed for brevity) </summary>

```sh
Code PushUp Report - @code-pushup/core@x.y.z
Code PushUp report

File size plugin audits
● File size audit 2 files
Expand Down Expand Up @@ -371,7 +371,7 @@ Now we can execute the CLI with `npx code-pushup collect` and see a similar outp
<summary> <b>stdout of CLI for the above code</b> (collapsed for brevity) </summary>

```sh
Code PushUp Report - @code-pushup/core@x.y.z
Code PushUp report

Chrome Lighthosue audits
● Largest Contentful Paint 0
Expand Down Expand Up @@ -656,7 +656,7 @@ Test the output by running `npx code-pushup collect`.
<summary> <b>stdout of basic lighthouse plugin</b> (collapsed for brevity) </summary>

```sh
Code PushUp Report - @code-pushup/core@x.y.z
Code PushUp report

Chrome Lighthouse audits
● Largest Contentful Paint 1,3 s
Expand Down
29 changes: 12 additions & 17 deletions packages/cli/src/lib/autorun/autorun-command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ansis from 'ansis';
import type { ArgumentsCamelCase, CommandModule } from 'yargs';
import {
type CollectOptions,
Expand All @@ -7,12 +6,10 @@ import {
upload,
} from '@code-pushup/core';
import { logger } from '@code-pushup/utils';
import { CLI_NAME } from '../constants.js';
import {
collectSuccessfulLog,
renderConfigureCategoriesHint,
renderIntegratePortalHint,
uploadSuccessfulLog,
printCliCommand,
renderCategoriesHint,
renderPortalHint,
} from '../implementation/logging.js';

type AutorunOptions = CollectOptions & UploadOptions;
Expand All @@ -23,8 +20,8 @@ export function yargsAutorunCommandObject() {
command,
describe: 'Shortcut for running collect followed by upload',
handler: async <T>(args: ArgumentsCamelCase<T>) => {
logger.info(ansis.bold(CLI_NAME));
logger.debug(`Running ${ansis.bold(command)} command`);
printCliCommand(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 @@ -39,20 +36,18 @@ export function yargsAutorunCommandObject() {
};

await collectAndPersistReports(optionsWithFormat);
collectSuccessfulLog();

if (!options.categories || options.categories.length === 0) {
renderConfigureCategoriesHint();
if (!options.categories?.length) {
renderCategoriesHint();
logger.newline();
}

if (options.upload) {
const report = await upload(options);
if (report?.url) {
uploadSuccessfulLog(report.url);
}
await upload(options);
} else {
logger.warn('Upload skipped because configuration is not set.');
renderIntegratePortalHint();
logger.warn('Upload skipped because Portal is not configured.');
logger.newline();
renderPortalHint();
}
},
} satisfies CommandModule;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/cli.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { commands } from './commands.js';
import { CLI_NAME, CLI_SCRIPT_NAME } from './constants.js';
import { CLI_DISPLAY_NAME, CLI_SCRIPT_NAME } from './constants.js';
import { middlewares } from './middlewares.js';
import { groups, options } from './options.js';
import { yargsCli } from './yargs-cli.js';

export const cli = (args: string[]) =>
yargsCli(args, {
usageMessage: CLI_NAME,
usageMessage: CLI_DISPLAY_NAME,
scriptName: CLI_SCRIPT_NAME,
options,
groups,
Expand Down
51 changes: 14 additions & 37 deletions packages/cli/src/lib/collect/collect-command.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,40 @@
import ansis from 'ansis';
import type { ArgumentsCamelCase, CommandModule } from 'yargs';
import {
type CollectAndPersistReportsOptions,
collectAndPersistReports,
} from '@code-pushup/core';
import {
formatAsciiLink,
formatAsciiSticker,
logger,
} from '@code-pushup/utils';
import { CLI_NAME } from '../constants.js';
import {
collectSuccessfulLog,
renderConfigureCategoriesHint,
printCliCommand,
renderCategoriesHint,
renderPortalHint,
renderUploadHint,
} from '../implementation/logging.js';

export function yargsCollectCommandObject(): CommandModule {
const command = 'collect';
return {
command,
describe: 'Run Plugins and collect results',
describe: 'Run plugins and collect results',
handler: async <T>(args: ArgumentsCamelCase<T>) => {
printCliCommand(command);

const options = args as unknown as CollectAndPersistReportsOptions;
logger.info(ansis.bold(CLI_NAME));
logger.debug(`Running ${ansis.bold(command)} command`);

await collectAndPersistReports(options);
collectSuccessfulLog();

if (!options.categories || options.categories.length === 0) {
renderConfigureCategoriesHint();
if (!options.categories?.length) {
renderCategoriesHint();
}

const { upload = {} } = args as unknown as Record<
const { upload } = args as unknown as Record<
'upload',
object | undefined
>;
if (Object.keys(upload).length === 0) {
renderUploadAutorunHint();
if (upload) {
renderUploadHint();
} else {
renderPortalHint();
}
},
} satisfies CommandModule;
}

export function renderUploadAutorunHint(): void {
logger.info(
formatAsciiSticker([
ansis.bold.gray('💡 Visualize your reports'),
'',
`${ansis.gray('❯')} npx code-pushup upload - ${ansis.gray(
'Run upload to upload the created report to the server',
)}`,
` ${formatAsciiLink(
'https://github.com/code-pushup/cli/tree/main/packages/cli#upload-command',
)}`,
`${ansis.gray('❯')} npx code-pushup autorun - ${ansis.gray('Run collect & upload')}`,
` ${formatAsciiLink(
'https://github.com/code-pushup/cli/tree/main/packages/cli#autorun-command',
)}`,
]),
);
}
5 changes: 2 additions & 3 deletions packages/cli/src/lib/compare/compare-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { CommandModule } from 'yargs';
import { type CompareOptions, compareReportFiles } from '@code-pushup/core';
import type { PersistConfig, UploadConfig } from '@code-pushup/models';
import { logger } from '@code-pushup/utils';
import { CLI_NAME } from '../constants.js';
import { yargsCompareOptionsDefinition } from '../implementation/compare.options.js';
import { printCliCommand } from '../implementation/logging.js';

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

const options = args as CompareOptions & {
persist: Required<PersistConfig>;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const CLI_NAME = 'Code PushUp CLI';
export const CLI_DISPLAY_NAME = 'Code PushUp CLI';
export const CLI_SCRIPT_NAME = 'code-pushup';
6 changes: 2 additions & 4 deletions packages/cli/src/lib/history/history-command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ansis from 'ansis';
import type { CommandModule } from 'yargs';
import { type HistoryOptions, history } from '@code-pushup/core';
import {
Expand All @@ -9,16 +8,15 @@ import {
logger,
safeCheckout,
} from '@code-pushup/utils';
import { CLI_NAME } from '../constants.js';
import { yargsFilterOptionsDefinition } from '../implementation/filter.options.js';
import { printCliCommand } from '../implementation/logging.js';
import type { HistoryCliOptions } from './history.model.js';
import { yargsHistoryOptionsDefinition } from './history.options.js';
import { normalizeHashOptions } from './utils.js';

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

const currentBranch = await getCurrentBranchOrTag();
const { targetBranch: rawTargetBranch, ...opt } = args as HistoryCliOptions &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('coreConfigMiddleware', () => {
it('should throw with invalid config path', async () => {
await expect(
coreConfigMiddleware({ config: 'wrong/path/to/config', ...CLI_DEFAULTS }),
).rejects.toThrow(/Provided path .* is not valid./);
).rejects.toThrow(/File '.*' does not exist/);
});

it('should load config which relies on provided --tsconfig', async () => {
Expand Down
Loading