Skip to content

Commit

Permalink
Merge pull request #12 from netgen/NGSTACK-789-js-linter-and-prettier
Browse files Browse the repository at this point in the history
NGSTACK-789 add extensions array option
  • Loading branch information
emodric authored Mar 20, 2024
2 parents 6ebf7d2 + 44bfeed commit ac4972e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Action/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class Action implements ActionInterface
{
protected const ERROR_MESSAGE = "I'm sorry, Dave. I'm afraid I can't do that. Please check your commit for errors";

public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void
public function execute(Config $config, IO $io, Repository $repository, ActionConfig $action): void
{
if (!$this->isEnabled($action)) {
return;
Expand All @@ -27,7 +27,7 @@ public function execute(Config $config, IO $io, Repository $repository, Config\A
$this->doExecute($config, $io, $repository, $action);
}

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

protected function throwError(ActionConfig $config, IO $io): void
{
Expand Down
13 changes: 10 additions & 3 deletions src/Action/JSLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor;
use SebastianFeldmann\Git\Repository;

use function array_merge;
use function count;
use function escapeshellarg;
use function preg_match;
Expand All @@ -19,8 +20,14 @@ final class JSLinter extends Action

protected function doExecute(Config $config, IO $io, Repository $repository, Config\Action $action): void
{
$changedJsFiles = $repository->getIndexOperator()->getStagedFilesOfType('js');
if (count($changedJsFiles) === 0) {
$extensions = $action->getOptions()->get('extensions', ['js']);

$changedFiles = [];
foreach ($extensions as $extension) {
$changedFiles = array_merge($changedFiles, $repository->getIndexOperator()->getStagedFilesOfType($extension));
}

if (count($changedFiles) === 0) {
return;
}

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

$io->write('Running linter on files:', true, IO::VERBOSE);
foreach ($changedJsFiles as $file) {
foreach ($changedFiles as $file) {
if ($this->shouldSkipFileCheck($file, $excludedFiles)) {
continue;
}
Expand Down
13 changes: 10 additions & 3 deletions src/Action/JSPrettier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor;
use SebastianFeldmann\Git\Repository;

use function array_merge;
use function count;
use function escapeshellarg;
use function preg_match;
Expand All @@ -19,8 +20,14 @@ final class JSPrettier extends Action

protected function doExecute(Config $config, IO $io, Repository $repository, Config\Action $action): void
{
$changedJsFiles = $repository->getIndexOperator()->getStagedFilesOfType('js');
if (count($changedJsFiles) === 0) {
$extensions = $action->getOptions()->get('extensions', ['js']);

$changedFiles = [];
foreach ($extensions as $extension) {
$changedFiles = array_merge($changedFiles, $repository->getIndexOperator()->getStagedFilesOfType($extension));
}

if (count($changedFiles) === 0) {
return;
}

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

$io->write('Running prettier on files:', true, IO::VERBOSE);
foreach ($changedJsFiles as $file) {
foreach ($changedFiles as $file) {
if ($this->shouldSkipFileCheck($file, $excludedFiles)) {
continue;
}
Expand Down

0 comments on commit ac4972e

Please sign in to comment.