Skip to content

Commit

Permalink
feat: add skipPlugin options to global configs
Browse files Browse the repository at this point in the history
  • Loading branch information
nacho-vazquez authored and matejchalk committed Jul 11, 2024
1 parent 7369585 commit 64f45da
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
4 changes: 4 additions & 0 deletions packages/cli/src/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export const cli = (args: string[]) =>
'code-pushup collect --onlyPlugins=coverage',
'Run collect with only coverage plugin, other plugins from config file will be skipped.',
],
[
'code-pushup collect --skipPlugins=coverage',
'Run collect skiping the coverage plugin, other plugins from config file will be included.',
],
[
'code-pushup upload --persist.outputDir=dist --persist.filename=cp-report --upload.apiKey=$CP_API_KEY',
'Upload dist/cp-report.json to portal using API key from environment variable',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('coreConfigMiddleware', () => {
progress: true,
verbose: false,
onlyPlugins: [],
skipPlugins: [],
};

it.each(['ts', 'mjs', 'js'])(
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/lib/implementation/core-config.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import {
import { CoreConfigCliOptions } from './core-config.model';
import { GeneralCliOptions } from './global.model';
import { OnlyPluginsOptions } from './only-plugins.model';
import { SkipPluginsOptions } from './skip-plugins.model';

export async function coreConfigMiddleware<
T extends GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions,
T extends GeneralCliOptions &
CoreConfigCliOptions &
OnlyPluginsOptions &
SkipPluginsOptions,
>(
processArgs: T,
): Promise<GeneralCliOptions & CoreConfig & OnlyPluginsOptions> {
): Promise<
GeneralCliOptions & CoreConfig & OnlyPluginsOptions & SkipPluginsOptions
> {
const {
config,
tsconfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { coreConfigMiddleware } from './core-config.middleware';
import { CoreConfigCliOptions } from './core-config.model';
import { GeneralCliOptions } from './global.model';
import { OnlyPluginsOptions } from './only-plugins.model';
import { SkipPluginsOptions } from './skip-plugins.model';

vi.mock('@code-pushup/core', async () => {
const { CORE_CONFIG_MOCK }: typeof import('@code-pushup/test-utils') =
Expand All @@ -19,15 +20,18 @@ vi.mock('@code-pushup/core', async () => {
describe('coreConfigMiddleware', () => {
it('should attempt to load code-pushup.config.(ts|mjs|js) by default', async () => {
await coreConfigMiddleware(
{} as GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions,
{} as GeneralCliOptions &
CoreConfigCliOptions &
OnlyPluginsOptions &
SkipPluginsOptions,
);
expect(autoloadRc).toHaveBeenCalled();
});

it('should directly attempt to load passed config', async () => {
await coreConfigMiddleware({
config: 'cli/custom-config.mjs',
} as GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions);
} as GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions & SkipPluginsOptions);
expect(autoloadRc).not.toHaveBeenCalled();
expect(readRcByPath).toHaveBeenCalledWith(
'cli/custom-config.mjs',
Expand All @@ -38,15 +42,15 @@ describe('coreConfigMiddleware', () => {
it('should forward --tsconfig option to config autoload', async () => {
await coreConfigMiddleware({
tsconfig: 'tsconfig.base.json',
} as GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions);
} as GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions & SkipPluginsOptions);
expect(autoloadRc).toHaveBeenCalledWith('tsconfig.base.json');
});

it('should forward --tsconfig option to custom config load', async () => {
await coreConfigMiddleware({
config: 'apps/website/code-pushup.config.ts',
tsconfig: 'apps/website/tsconfig.json',
} as GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions);
} as GeneralCliOptions & CoreConfigCliOptions & OnlyPluginsOptions & SkipPluginsOptions);
expect(readRcByPath).toHaveBeenCalledWith(
'apps/website/code-pushup.config.ts',
'apps/website/tsconfig.json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('onlyPluginsMiddleware', () => {
expect(plugins).toStrictEqual([expect.objectContaining({ slug: 'p1' })]);
});

it('should filter categories for slug "p1"', () => {
it('should forward plugins and categories for a slug not present in plugins', () => {
const originalCategories = [
{
slug: 'c1',
Expand All @@ -69,7 +69,7 @@ describe('onlyPluginsMiddleware', () => {
expect(plugins).toBe(originalPlugins);
});

it('should forward plugins and categories for a slug not present in plugins', () => {
it('should filter categories for slug "p1"', () => {
const { categories } = onlyPluginsMiddleware({
onlyPlugins: ['p1'],
plugins: [{ slug: 'p1' }, { slug: 'p2' }] as PluginConfig[],
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/lib/middlewares.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MiddlewareFunction } from 'yargs';
import { coreConfigMiddleware } from './implementation/core-config.middleware';
import { onlyPluginsMiddleware } from './implementation/only-plugins.middleware';
import { skipPluginsMiddleware } from './implementation/skip-plugins.middleware';

export const middlewares = [
{
Expand All @@ -11,4 +12,8 @@ export const middlewares = [
middlewareFunction: onlyPluginsMiddleware as unknown as MiddlewareFunction,
applyBeforeValidation: false,
},
{
middlewareFunction: skipPluginsMiddleware as unknown as MiddlewareFunction,
applyBeforeValidation: false,
},
];
3 changes: 3 additions & 0 deletions packages/cli/src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import {
} from './implementation/core-config.options';
import { yargsGlobalOptionsDefinition } from './implementation/global.options';
import { yargsOnlyPluginsOptionsDefinition } from './implementation/only-plugins.options';
import { yargsSkipPluginsOptionsDefinition } from './implementation/skip-plugins.options';

export const options = {
...yargsGlobalOptionsDefinition(),
...yargsCoreConfigOptionsDefinition(),
...yargsOnlyPluginsOptionsDefinition(),
...yargsSkipPluginsOptionsDefinition(),
};

export const groups = {
'Global Options:': [
...Object.keys(yargsGlobalOptionsDefinition()),
...Object.keys(yargsOnlyPluginsOptionsDefinition()),
...Object.keys(yargsSkipPluginsOptionsDefinition()),
],
'Persist Options:': Object.keys(yargsPersistConfigOptionsDefinition()),
'Upload Options:': Object.keys(yargsUploadConfigOptionsDefinition()),
Expand Down

0 comments on commit 64f45da

Please sign in to comment.