Skip to content

Commit 14e3f45

Browse files
feat: add pre-commit hooks using husky
Based on usage in City-of-Helsinki/palvelutarjotin-ui@0d9089a refs HCRC-187
1 parent da90a99 commit 14e3f45

7 files changed

Lines changed: 1090 additions & 11 deletions

File tree

.husky/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/commit-msg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx --no-install commitlint --edit "$1"

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
yarn doctoc . -u
2+
yarn lint-staged --relative

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
# React Helsinki Headless CMS
22

3+
React UI component library to visualize [Headless CMS](https://github.com/City-of-Helsinki/headless-cms)
4+
data using [Helsinki Design System](https://github.com/City-of-Helsinki/helsinki-design-system).
5+
6+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
7+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
8+
**Table of Contents**
9+
10+
- [Introduction](#introduction)
11+
- [The known clients that are using this library](#the-known-clients-that-are-using-this-library)
12+
- [Installation](#installation)
13+
- [Development](#development)
14+
- [When to develop](#when-to-develop)
15+
- [Available scripts](#available-scripts)
16+
- [Development environments](#development-environments)
17+
- [Module structure](#module-structure)
18+
- [Husky Git Hooks](#husky-git-hooks)
19+
- [Pre-commit Hook](#pre-commit-hook)
20+
- [Commit-msg Hook](#commit-msg-hook)
21+
- [CI](#ci)
22+
- [CD](#cd)
23+
- [Storybook](#storybook)
24+
- [Apollo](#apollo)
25+
- [NextJS](#nextjs)
26+
- [Use as a application dependency](#use-as-a-application-dependency)
27+
- [Build](#build)
28+
- [Releasing new versions](#releasing-new-versions)
29+
- [Testing](#testing)
30+
- [Testing in IDE terminal](#testing-in-ide-terminal)
31+
- [Usage](#usage)
32+
- [Publishing new versions](#publishing-new-versions)
33+
- [Known issues](#known-issues)
34+
35+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
36+
337
## Introduction
438

539
React Helsinki Headless CMS - is a highly customized component library based on [HDS](https://github.com/City-of-Helsinki/helsinki-design-system). It is designed for Helsinki City Web applications which are using preconfigured Wordpress Headless CMS environments (compatible with the library). This library is a set of unified visual components for Pages, Artciles, Artcicle Archives which provide:
@@ -81,6 +115,36 @@ This library consists of three modules.
81115
- Apollo module that wraps core module components with logic that is able to fetch data with the help of an `ApolloClient` instance.
82116
- Nextjs module that provides utilities when working with `Nextjs` and `Apollo`.
83117

118+
### Husky Git Hooks
119+
120+
This project uses [Husky](https://typicode.github.io/husky/#/) to manage Git hooks. Husky is configured to run specific scripts before committing changes to ensure code quality and consistency.
121+
122+
#### Pre-commit Hook
123+
124+
The pre-commit hook is configured to run the following commands:
125+
126+
```sh
127+
yarn doctoc .
128+
yarn lint-staged --relative
129+
```
130+
131+
- `yarn doctoc .`: This command updates the table of contents in your markdown files.
132+
- `yarn lint-staged --relative`: This command runs linting on staged files to ensure they meet the project's coding standards. The lint-staged configuration can be found from [package.json](./package.json).
133+
- Using `--relative` flag to reduce command line length,
134+
as the combined length of all the absolute paths for a large commit can get quite long
135+
136+
> NOTE: `doctoc` and `husky` does not work seamlessly together, since the `doctoc` does update the TOCs of the markdown files, but does not reject the pre-commit hook execution, and only leaves the refactored files as unstaged in Git.
137+
138+
#### Commit-msg Hook
139+
140+
The commit-msg hook is configured to run the following command:
141+
142+
```sh
143+
npx --no-install commitlint --edit "$1"
144+
```
145+
146+
- `npx --no-install commitlint --edit "$1"`: This command uses [Commitlint](https://commitlint.js.org/#/) to lint commit messages based on the project's commit message conventions. This repo follows the [Conventional Commits](#conventional-commits).
147+
84148
### CI
85149

86150
Checks

commitlint.config.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default {
2+
extends: ['@commitlint/config-conventional'],
3+
ignores: [(message) => message.includes('Signed-off-by: dependabot[bot]')],
4+
rules: {
5+
'header-max-length': [2, 'always', 72],
6+
'body-max-line-length': [2, 'always', 72],
7+
'body-leading-blank': [2, 'always'],
8+
'type-enum': [
9+
2,
10+
'always',
11+
[
12+
'build',
13+
'chore',
14+
'ci',
15+
'deps',
16+
'docs',
17+
'feat',
18+
'fix',
19+
'perf',
20+
'refactor',
21+
'revert',
22+
'style',
23+
'test',
24+
],
25+
],
26+
},
27+
};

package.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
"module": "index.js",
77
"types": "index.d.ts",
88
"scripts": {
9+
"prepare": "husky",
910
"dev": "yarn storybook",
1011
"dev:no-open": "yarn storybook --no-open",
1112
"typecheck": "tsc --project ./tsconfig.json --noEmit",
1213
"lint": "yarn eslint \"src/**/*.{ts,tsx}\" && yarn prettier ./src -c",
1314
"lint-fix": "yarn eslint \"src/**/*.{ts,tsx}\" --fix && yarn prettier ./src -w",
1415
"test": "jest",
16+
"test:staged": "jest --findRelatedTests --passWithNoTests",
1517
"test-storybook": "test-storybook",
1618
"build": "rollup -c --bundleConfigAsCjs",
1719
"docker:dev": "cross-env DOCKER_TARGET=development docker compose up",
@@ -65,6 +67,8 @@
6567
"@babel/preset-react": "^7.25.9",
6668
"@babel/preset-typescript": "^7.26.0",
6769
"@chromatic-com/storybook": "^4.1.2",
70+
"@commitlint/cli": "^20.2.0",
71+
"@commitlint/config-conventional": "^20.2.0",
6872
"@graphql-codegen/cli": "^5.0.3",
6973
"@graphql-codegen/typescript": "^4.1.1",
7074
"@graphql-codegen/typescript-operations": "^4.3.1",
@@ -109,10 +113,12 @@
109113
"git-rev-sync": "^3.0.2",
110114
"graphql": "^16.9.0",
111115
"hds-react": "4.8.1",
116+
"husky": "^9.1.7",
112117
"identity-obj-proxy": "^3.0.0",
113118
"jest": "^29.7.0",
114119
"jest-axe": "^9.0.0",
115120
"jest-fetch-mock": "^3.0.3",
121+
"lint-staged": "^16.2.7",
116122
"msw-storybook-addon": "^2.0.6",
117123
"postcss": "^8.4.49",
118124
"postcss-scss": "^4.0.9",
@@ -139,6 +145,7 @@
139145
"dependencies": {
140146
"classnames": "^2.5.1",
141147
"date-fns": "^4.1.0",
148+
"doctoc": "^2.2.1",
142149
"hds-design-tokens": "^4.8.1",
143150
"html-entities": "^2.5.2",
144151
"html-react-parser": "^4.2.9",
@@ -150,5 +157,12 @@
150157
},
151158
"msw": {
152159
"workerDirectory": "public"
160+
},
161+
"lint-staged": {
162+
"src/**/*.{js,jsx,ts,tsx,cjs,mjs}": [
163+
"eslint --fix",
164+
"prettier --write",
165+
"yarn test:staged"
166+
]
153167
}
154-
}
168+
}

0 commit comments

Comments
 (0)