Skip to content

Commit ac4972e

Browse files
authored
Merge pull request #12 from netgen/NGSTACK-789-js-linter-and-prettier
NGSTACK-789 add extensions array option
2 parents 6ebf7d2 + 44bfeed commit ac4972e

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/Action/Action.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class Action implements ActionInterface
1818
{
1919
protected const ERROR_MESSAGE = "I'm sorry, Dave. I'm afraid I can't do that. Please check your commit for errors";
2020

21-
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
21+
public function execute(Config $config, IO $io, Repository $repository, ActionConfig $action): void
2222
{
2323
if (!$this->isEnabled($action)) {
2424
return;
@@ -27,7 +27,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
2727
$this->doExecute($config, $io, $repository, $action);
2828
}
2929

30-
abstract protected function doExecute(Config $config, IO $io, Repository $repository, Config\Action $action): void;
30+
abstract protected function doExecute(Config $config, IO $io, Repository $repository, ActionConfig $action): void;
3131

3232
protected function throwError(ActionConfig $config, IO $io): void
3333
{

src/Action/JSLinter.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor;
1010
use SebastianFeldmann\Git\Repository;
1111

12+
use function array_merge;
1213
use function count;
1314
use function escapeshellarg;
1415
use function preg_match;
@@ -19,8 +20,14 @@ final class JSLinter extends Action
1920

2021
protected function doExecute(Config $config, IO $io, Repository $repository, Config\Action $action): void
2122
{
22-
$changedJsFiles = $repository->getIndexOperator()->getStagedFilesOfType('js');
23-
if (count($changedJsFiles) === 0) {
23+
$extensions = $action->getOptions()->get('extensions', ['js']);
24+
25+
$changedFiles = [];
26+
foreach ($extensions as $extension) {
27+
$changedFiles = array_merge($changedFiles, $repository->getIndexOperator()->getStagedFilesOfType($extension));
28+
}
29+
30+
if (count($changedFiles) === 0) {
2431
return;
2532
}
2633

@@ -30,7 +37,7 @@ protected function doExecute(Config $config, IO $io, Repository $repository, Con
3037
$linterOptions = $action->getOptions()->get('linter_options', '--fix-dry-run');
3138

3239
$io->write('Running linter on files:', true, IO::VERBOSE);
33-
foreach ($changedJsFiles as $file) {
40+
foreach ($changedFiles as $file) {
3441
if ($this->shouldSkipFileCheck($file, $excludedFiles)) {
3542
continue;
3643
}

src/Action/JSPrettier.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor;
1010
use SebastianFeldmann\Git\Repository;
1111

12+
use function array_merge;
1213
use function count;
1314
use function escapeshellarg;
1415
use function preg_match;
@@ -19,8 +20,14 @@ final class JSPrettier extends Action
1920

2021
protected function doExecute(Config $config, IO $io, Repository $repository, Config\Action $action): void
2122
{
22-
$changedJsFiles = $repository->getIndexOperator()->getStagedFilesOfType('js');
23-
if (count($changedJsFiles) === 0) {
23+
$extensions = $action->getOptions()->get('extensions', ['js']);
24+
25+
$changedFiles = [];
26+
foreach ($extensions as $extension) {
27+
$changedFiles = array_merge($changedFiles, $repository->getIndexOperator()->getStagedFilesOfType($extension));
28+
}
29+
30+
if (count($changedFiles) === 0) {
2431
return;
2532
}
2633

@@ -30,7 +37,7 @@ protected function doExecute(Config $config, IO $io, Repository $repository, Con
3037
$prettierOptions = $action->getOptions()->get('prettier_options', '--check');
3138

3239
$io->write('Running prettier on files:', true, IO::VERBOSE);
33-
foreach ($changedJsFiles as $file) {
40+
foreach ($changedFiles as $file) {
3441
if ($this->shouldSkipFileCheck($file, $excludedFiles)) {
3542
continue;
3643
}

0 commit comments

Comments
 (0)