Skip to content

Commit

Permalink
refactor(cli): narrow down CLI options type in middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlacenka committed Feb 21, 2024
1 parent f7e6ab6 commit f509381
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
10 changes: 7 additions & 3 deletions packages/cli/src/lib/implementation/core-config.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { autoloadRc, readRcByPath } from '@code-pushup/core';
import {
CoreConfig,
PERSIST_FILENAME,
PERSIST_FORMAT,
PERSIST_OUTPUT_DIR,
uploadConfigSchema,
} from '@code-pushup/models';
import { CoreConfigCliOptions } from './core-config.model';
import { GeneralCliOptions } from './global.model';
import { OnlyPluginsOptions } from './only-plugins.model';

export async function coreConfigMiddleware<
T extends Partial<GeneralCliOptions & CoreConfigCliOptions>,
>(processArgs: T) {
T extends GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions,
>(
processArgs: T,
): Promise<GeneralCliOptions & CoreConfig & OnlyPluginsOptions> {
const {
config,
tsconfig,
Expand All @@ -19,7 +23,7 @@ export async function coreConfigMiddleware<
...remainingCliOptions
} = processArgs;

// if config path is given use it otherwise auto-load
// Search for possible configuration file extensions if path is not given
const importedRc = config
? await readRcByPath(config, tsconfig)
: await autoloadRc(tsconfig);
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/lib/implementation/core-config.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export type UploadConfigCliOptions = {
/* eslint-enable @typescript-eslint/naming-convention */

export type ConfigCliOptions = {
config: string;
config?: string;
tsconfig?: string;
};

export type CoreConfigCliOptions = Omit<CoreConfig, 'upload'> & {
export type CoreConfigCliOptions = Pick<CoreConfig, 'persist'> & {
upload?: Partial<Omit<UploadConfig, 'timeout'>>;
};
4 changes: 2 additions & 2 deletions packages/cli/src/lib/implementation/global.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ export function logErrorBeforeThrow<T extends (...args: any[]) => any>(
}) as T;
}

export function coerceArray<T extends string>(param: T | T[] = []): T[] {
return [...new Set(toArray(param).flatMap((f: T) => f.split(',') as T[]))];
export function coerceArray(param: string): string[] {
return [...new Set(toArray(param).flatMap(f => f.split(',')))];
}
26 changes: 10 additions & 16 deletions packages/cli/src/lib/implementation/only-plugins.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@ import {
} from './only-plugins.utils';

export function onlyPluginsMiddleware<
T extends Partial<GeneralCliOptions & CoreConfig & OnlyPluginsOptions>,
>(processArgs: T) {
const args = processArgs;
const cliOptions = args as GeneralCliOptions &
Required<CoreConfig> &
OnlyPluginsOptions;
T extends GeneralCliOptions &
Omit<CoreConfig, 'categories'> &
Required<Pick<CoreConfig, 'categories'>> &
OnlyPluginsOptions,
>(processArgs: T): GeneralCliOptions & CoreConfig & OnlyPluginsOptions {
validateOnlyPluginsOption(processArgs.plugins, processArgs);

validateOnlyPluginsOption(cliOptions.plugins, cliOptions);

const parsedProcessArgs: Required<CoreConfig> &
GeneralCliOptions &
OnlyPluginsOptions = {
...cliOptions,
plugins: filterPluginsBySlug(cliOptions.plugins, cliOptions),
categories: filterCategoryByPluginSlug(cliOptions.categories, cliOptions),
return {
...processArgs,
plugins: filterPluginsBySlug(processArgs.plugins, processArgs),
categories: filterCategoryByPluginSlug(processArgs.categories, processArgs),
};

return parsedProcessArgs;
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ describe('filterCategoryByPluginSlug', () => {
expect.stringContaining('skipped plugin "plugin2"'),
);
});

it('should return empty array for no categories', () => {
expect(
filterCategoryByPluginSlug([], { onlyPlugins: ['plugin1'] }),
).toEqual([]);
});
});

describe('validateOnlyPluginsOption', () => {
Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/lib/implementation/runner.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { vol } from 'memfs';
import {
AuditOutputs,
RunnerFunction,
auditOutputsSchema,
} from '@code-pushup/models';
import { AuditOutputs, auditOutputsSchema } from '@code-pushup/models';
import {
ISO_STRING_REGEXP,
MEMFS_VOLUME,
Expand Down Expand Up @@ -106,7 +102,8 @@ describe('executeRunnerFunction', () => {

it('should throw with an invalid runner type', async () => {
await expect(
executeRunnerFunction('' as unknown as RunnerFunction),
// @ts-expect-error Testing a use case with invalid type passed as a function.
executeRunnerFunction(''),
).rejects.toThrow('runner is not a function');
});
});
2 changes: 1 addition & 1 deletion packages/utils/src/lib/reports/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export function compareIssueSeverity(
type LoadedReportFormat<T extends Format> = T extends 'json' ? Report : string;

export async function loadReport<T extends Format>(
options: Omit<NonNullable<Required<PersistConfig>>, 'format'> & {
options: Required<Omit<PersistConfig, 'format'>> & {
format: T;
},
): Promise<LoadedReportFormat<T>> {
Expand Down

0 comments on commit f509381

Please sign in to comment.