Skip to content

Commit ef37bb8

Browse files
committed
wip PrettierPreCommitHook.php
1 parent 440920f commit ef37bb8

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

config/git-hooks.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@
187187
'path' => env('BLADE_FORMATTER_PATH', 'node_modules/.bin/blade-formatter'),
188188
'config' => env('BLADE_FORMATTER_CONFIG', '.bladeformatterrc.json'),
189189
],
190+
'prettier' => [
191+
'path' => env('PRETTIER_PATH', 'node_modules/.bin/prettier'),
192+
'config' => env('PRETTIER_CONFIG', '.prettierrc.json'),
193+
'additional_params' => env('PRETTIER_ADDITIONAL_PARAMS', ''),
194+
],
190195
],
191196

192197
];
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Igorsgm\GitHooks\Console\Commands\Hooks;
4+
5+
use Closure;
6+
use Igorsgm\GitHooks\Contracts\CodeAnalyzerPreCommitHook;
7+
use Igorsgm\GitHooks\Git\ChangedFiles;
8+
9+
class PrettierPreCommitHook extends BaseCodeAnalyzerPreCommitHook implements CodeAnalyzerPreCommitHook
10+
{
11+
/**
12+
* @var string
13+
*/
14+
protected $configParam;
15+
16+
/**
17+
* Name of the hook
18+
*
19+
* @var string
20+
*/
21+
protected $name = 'Prettier';
22+
23+
/**
24+
* Analyze and fix committed JS files using Prettier.
25+
*
26+
* @param ChangedFiles $files The files that have been changed in the current commit.
27+
* @param Closure $next A closure that represents the next middleware in the pipeline.
28+
* @return mixed|null
29+
*/
30+
public function handle(ChangedFiles $files, Closure $next)
31+
{
32+
$this->configParam = $this->configParam();
33+
34+
return $this->setFileExtensions('/\.(jsx?|tsx?|vue)$/')
35+
->setAnalyzerExecutable(config('git-hooks.code_analyzers.prettier.path'), true)
36+
->handleCommittedFiles($files, $next);
37+
}
38+
39+
/**
40+
* Returns the command to run Prettier tester
41+
*/
42+
public function analyzerCommand(): string
43+
{
44+
return trim(
45+
sprintf('%s --check %s %s', $this->getAnalyzerExecutable(), $this->configParam, $this->additionalParams())
46+
);
47+
}
48+
49+
/**
50+
* Returns the command to run Prettier fixer
51+
*/
52+
public function fixerCommand(): string
53+
{
54+
return trim(
55+
sprintf('%s --write %s %s', $this->getFixerExecutable(), $this->configParam, $this->additionalParams())
56+
);
57+
}
58+
59+
/**
60+
* Gets the command-line parameter for specifying the configuration file for Prettier.
61+
*
62+
* @return string The command-line parameter for the configuration file, or an empty string if not set.
63+
*/
64+
protected function configParam(): string
65+
{
66+
$prettierConfig = rtrim(config('git-hooks.code_analyzers.prettier.config'), '/');
67+
68+
return empty($prettierConfig) ? '' : '--config='.$prettierConfig;
69+
}
70+
71+
/**
72+
* Retrieves additional parameters for the Prettier code analyzer from the configuration file,
73+
* filters out pre-defined parameters to avoid conflicts, and returns them as a string.
74+
*
75+
* @return string
76+
*/
77+
protected function additionalParams(): string
78+
{
79+
$additionalParams = config('git-hooks.code_analyzers.prettier.additional_params');
80+
81+
if (!empty($additionalParams)) {
82+
$additionalParams = preg_replace('/\s*--(config|find-config-path|write|check)\b(=\S*)?\s*/', '', $additionalParams);
83+
}
84+
85+
return $additionalParams;
86+
}
87+
}

0 commit comments

Comments
 (0)