feat(next): version 5.0.0 with lefthook #151
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Polyglot Git hook management
I have been playing around with using Lefthook for managing our Git hooks for a while now, to replace Husky and LintStaged. Those two have been great for us for as long as d2-style has been a frontend-first tool.
The idea has always been to support more than just JavaScript, and while it has been possible to do so, configuration and setup for Husky and LintStaged has been difficult, forcing us to use a
validate
command which sets up everything for LintStaged -- but that only works for JavaScript.So here's a working implementation of Git hook management that works for frontend and backend that does away with most of our custom code with how we integrate tools like commitlint, prettier, eslint, which in turn makes it a lot easier to reason about and add new tools that do not have a NodeJS API.
Like all things, this comes at a cost and requires a workflow change for us as d2-style becomes less aggressive in how it goes about its business.
Trade-offs
Workflow change
Regarding the regression in functionality (losing automatically fixing and staging hunks in a pre-commit hook), the intended workflow is slightly different.
It is only important if we want to automatically apply any code formatting when we commit files, and there is an alternative workflow where we check our files for code style before we commit, and abort the commit if the code style fails.
This means that a user will have to run
d2-style js apply --staged
manually and add the changes she wants.Simpler development
Compare the files in tools:
src/tools/js/prettier.js
with the replacementsrc/tools/prettier.js
.src/tools/js/eslint.js
withsrc/tools/eslint.js
.Adding new tools in the same style is trivial, where before we relied on NodeJS API bindings to wrangle everything to how we wanted it internally.
The new structure is a lot easier to grok:
New features
Global installation
Since we don't rely on Husky to install the Git hooks on a
postinstall
we do not need to installd2-style
as a dependency in repositories that might not want apackage.json
file and running "{npm,yarn} install` in their workflow.If
d2
CLI and Lefthook is available globally, then this is enough to install the hooks in any repository:This creates a
lefthook.yml
file and installs the commit hooks in.git/hooks
.Structured text
Use Prettier to check/apply format rules for various types of structured text:
project/react
SetupGenerate ESLint configuration for a React project:
ESLint config includes recommended rules
We tried working from bottom-up principles in previous versions, adding more rules as we found them useful, but that work has died off and multiple repos are now (probably wisely) adding
eslint:recommended
to the.eslintrc.js
configuration file. This centralises and adds it together with theeslint-config-prettier
to avoid any conflicts between the two.