Run Duster with Husky in Laravel
Tags: laravel, php, husky, duster
Published: 2023-07-19 12:44:52

Duster is a great tool as code linter & fixer for Laravel, in words of Tighten the company that creates the package:

`Takes the best of Laravel’s Pint, together with the power of PHP_CodeSniffer and PHP-CS-Fixer configured the Tighten way... Duster works on the command line, but you can also integrate it with Husky to run it automatically in response to local Git triggers, or use our premade GitHub Action to run it in your CI pipeline.` 

The package has a great command to set Github Actions, but set husky locally is a little bit more tricky, in this post we are going to set Duster to run code lint with a pre-commit hook of husky.


### Initialize a GIT repository

If the project already has a git repository initialized you should skip this command:

```bash
git init
```

### Install Husky

```bash
npm i -D husky
```

### Install lint-staged

```bash
npm i -D lint-staged
```

### Run husky init command

```bash
npx husky-init
```

it should add a new script on `package.json` file with key "prepare" and execute it, this would add a new `.husky` directory.

### Update Husky pre-commit

```bash
npx husky add ./.husky/pre-commit 'npx --no-install lint-staged'
```

You can go to `./husky/pre-commit` file and remove the npm test line. 

### Configure lint-staged for all *.PHP files

Update your `package.json` file by adding this section:

```json
    ...
    "lint-staged": {
        "**/*.php*": [
            "vendor/bin/duster lint"
        ]
    }
    ...
```

The file should look like this:

```json
{
    "private": true,
    "type": "module",
    "scripts": {
        "dev": "vite",
        "build": "vite build",
        "prepare": "husky install"
    },
    "devDependencies": {
        "axios": "^1.1.2",
        "laravel-vite-plugin": "^0.7.5",
        "vite": "^4.0.0",
        "husky": "^8.0.0"
    },
    "dependencies": {
        "husky": "^8.0.3",
        "lint-staged": "^13.2.3"
    },
    "lint-staged": {
        "**/*.php*": [
            "vendor/bin/duster lint"
        ]
    }
}
```

### Test the pre-commit hook

Now just add files & make a commit:

```bash
git add .
git commit "set duster & husky"
```

At this point it should lint your php files and if everything passes make the commit.


![Terminal output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpcmqkbds0h1ciajmvm2.png)



### Advance options

#### Fix code automatically with pre-commit hook

Instead of running a lint, probably you would prefer to automatically fix your code, in this case update the `package.json` file by changing the command:

```json
    "lint-staged": {
        "**/*.php*": [
            "vendor/bin/duster fix"
        ]
    }
```

#### Set custom configurations

Duster uses behind the scenes `Laravel Pint`, `PHP_CodeSniffer` & `PHP-CS-Fixer`, you could add configuration files and set the tools as you required for your own project.

### Add more tools to run with duster

<strong>Backend Tools</strong>

For backend you can add more tools to run with Duster by create/update the `duster.json` file, as an example from the package readme file you can add PHPStan:

```json
{
    "scripts": {
        "lint": {
            "phpstan": ["./vendor/bin/phpstan", "analyse"]
        }
    },
    "processTimeout": 120
}
```

You can customize wich tool & the order to run them on Duster with flag `--using` in the duster commands (lint/fix): `./vendor/bin/duster lint --using="phpstan,tlint,pint"`

<strong>Frontend Tools</strong>

To run lint for frontend technologies you could use `lint-staged` to run a lint, in this example it would add ESlint:

1 - Install ESLint

```bash
npx eslint --init
```

2 - Create the file `.lintstagedrc`

```bash
touch .lintstagedrc
```

3 - Then update the file with this content:

```json
{
  "*.(js|ts)" : ["eslint"]
}
```

Hope it would be helpful to speed-up your code lint/fix workflow, as always thanks for reading & happy coding.