Skip to content

Commit

Permalink
Add option to customize checked file extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
aboks committed Apr 14, 2020
1 parent c850439 commit f7bc26f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

moxio/captainhook-eslint
========================
This project is a plugin for [CaptainHook](https://github.com/captainhookphp/captainhook) to check JavaScript files
This project is a plugin for [CaptainHook](https://github.com/captainhookphp/captainhook) to check JavaScript files
using [ESLint](https://eslint.org/) in a git pre-commit hook. By default all `*.js` and `*.mjs` files are checked, and
the commit is blocked when one or more errors are found. Warnings produced by ESLint are ignored.

Expand All @@ -23,19 +23,19 @@ Add ESLint validation as a `pre-commit` to your `captainhook.json` configuration
"actions": [
{
"action": "\\Moxio\\CaptainHook\\ESLint\\ESLintAction"
}
]
}
]
}
}
```

The action expects [ESLint](https://eslint.org/) to be installed as a local NPM package (i.e. available at
The action expects [ESLint](https://eslint.org/) to be installed as a local NPM package (i.e. available at
`node_modules/.bin/eslint`). It should be [configured](https://eslint.org/docs/user-guide/configuring#configuring-eslint)
in a way that automatically finds the appropriate configuration, e.g. as an `.eslintrc.*` file or with the `eslintConfig`
field in `package.json`.

### Conditional usage
If you want to perform ESLint validation only when ESLint is installed (i.e. available at `node_modules/.bin/eslint`),
If you want to perform ESLint validation only when ESLint is installed (i.e. available at `node_modules/.bin/eslint`),
you can add a corresponding condition to the action:
```json
{
Expand All @@ -49,15 +49,36 @@ you can add a corresponding condition to the action:
"exec": "\\Moxio\\CaptainHook\\ESLint\\Condition\\ESLintInstalled"
}
]
}
]
}
]
}
}
```
This may be useful in scenarios where you have a shared CaptainHook configuration file that is
This may be useful in scenarios where you have a shared CaptainHook configuration file that is
[included](https://captainhookphp.github.io/captainhook/configure.html#includes) both in projects that use ESLint and
projects that don't. If ESLint is installed, the action is run. In projects without ESLint, the validation is skipped.

### Configuring checked file extensions

By default, committed files with a `.js` or `.mjs` extension will be checked. If you want to customize this,
e.g. to also validate TypeScript files, you can do so with the `extensions` option, which accepts an array
with extensions of files to lint:
```json
{
"pre-commit": {
"enabled": true,
"actions": [
{
"action": "\\Moxio\\CaptainHook\\ESLint\\ESLintAction",
"options": {
"extensions": [ "js", "mjs", "ts", "tsx" ]
}
}
]
}
}
```

Versioning
----------
This project adheres to [Semantic Versioning](http://semver.org/).
Expand Down
11 changes: 7 additions & 4 deletions src/ESLintAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

class ESLintAction implements Action {
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void {
$options = $action->getOptions();
$extensions = $options->get("extensions", ["js", "mjs"]);

$index_operator = $repository->getIndexOperator();
$changed_js_files = array_merge(
$index_operator->getStagedFilesOfType("js"),
$index_operator->getStagedFilesOfType("mjs")
);
$changed_js_files = [];
foreach ($extensions as $extension) {
$changed_js_files = array_merge($changed_js_files, $index_operator->getStagedFilesOfType($extension));
}

if (count($changed_js_files) === 0) {
return;
Expand Down

0 comments on commit f7bc26f

Please sign in to comment.