diff --git a/README.md b/README.md index 71187c9..1abcb25 100644 --- a/README.md +++ b/README.md @@ -152,3 +152,19 @@ These are the default options for `phpstan`. All are optional and you can overri ``` This runs `phpstan` on the code base with the specified options. For the defaults to work, you must have a `phpstan.neon` configuration file in your codebase as [mentioned in the documentation](https://phpstan.org/user-guide/command-line-usage#running-without-arguments). Note that the default `phpstan.neon` as generated by [`axelerant/drupal-quality-checker`](https://github.com/axelerant/drupal-quality-checker) is valid for this purpose. + +### custom + +This allows you to run any arbitrary commands that you may want to run from within the tools that are available within the Docker image. This is useful if you want to run one of the tools available in the drupalqa Docker image but there is no custom check written above, or if you want to use a configuration option not available above. In any case, if you think what you are trying to run here is common enough, consider submitting an issue and/or a pull request to add the check. Look at other pull requests such as #121 and #122 to see how to write a check. + +Example usage: + +```yaml + checks: | + custom_linters: + command: ['grumphp', 'run', '--testsuite=linters'] + custom_stylecheck: + command: ['grumphp', 'run', '--testsuite=style'] +``` + +The above will run two commands as shown. The `custom` check was introduced for situations where we need to run the same command twice with different options. This is otherwise not possible using the typical check format as keys may not be repeated in YAML. diff --git a/__tests__/custom.test.ts b/__tests__/custom.test.ts new file mode 100644 index 0000000..232cf3e --- /dev/null +++ b/__tests__/custom.test.ts @@ -0,0 +1,28 @@ +import custom from '../src/checks/custom' +import {expect, test} from '@jest/globals' + +test('it returns defaults', () => { + expect(custom({}, 'web')).toEqual([]) +}) + +test('it handles empty commands', () => { + let command + command = custom( + { + command: [] + }, + 'web' + ) + expect(command).toEqual([]) +}) + +test('it handles commands', () => { + let command + command = custom( + { + command: ['phpmd'] + }, + 'docroot' + ) + expect(command).toEqual(['phpmd']) +}) diff --git a/src/checks/custom.ts b/src/checks/custom.ts new file mode 100644 index 0000000..46d3133 --- /dev/null +++ b/src/checks/custom.ts @@ -0,0 +1,9 @@ +import {CustomOptions} from '../types' + +export default function custom( + options: CustomOptions, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + webRoot: string +): string[] { + return options.command !== undefined ? options.command : [] +} diff --git a/src/main.ts b/src/main.ts index 7baf452..09e32a9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ import * as YAML from 'yaml' import {CheckCallable} from './types' +import custom from './checks/custom' import grumphp from './checks/grumphp' import phplint from './checks/phplint' import phpcs from './checks/phpcs' @@ -13,6 +14,7 @@ import phpstan from './checks/phpstan' const availableChecks: { [key: string]: CheckCallable } = { + custom, grumphp, phplint, phpcs, @@ -62,6 +64,8 @@ async function run(): Promise { } if (key in availableChecks) { checksCommands.push(availableChecks[key](value, webRoot)) + } else if (key.startsWith('custom_')) { + checksCommands.push(availableChecks['custom'](value, webRoot)) } else { throw new Error(`invalid check ${key} specified.`) } diff --git a/src/types.ts b/src/types.ts index 660f0b5..87a2c82 100644 --- a/src/types.ts +++ b/src/types.ts @@ -37,3 +37,7 @@ export interface PhpStanOptions { configuration?: string paths?: string[] } + +export interface CustomOptions { + command?: string[] +}