Skip to content

Commit

Permalink
feat: make portal-client dependency optional
Browse files Browse the repository at this point in the history
feat: make portal-client dependency optional

feat: make portal-client dependency optional

feat: make portal-client dependency optional
  • Loading branch information
hanna-skryl committed Oct 11, 2024
1 parent f423c6c commit bc325eb
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 18 deletions.
25 changes: 25 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ _If you're looking for programmatic usage, then refer to the underlying [@code-p
```

</details>
<details>
<summary>Installing without optional dependencies</summary>

To avoid installing optional dependencies like `@code-pushup/portal-client`, use the commands below. Please note that omitting this dependency will limit the upload functionality.

**npm**

```sh
npm install @code-pushup/cli --omit=dev --omit=optional
```

**pnpm**

`pnpm` requires reinstalling all packages to skip optional dependencies. The `--no-optional` flag applies globally, omitting all optional dependencies across the workspace. For more granular control, you can customize dependency behavior using a [`.pnpmfile.cjs`](https://pnpm.io/pnpmfile) configuration file.

```sh
pnpm add --save-dev @code-pushup/cli
rm -rf pnpm-lock.yaml node_modules
pnpm install --no-optional
```

**yarn**

`yarn` does not support omitting optional dependencies.
</details>

2. Create a `code-pushup.config.ts` configuration file (`.js` or `.mjs` extensions are also supported).

Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/lib/autorun/autorun-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ export function yargsAutorunCommandObject() {
}

if (options.upload) {
const { url } = await upload(options);
uploadSuccessfulLog(url);
const report = await upload(options);
if (report?.url) {
uploadSuccessfulLog(report.url);
}
} else {
ui().logger.warning('Upload skipped because configuration is not set.');
renderIntegratePortalHint();
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/lib/upload/upload-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export function yargsUploadCommandObject() {
renderIntegratePortalHint();
throw new Error('Upload configuration not set');
}
const { url } = await upload(options);
uploadSuccessfulLog(url);
const report = await upload(options);
if (report?.url) {
uploadSuccessfulLog(report.url);
}
},
} satisfies CommandModule;
}
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"dependencies": {
"@code-pushup/models": "0.51.0",
"@code-pushup/utils": "0.51.0",
"@code-pushup/portal-client": "^0.9.0",
"ansis": "^3.3.0"
},
"optionalDependencies": {
"@code-pushup/portal-client": "^0.9.0"
}
}
10 changes: 6 additions & 4 deletions packages/core/src/lib/compare.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import {
PortalOperationError,
getPortalComparisonLink,
} from '@code-pushup/portal-client';
import {
type Format,
type PersistConfig,
Expand All @@ -28,6 +24,7 @@ import {
compareCategories,
compareGroups,
} from './implementation/compare-scorables';
import { loadPortalClient } from './load-portal-client';

export async function compareReportFiles(
inputPaths: Diff<string>,
Expand Down Expand Up @@ -119,6 +116,11 @@ async function fetchPortalComparisonLink(
commits: NonNullable<ReportsDiff['commits']>,
): Promise<string | undefined> {
const { server, apiKey, organization, project } = uploadConfig;
const portalClient = await loadPortalClient();
if (!portalClient) {

Check failure on line 120 in packages/core/src/lib/compare.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
return;
}

Check warning on line 122 in packages/core/src/lib/compare.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 121-122 are not covered in any test case.
const { PortalOperationError, getPortalComparisonLink } = portalClient;
try {
return await getPortalComparisonLink({
server,
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/lib/load-portal-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ui } from '@code-pushup/utils';

Check failure on line 1 in packages/core/src/lib/load-portal-client.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.

export async function loadPortalClient(): Promise<
typeof import('@code-pushup/portal-client') | null
> {
try {
return await import('@code-pushup/portal-client');
} catch {

Check failure on line 8 in packages/core/src/lib/load-portal-client.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
ui().logger.error(
'Optional dependency @code-pushup/portal-client is not available. Make sure it is installed to enable upload functionality.',
);
return null;
}

Check warning on line 13 in packages/core/src/lib/load-portal-client.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 9-13 are not covered in any test case.
}
18 changes: 9 additions & 9 deletions packages/core/src/lib/upload.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
type SaveReportMutationVariables,
uploadToPortal,
} from '@code-pushup/portal-client';
import type { SaveReportMutationVariables } from '@code-pushup/portal-client';
import type { PersistConfig, Report, UploadConfig } from '@code-pushup/models';
import { loadReport } from '@code-pushup/utils';
import { reportToGQL } from './implementation/report-to-gql';
import { loadPortalClient } from './load-portal-client';
import type { GlobalOptions } from './types';

export type UploadOptions = { upload?: UploadConfig } & {
Expand All @@ -16,13 +14,15 @@ export type UploadOptions = { upload?: UploadConfig } & {
* @param options
* @param uploadFn
*/
export async function upload(
options: UploadOptions,
uploadFn: typeof uploadToPortal = uploadToPortal,
) {
export async function upload(options: UploadOptions) {
if (options.upload == null) {
throw new Error('Upload configuration is not set.');
}
const portalClient = await loadPortalClient();
if (!portalClient) {

Check failure on line 22 in packages/core/src/lib/upload.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
return;
}

Check warning on line 24 in packages/core/src/lib/upload.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 23-24 are not covered in any test case.
const { uploadToPortal } = portalClient;
const { apiKey, server, organization, project, timeout } = options.upload;
const report: Report = await loadReport({
...options.persist,
Expand All @@ -39,5 +39,5 @@ export async function upload(
...reportToGQL(report),
};

return uploadFn({ apiKey, server, data, timeout });
return uploadToPortal({ apiKey, server, data, timeout });
}

0 comments on commit bc325eb

Please sign in to comment.