Skip to content

Commit

Permalink
Merge pull request #124 from hussainweb/custom-check
Browse files Browse the repository at this point in the history
feat: add custom check
  • Loading branch information
hussainweb authored May 16, 2024
2 parents 51f3b45 + 4d2eb55 commit 17b0b9d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
28 changes: 28 additions & 0 deletions __tests__/custom.test.ts
Original file line number Diff line number Diff line change
@@ -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'])
})
21 changes: 21 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/checks/custom.ts
Original file line number Diff line number Diff line change
@@ -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 : []
}
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -13,6 +14,7 @@ import phpstan from './checks/phpstan'
const availableChecks: {
[key: string]: CheckCallable
} = {
custom,
grumphp,
phplint,
phpcs,
Expand Down Expand Up @@ -62,6 +64,8 @@ async function run(): Promise<void> {
}
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.`)
}
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ export interface PhpStanOptions {
configuration?: string
paths?: string[]
}

export interface CustomOptions {
command?: string[]
}

0 comments on commit 17b0b9d

Please sign in to comment.