diff --git a/src/Action/CheckForTypescriptErrors.php b/src/Action/CheckForTypescriptErrors.php new file mode 100644 index 0000000..e2c6b63 --- /dev/null +++ b/src/Action/CheckForTypescriptErrors.php @@ -0,0 +1,52 @@ +getOptions()->get('config_dirs', ['.']); + $typescriptCompilerCommand = $action->getOptions()->get('typescript_compiler_command', 'npx tsc'); + $typescriptCompilerOptions = $action->getOptions()->get('typescript_compiler_options', '--noEmit'); + + $io->write(sprintf('Running %s on files:', $typescriptCompilerCommand), true, IO::VERBOSE); + foreach ($configDirs as $dir) { + $io->write(sprintf(' - %s', $dir), true, IO::VERBOSE); + + $result = $this->checkTypescriptErrors($dir, $typescriptCompilerCommand, $typescriptCompilerOptions); + $io->write($result['output']); + + if ($result['success'] !== true) { + $this->throwError($action, $io); + } + } + } + + /** + * @return array + */ + protected function checkTypescriptErrors(string $dir, string $typescriptCompilerCommand, string $typescriptCompilerOptions): array + { + $process = new Processor(); + $result = $process->run($typescriptCompilerCommand . ' ' . $typescriptCompilerOptions . ' --project ' . escapeshellarg($dir)); + + return [ + 'success' => $result->isSuccessful(), + 'output' => $result->getStdOut(), + ]; + } +} diff --git a/src/Action/CheckLinter.php b/src/Action/CheckLinter.php new file mode 100644 index 0000000..76bc681 --- /dev/null +++ b/src/Action/CheckLinter.php @@ -0,0 +1,56 @@ +getOptions()->get('directories', ['assets']); + $linterCommand = $action->getOptions()->get('linter_command', 'pnpm eslint'); + + $io->write(sprintf('Running %s on files:', $linterCommand), true, IO::VERBOSE); + + $result = $this->checkLinter($directories, $linterCommand); + $io->write($result['output']); + + if ($result['success'] !== true) { + $this->throwError($action, $io); + } + } + + /** + * @param string[] $directories + * + * @return array + */ + protected function checkLinter(array $directories, string $linterCommand): array + { + $process = new Processor(); + $cliString = $linterCommand; + + foreach ($directories as $directory) { + $cliString .= ' ' . escapeshellarg($directory); + } + + $result = $process->run($cliString); + + return [ + 'success' => $result->isSuccessful(), + 'output' => $result->getStdOut(), + ]; + } +} diff --git a/src/Action/CheckPrettier.php b/src/Action/CheckPrettier.php new file mode 100644 index 0000000..5e10567 --- /dev/null +++ b/src/Action/CheckPrettier.php @@ -0,0 +1,100 @@ +getOptions()->get('extensions', ['js', 'jsx', 'ts', 'tsx', 'css', 'scss']); + $excludedFiles = $action->getOptions()->get('excluded_files') ?? []; + $directories = $action->getOptions()->get('directories', ['assets']); + $prettierCommand = $action->getOptions()->get('prettier_command', 'pnpm prettier'); + $formatOptions = $action->getOptions()->get('prettier_options', '--check'); + + $finder = new Finder(); + preg_filter('/^/', '*.', $extensions); + $finder->in($directories)->files()->name($extensions); + + if ($finder->hasResults()) { + $io->write(sprintf('Running %s on files:', $prettierCommand)); + + foreach ($finder as $file) { + if ($this->shouldSkipFileCheck($file->getPath(), $excludedFiles)) { + continue; + } + + $result = $this->checkPrettier($file->getPath(), $prettierCommand, $formatOptions); + + $io->write(sprintf('%s: ', $file->getPath())); + + /** @var bool $isResultSuccess */ + $isResultSuccess = $result['success']; + + if ($isResultSuccess) { + $io->write($result['output']); + } else { + $io->writeError(sprintf('%s', $result['error'])); + } + + if ($result['success'] !== true) { + $this->throwError($action, $io); + } + } + } + } + + /** + * @param string[] $excludedFiles + */ + protected function shouldSkipFileCheck(string $file, array $excludedFiles): bool + { + foreach ($excludedFiles as $excludedFile) { + // File definition using regexp + if ($excludedFile[0] === '/') { + if (preg_match($excludedFile, $file) === 1) { + return true; + } + + continue; + } + if ($excludedFile === $file) { + return true; + } + } + + return false; + } + + /** + * @return array + */ + protected function checkPrettier(string $file, string $prettierCommand, string $prettierOptions): array + { + $process = new Processor(); + $result = $process->run($prettierCommand . ' ' . $prettierOptions . ' ' . escapeshellarg($file)); + + return [ + 'success' => $result->isSuccessful(), + 'output' => $result->getStdOut(), + 'error' => $result->getStdErr(), + ]; + } +}