Skip to content

Commit

Permalink
refactor(utils): add package.json metadata to report in cli, not utils
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Sep 25, 2023
1 parent bba4d0a commit 7d3de10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
9 changes: 7 additions & 2 deletions packages/cli/src/lib/collect/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pluginOutputSchema } from '@quality-metrics/models';
import { pluginOutputSchema, Report } from '@quality-metrics/models';
import {
collect,
CollectOptions,
Expand All @@ -12,7 +12,12 @@ export function yargsCollectCommandObject() {
const handler = async (
config: CollectOptions & { format: string },
): Promise<void> => {
const report = await collect({ ...config, packageJson });
const collectReport = await collect(config);
const report: Report = {
...collectReport,
packageName: packageJson.name,
version: packageJson.version,
};

await persistReport(report, config);

Expand Down
13 changes: 4 additions & 9 deletions packages/utils/src/lib/collect/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { reportSchema } from '@quality-metrics/models';
import { mockCoreConfig } from '@quality-metrics/models/testing';
import type { PackageJson } from 'type-fest';
import { describe, expect, it } from 'vitest';
import { CollectOptions, collect } from '../collect/';

Expand All @@ -13,13 +12,9 @@ const baseOptions: CollectOptions = {

describe('collect', () => {
it('should execute with valid options`', async () => {
const packageJson: PackageJson = {
name: '@code-pushup/cli',
version: '0.1.0',
};
const report = await collect({ ...baseOptions, packageJson });
expect(report.packageName).toBe(packageJson.name);
expect(report.version).toBe(packageJson.version);
expect(() => reportSchema.parse(report)).not.toThrow();
const report = await collect(baseOptions);
expect(() =>
reportSchema.omit({ packageName: true, version: true }).parse(report),
).not.toThrow();
});
});
12 changes: 2 additions & 10 deletions packages/utils/src/lib/collect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@ export class CollectOutputError extends Error {

export type CollectOptions = GlobalCliArgs & CoreConfig;

type PackageJson = {
name?: string;
version?: string;
};

/**
* Run audits, collect plugin output and aggregate it into a JSON object
* @param options
*/
export async function collect(
options: CollectOptions & { packageJson: PackageJson },
): Promise<Report> {
const { version, name } = options.packageJson;
options: CollectOptions,
): Promise<Omit<Report, 'packageName' | 'version'>> {
const { plugins, categories } = options;

if (!plugins?.length) {
Expand All @@ -43,8 +37,6 @@ export async function collect(
const pluginOutputs = await executePlugins(plugins);

return {
packageName: name,
version,
date,
duration: calcDuration(start),
categories,
Expand Down

0 comments on commit 7d3de10

Please sign in to comment.