The R2D2 product stack is composed of multiple NodeJS micro-services. They need to be written with the same rules, and it’s a lot of time spent maintaining every config file on all repositories.
You need to add the following NPM development dependency to the project that needs to be checked:
npm install --save-dev @r2d2bzh/js-rules
Once installed, @r2d2bzh/js-rules
provides the r2d2bzh-js-rules
executable from the node_modules executables.
You have to call r2d2bzh-js-rules
from the postinstall script of the package.json
file of your project:
{
"scripts": {
"postinstall": "r2d2bzh-js-rules"
}
}
📎
|
link:./package.json[role=include] |
To do so we are using three tools:
What is Prettier? An opinionated code formatter
This tool is responsible for the code format: comma, semicolon, width, that’s for Prettier
.
Eslint Find and fix problems in your JavaScript code
A useless variable?
var
, const
or let
?
All these conventions are settled by the eslint
configuration.
Husky can prevent a bad git commit, git push and more 🐶 woof!
This tool allows us to easily share git hooks. We are using it with the previous ones to keep our code base safe from linting errors.
When you save your file, you can ask your IDE to format your code for you.
It will follow the rules described in the .prettierrc.json
config file.
In order to make it work you may need some plugins:
The exact same thing goes for eslint.
You can ask your IDE to follow the rules specified in the eslint-config
repository.
In order to make it work you may again need some plugins:
-
You can find an IDE plugins list on eslint documentation
📎
|
These are not mandatory but strongly recommended. |
When you commit your code modifications, the following husky
hook will be executed:
link:.husky/pre-commit[role=include]
The hook will simply ensure that the project’s source code complies with the eslint
configuration.
❗
|
If an error is found during the validation the commit is canceled. You will have to first fix the lint errors before trying to commit again. |
js-rules
will also ensure that the following husky
hook is used on push:
link:.husky/pre-push[role=include]
So as long as the current branch name of your project does not reflect a work in progress, linting and tests are executed on push.
📎
|
The convention applied to tell that a branch holds a work in progress is that its name is of the form:
|