Skip to content

Commit

Permalink
Merge pull request #165 from ThaKladd/feature/add-environment-support…
Browse files Browse the repository at this point in the history
…-for-husky

Added environment support for husky hooks command.
  • Loading branch information
driftingly authored Nov 22, 2024
2 parents ec9bc37 + dce8d97 commit 4fcb425
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ Or you can publish Husky Hooks:
./vendor/bin/duster husky-hooks
```

If you are using a containerized environment and do not have PHP installed locally, you can use the `--env` option to specify it:

```bash
./vendor/bin/duster husky-hooks --env=ddev
```

Supported environments are `ddev`, `warden` `lando`, and `sail`.

## Usage

To lint everything at once:
Expand Down
16 changes: 12 additions & 4 deletions app/Commands/HuskyHooksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class HuskyHooksCommand extends Command
{
use CommandHelpers;

protected $signature = 'husky-hooks';
protected $signature = 'husky-hooks {--env= : Development environment (ddev, lando, warden, sail)}';

protected $description = 'Publish Husky Hooks';

Expand All @@ -20,6 +20,14 @@ class HuskyHooksCommand extends Command
*/
public function handle(): int
{
$exec = match ($this->option('env')) {
'ddev' => 'ddev exec ',
'lando' => 'lando ',
'warden' => 'warden env exec ',
'sail' => 'sail exec ',
default => '',
};

$choices = [
'Lint only' => 'lint',
'Fix and commit' => 'fix',
Expand All @@ -34,11 +42,11 @@ public function handle(): int
$this->runCommands(['git init']);

if (! file_exists(base_path('node_modules/husky'))) {
$this->runCommands(['npx husky-init']);
$this->runCommands([$exec . 'npx husky-init']);
}

if (! file_exists(base_path('node_modules/lint-staged'))) {
$this->runCommands(['npm install lint-staged --save-dev']);
$this->runCommands([$exec . 'npm install lint-staged --save-dev']);
}

$preCommit = file_get_contents(__DIR__ . '/../../stubs/husky-hooks/pre-commit');
Expand All @@ -53,7 +61,7 @@ public function handle(): int

file_put_contents(getcwd() . '/lint-staged.config.js', $lintStaged);

$this->runCommands(["npx husky add ./.husky/pre-commit 'npx --no-install lint-staged'"]);
$this->runCommands(["npx husky add ./.husky/pre-commit '{$exec}npx --no-install lint-staged'"]);

$this->success('Husky Pre-Commit Git Hook added');

Expand Down

0 comments on commit 4fcb425

Please # to comment.