DEV Community

Ariel Mejia
Ariel Mejia

Posted on

Run Duster with Husky in Laravel

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:

git init
Enter fullscreen mode Exit fullscreen mode

Install Husky

npm i -D husky
Enter fullscreen mode Exit fullscreen mode

Install lint-staged

npm i -D lint-staged
Enter fullscreen mode Exit fullscreen mode

Run husky init command

npx husky-init
Enter fullscreen mode Exit fullscreen mode

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

npx husky add ./.husky/pre-commit 'npx --no-install lint-staged'
Enter fullscreen mode Exit fullscreen mode

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:

    ...
    "lint-staged": {
        "**/*.php*": [
            "vendor/bin/duster lint"
        ]
    }
    ...
Enter fullscreen mode Exit fullscreen mode

The file should look like this:

{
    "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"
        ]
    }
}
Enter fullscreen mode Exit fullscreen mode

Test the pre-commit hook

Now just add files & make a commit:

git add .
git commit "set duster & husky"
Enter fullscreen mode Exit fullscreen mode

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

Terminal output

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:

    "lint-staged": {
        "**/*.php*": [
            "vendor/bin/duster fix"
        ]
    }
Enter fullscreen mode Exit fullscreen mode

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

Backend Tools

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:

{
    "scripts": {
        "lint": {
            "phpstan": ["./vendor/bin/phpstan", "analyse"]
        }
    },
    "processTimeout": 120
}
Enter fullscreen mode Exit fullscreen mode

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"

Frontend Tools

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

npx eslint --init
Enter fullscreen mode Exit fullscreen mode

2 - Create the file .lintstagedrc

touch .lintstagedrc
Enter fullscreen mode Exit fullscreen mode

3 - Then update the file with this content:

{
  "*.(js|ts)" : ["eslint"]
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)