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
Install Husky
npm i -D husky
Install lint-staged
npm i -D lint-staged
Run husky init command
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
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:
...
"lint-staged": {
"**/*.php*": [
"vendor/bin/duster lint"
]
}
...
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"
]
}
}
Test the pre-commit hook
Now just add files & make a commit:
git add .
git commit "set duster & husky"
At this point it should lint your php files and if everything passes make the commit.
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"
]
}
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
}
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
2 - Create the file .lintstagedrc
touch .lintstagedrc
3 - Then update the file with this content:
{
"*.(js|ts)" : ["eslint"]
}
Hope it would be helpful to speed-up your code lint/fix workflow, as always thanks for reading & happy coding.
Top comments (4)
I'm trying to figure out how to make this work with laravel sail. But without success. By following your article and other resources it does not run duster as such.
Just commit without running duster
Hi @mreduar did you tried to run the command manually:
How it works?
Hey! Directly that command does not work, it has to be executed by sail i.e.
sail bin duster lint
If I run
vendor/bin/duster lint
nothing happens.Now with Herd I did not use Sail anymore, feels like overkill, but probably you would need to check if this command works
Alternatively, Sail has a bin script that eases the execution of package binaries, so you do the same thing like this:
Following this links github.com/tighten/duster?tab=read...