Skip to content

Commit

Permalink
Git Hook: lint-staged (#18)
Browse files Browse the repository at this point in the history
Git Hooks [1] are a fantastic way to customize the development workflow
of a project to simplify and automate specific tasks that are required
when working on the code base. For example, this includes tasks like
formatting, "linting" and running tests before pushing a commit to
ensure it conforms to the code style and works as expected.
Most Git Hooks are not that complex and fullfil a simple purpose while
other solutions like Danger [2] can help to manage larger projects and
projects that need to scale.

This "base" repository template initially uses a Git Hook that
automatically runs configured linters on all files that have been staged
and that match the configured pattern (file extension, filename etc.).
Like documented in GH-15, NodeJS [3] is already a development dependency
anyway so the lint-staged [4] NPM package is used for this goal.
I've used this package in almost any project and it's again the most
stable, production-proven and advanced tool that is currently out there
with no comparable alternatives in other languages.

>>> Configuration

The configuration file `lint-staged.config.js` is placed in the project
root and includes the command that should be run for matching file
extensions (globs). Initialliy it includes the following three entries
with the same order as listed here:

1. `prettier --check` - Runs Prettier [5] (GH-13)
   to ensure all files are formatted correctly.
   The `--list-different` prints files that are not conform with the
   Prettier configuration.
2. `remark --no-stdout` - Runs remark-lint [6] (GH-15) against `*.md` to
   ensure all Markdown files are compliant to the style guide.
   The `--no-stdout` flag suppresses the output of the parsed file
   content.

[1]: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
[2]: https://danger.systems
[3]: https://nodejs.org
[4]: https://github.com/okonet/lint-staged
[5]: https://prettier.io
[6]: https://github.com/remarkjs/remark-lint

Closes GH-17
  • Loading branch information
svengreb committed Aug 22, 2020
1 parent 5ce5cca commit 55cad3c
Show file tree
Hide file tree
Showing 3 changed files with 597 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2020-present Sven Greb <[email protected]>
* This source code is licensed under the MIT license found in the LICENSE file.
*/

/**
* The configuration for lint-staged.
*
* @see https://github.com/okonet/lint-staged#configuration
*/
module.exports = {
"*": "prettier --check",
"*.md": "remark --no-stdout",
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"lint:pretty": "prettier --check ."
},
"devDependencies": {
"lint-staged": "^10.2.11",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"remark-preset-lint-arcticicestudio": ">=0.3.0 <1.0.0"
Expand Down
Loading

0 comments on commit 55cad3c

Please sign in to comment.