-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): introduce the
onlyPlugins
option (#246)
Close #119
- Loading branch information
Showing
11 changed files
with
267 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 88 additions & 17 deletions
105
packages/cli/src/lib/implementation/config-middleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,100 @@ | ||
import chalk from 'chalk'; | ||
import { readCodePushupConfig } from '@code-pushup/core'; | ||
import { CoreConfig } from '@code-pushup/models'; | ||
import { GeneralCliOptions } from './model'; | ||
import { OnlyPluginsOptions } from './only-plugins-options'; | ||
|
||
export async function configMiddleware< | ||
T extends Partial<GeneralCliOptions & CoreConfig>, | ||
T extends Partial<GeneralCliOptions & CoreConfig & OnlyPluginsOptions>, | ||
>(processArgs: T) { | ||
const args = processArgs as T; | ||
const { config, ...cliOptions } = args as GeneralCliOptions & | ||
Required<CoreConfig>; | ||
Required<CoreConfig> & | ||
OnlyPluginsOptions; | ||
const importedRc = await readCodePushupConfig(config); | ||
const parsedProcessArgs: CoreConfig & GeneralCliOptions = { | ||
config, | ||
progress: cliOptions.progress, | ||
verbose: cliOptions.verbose, | ||
upload: { | ||
...importedRc?.upload, | ||
...cliOptions?.upload, | ||
}, | ||
persist: { | ||
...importedRc.persist, | ||
...cliOptions?.persist, | ||
}, | ||
plugins: importedRc.plugins, | ||
categories: importedRc.categories, | ||
}; | ||
|
||
validateOnlyPluginsOption(importedRc.plugins, cliOptions); | ||
|
||
const parsedProcessArgs: CoreConfig & GeneralCliOptions & OnlyPluginsOptions = | ||
{ | ||
config, | ||
progress: cliOptions.progress, | ||
verbose: cliOptions.verbose, | ||
upload: { | ||
...importedRc?.upload, | ||
...cliOptions?.upload, | ||
}, | ||
persist: { | ||
...importedRc.persist, | ||
...cliOptions?.persist, | ||
}, | ||
plugins: filterPluginsByOnlyPluginsOption(importedRc.plugins, cliOptions), | ||
categories: filterCategoryByOnlyPluginsOption( | ||
importedRc.categories, | ||
cliOptions, | ||
), | ||
onlyPlugins: cliOptions.onlyPlugins, | ||
}; | ||
|
||
return parsedProcessArgs; | ||
} | ||
|
||
export function filterPluginsByOnlyPluginsOption( | ||
plugins: CoreConfig['plugins'], | ||
{ onlyPlugins }: { onlyPlugins?: string[] }, | ||
): CoreConfig['plugins'] { | ||
if (!onlyPlugins?.length) { | ||
return plugins; | ||
} | ||
return plugins.filter(plugin => onlyPlugins.includes(plugin.slug)); | ||
} | ||
|
||
// skip the whole category if it has at least one skipped plugin ref | ||
// see https://github.com/code-pushup/cli/pull/246#discussion_r1392274281 | ||
export function filterCategoryByOnlyPluginsOption( | ||
categories: CoreConfig['categories'], | ||
{ onlyPlugins }: { onlyPlugins?: string[] }, | ||
): CoreConfig['categories'] { | ||
if (!onlyPlugins?.length) { | ||
return categories; | ||
} | ||
|
||
return categories.filter(category => | ||
category.refs.every(ref => { | ||
const isNotSkipped = onlyPlugins.includes(ref.slug); | ||
|
||
if (!isNotSkipped) { | ||
console.log( | ||
`${chalk.yellow('⚠')} Category "${ | ||
category.title | ||
}" is ignored because it references audits from skipped plugin "${ | ||
ref.slug | ||
}"`, | ||
); | ||
} | ||
|
||
return isNotSkipped; | ||
}), | ||
); | ||
} | ||
|
||
export function validateOnlyPluginsOption( | ||
plugins: CoreConfig['plugins'], | ||
{ onlyPlugins }: { onlyPlugins?: string[] }, | ||
): void { | ||
const missingPlugins = onlyPlugins?.length | ||
? onlyPlugins.filter(plugin => !plugins.some(({ slug }) => slug === plugin)) | ||
: []; | ||
|
||
if (missingPlugins.length) { | ||
console.log( | ||
`${chalk.yellow( | ||
'⚠', | ||
)} The --onlyPlugin argument references plugins with "${missingPlugins.join( | ||
'", "', | ||
)}" slugs, but no such plugin is present in the configuration. Expected one of the following plugin slugs: "${plugins | ||
.map(({ slug }) => slug) | ||
.join('", "')}".`, | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/cli/src/lib/implementation/filter-kebab-case-keys.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { expect } from 'vitest'; | ||
import { filterKebabCaseKeys } from './filter-kebab-case-keys'; | ||
|
||
describe('filterKebabCaseKeys', () => { | ||
it('should filter kebab-case keys', () => { | ||
const obj = { | ||
'kebab-case': 'value', | ||
camelCase: 'value', | ||
snake_case: 'value', | ||
}; | ||
const filtered = filterKebabCaseKeys(obj); | ||
expect(filtered).toEqual({ | ||
camelCase: 'value', | ||
snake_case: 'value', | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
packages/cli/src/lib/implementation/filter-kebab-case-keys.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function filterKebabCaseKeys<T extends Record<string, unknown>>( | ||
obj: T, | ||
): T { | ||
const newObj: Record<string, unknown> = {}; | ||
|
||
Object.keys(obj).forEach(key => { | ||
if (key.includes('-')) { | ||
return; | ||
} | ||
newObj[key] = obj[key]; | ||
}); | ||
|
||
return newObj as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Options } from 'yargs'; | ||
|
||
export const onlyPluginsOption: Options = { | ||
describe: 'List of plugins to run. If not set all plugins are run.', | ||
type: 'array', | ||
default: [], | ||
coerce: (arg: string[]) => arg.flatMap(v => v.split(',')), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface OnlyPluginsOptions { | ||
onlyPlugins: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters