Skip to content

Commit

Permalink
feat: refactoring file structure, readme docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lotyp committed Feb 9, 2023
1 parent 5616f7a commit 8a16f5e
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 53 deletions.
3 changes: 0 additions & 3 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# run directly, without docker support
# pnpm commitlint --edit "${1}"

# through docker-compose
pnpm commitlint --edit "${1}"
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"deps:update": "pnpm dlx npm-check-updates@latest --configFileName .ncurc.yml -u --deep --mergeConfig && pnpm install",
"preinstall": "npx only-allow pnpm",
"lint": "pnpm run lint:md && pnpm run lint:js && pnpm run lint:secrets",
"lint:commits": "pnpm commitlint --from HEAD~1 --to HEAD --verbose",
"lint:fix": "turbo run lint:fix && pnpm lint:package-json",
"lint:js": "eslint --fix **/*.js",
"lint:md": "markdownlint --fix **/*.md --ignore '**/node_modules/**' --ignore '**/CHANGELOG.md'",
Expand Down
3 changes: 1 addition & 2 deletions packages/browserslist-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<img width="456" src="https://raw.githubusercontent.com/wayofdev/next-starter-tpl/master/assets/logo.gh-light-mode-only.png#gh-light-mode-only">
<img width="456" src="https://raw.githubusercontent.com/wayofdev/next-starter-tpl/master/assets/logo.gh-dark-mode-only.png#gh-dark-mode-only">
</div>

<br>

<br>
Expand Down Expand Up @@ -47,7 +46,7 @@ Add as dev-dependency to your monorepo
$ pnpm add -wD browserslist @wayofdev/browserslist-config
```

Assuming that you have the following structure:
This package should be added to the root of your monorepo, where you have a file `.browserslistrc` and a `package.json` file. Within your monorepo, you should have a structure with directories for your apps and packages, such as:

```bash
.
Expand Down
33 changes: 33 additions & 0 deletions packages/browserslist-config/lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-check

/**
* This files overrides the base lint-staged.config.js present in the root directory.
* It allows to run eslint based the package specific requirements.
* {@link https://github.com/okonet/lint-staged#how-to-use-lint-staged-in-a-multi-package-monorepo}
* {@link https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docs/about-lint-staged.md}
*/

const { concatFilesForPrettier, getEslintFixCmd } = require('../../lint-staged.common.js')

/**
* @type {Record<string, (filenames: string[]) => string | string[] | Promise<string | string[]>>}
*/
const rules = {
'**/*.{js,jsx,ts,tsx,mjs,cjs}': filenames => {
return getEslintFixCmd({
cwd: __dirname,
fix: true,
cache: true,
// when auto-fixing staged-files a good tip is to disable react-hooks/exhaustive-deps, cause
// a change here can potentially break things without proper visibility.
rules: ['react-hooks/exhaustive-deps: off'],
maxWarnings: 25,
files: filenames,
})
},
'**/*.{json,md,mdx,css,html,yml,yaml,scss}': filenames => {
return [`prettier --write ${concatFilesForPrettier(filenames)}`]
},
}

module.exports = rules
199 changes: 158 additions & 41 deletions packages/commitlint-config/README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,188 @@
# Commitlint Config
<br>

Shareable commitlint configuration.
<div align="center">
<img width="456" src="https://raw.githubusercontent.com/wayofdev/next-starter-tpl/master/assets/logo.gh-light-mode-only.png#gh-light-mode-only">
<img width="456" src="https://raw.githubusercontent.com/wayofdev/next-starter-tpl/master/assets/logo.gh-dark-mode-only.png#gh-dark-mode-only">
</div>

## Install
<br>

<br>

<div align="center">
<a href="https://actions-badge.atrox.dev/wayofdev/npm-shareable-configs/goto"><img alt="Build Status" src="https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fwayofdev%2Fnext-starter-tpl%2Fbadge&style=flat-square"/></a>
<a href="https://www.npmjs.com/package/@wayofdev/commitlint-config"><img alt="GitHub package.json version" src="https://img.shields.io/npm/v/@wayofdev/commitlint-config?style=flat-square"></a>
<a href="https://www.npmjs.com/package/@wayofdev/commitlint-config?activeTab=versions"><img alt="Downloads per month" src="https://img.shields.io/npm/dm/@wayofdev/commitlint-config?style=flat-square"></a>
<a href="LICENSE.md"><img src="https://img.shields.io/github/license/wayofdev/npm-shareable-configs.svg?style=flat-square&color=blue" alt="Software License"/></a>
</div>

<br>

# Shareable Commitlint Config

## 📄 About

A shareable [commitlint](https://commitlint.js.org/#/) configuration for enforcing consistent commit messages in your projects.

### → Purpose

Consistent commit messages are important for project collaboration, maintainability, and project history. This commitlint configuration provides a set of rules to ensure that all commits in your project follow a consistent structure, making it easier for your team to understand what changes were made and why.

<br>

To use this configuration, you'll need to install the `@commitlint/cli` package as a devDependency in your project:

```bash
# install with yarn
$ yarn add -D @commitlint/cli @wayofdev/commitlint-config
# Install as dev-dependency into root of monorepo
$ pnpm add -wD @commitlint/cli @wayofdev/commitlint-config
```

# install with pnpm
$ pnpm add -Dw @commitlint/cli @wayofdev/commitlint-config
This package should be added to the root of your monorepo, where you have a file `commitlint.config.js` and a `package.json` file. Within your monorepo, you should have a structure with directories for your apps and packages, such as:

```bash
.
├── commitlint.config.js (root)
├── package.json (root)
├── apps
│ └── my-first-app
│ ├── package.json
│ └── ... (other app files)
└── packages
└── my-first-package
├── package.json
└── ... (other package files)
```

## Usage
<br>

**`commitlint.config.js`**
### → Configure

```js
module.exports = {
extends: ['@wayofdev/commitlint-config'],
};
1. To configure the `commitlint.config.js` file, include the following line:

```javascript
module.exports = {
extends: ["@wayofdev/commitlint-config"],
}
```

This extends the `@wayofdev/commitlint-config` configuration and uses its [pre-defined configuration](https://github.com/wayofdev/npm-shareable-configs/blob/master/packages/commitlint-config/index.js).

Alternatively the configuration can be defined in a `commitlint.config.js`, `.commitlintrc.js`, `.commitlintrc`, `.commitlintrc.json`, `.commitlintrc.yml` file or a `commitlint` field in `package.json`.

2. Install Husky

Install `husky` as devDependency, a handy git hook helper available on npm.

```bash
# Install as dev-dependency into root of monorepo
$ pnpm add -wD husky is-ci

# Activate hooks
$ pnpm husky install
```

3. Add hook

```bash
$ npx husky add .husky/commit-msg 'pnpm commitlint --edit "${1}"'
```

4. Add scripts to `package.json`

```bash
$ pnpm pkg set scripts.lint:commits="pnpm commitlint --from HEAD~1 --to HEAD --verbose"
$ pnpm pkg set scripts.prepare="is-ci || husky install"
```

<br>

## 💻 Usage

### → Test simple usage

For a first simple usage test of commitlint you can do the following:

```bash
# using pnpm
$ pnpm commitlint --from HEAD~1 --to HEAD --verbose

# or, using npx
$ npx commitlint --from HEAD~1 --to HEAD --verbose

# or, if script was added
$ pnpm lint:commits
```

This will check your last commit and return an error if invalid or a positive output if valid.

### → Test the hook

You can test the hook by simply committing. You should see something like this if everything works.

```bash
$ git commit -m "foo: this will fail"
husky > commit-msg (node v10.1.0)
No staged files match any of provided globs.
⧗ input: foo: this will fail
type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]

✖ found 1 problems, 0 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

husky > commit-msg hook failed (add --no-verify to bypass)
```

## Extending
<br>

## 🛠️ Extending

This shows, how config can be extended with your custom rules.

**`commitlint.config.js`**

```js
module.exports = {
extends: ['@wayofdev/commitlint-config'],
rules: {
'body-leading-blank': [2, 'always'],
},
};
extends: ["@wayofdev/commitlint-config"],
rules: {
"body-leading-blank": [2, "always"],
},
}
```

## Add a Husky Hook
<br>

Install husky:
## 🤝 License

```bash
# using yarn
$ yarn dlx husky-init --yarn2 && yarn && npm pkg set scripts.prepare="husky install" && yarn prepare
[![Licence](https://img.shields.io/github/license/wayofdev/npm-shareable-configs?style=for-the-badge&color=blue)](./LICENSE)

# using pnpm
$ pnpm dlx husky-init && pnpm install && pnpm pkg set scripts.prepare="husky install" && pnpm prepare
```
<br>

Add the hook:
## 🧱 Credits and Useful Resources

```bash
# using npm
$ npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "${1}"'
Based on:

# using yarn
$ yarn dlx husky add .husky/pre-commit 'npx --no -- commitlint --edit "${1}"'
- [shareable-configs](https://github.com/waldronmatt/shareable-configs) from [waldronmatt](https://github.com/waldronmatt)

# using pnpm
$ pnpm dlx husky add .husky/pre-commit 'npx --no -- commitlint --edit "${1}"'
```
Guides:

- [Official commitlint setup guide](https://commitlint.js.org/#/./guides-local-setup?id=guides-local-setup)

<br>

## 🙆🏼‍♂️ Author Information

This repository was created in **2023** by [lotyp / wayofdev](https://github.com/wayofdev).

## Under The Hood
<br>

### `index.js`
## 🙌 Want to Contribute?

- `@commitlint/config-conventional`
Thank you for considering contributing to the wayofdev community!
We are open to all kinds of contributions. If you want to:

## License
- 🤔 Suggest a feature
- 🐛 Report an issue
- 📖 Improve documentation
- 👨‍💻 Contribute to the code

MIT
You are more than welcome. Before contributing, kindly check our [guidelines](https://next-starter-tpl-docs.wayof.dev/contribution).
33 changes: 33 additions & 0 deletions packages/commitlint-config/lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @ts-check

/**
* This files overrides the base lint-staged.config.js present in the root directory.
* It allows to run eslint based the package specific requirements.
* {@link https://github.com/okonet/lint-staged#how-to-use-lint-staged-in-a-multi-package-monorepo}
* {@link https://github.com/belgattitude/nextjs-monorepo-example/blob/main/docs/about-lint-staged.md}
*/

const { concatFilesForPrettier, getEslintFixCmd } = require('../../lint-staged.common.js')

/**
* @type {Record<string, (filenames: string[]) => string | string[] | Promise<string | string[]>>}
*/
const rules = {
'**/*.{js,jsx,ts,tsx,mjs,cjs}': filenames => {
return getEslintFixCmd({
cwd: __dirname,
fix: true,
cache: true,
// when auto-fixing staged-files a good tip is to disable react-hooks/exhaustive-deps, cause
// a change here can potentially break things without proper visibility.
rules: ['react-hooks/exhaustive-deps: off'],
maxWarnings: 25,
files: filenames,
})
},
'**/*.{json,md,mdx,css,html,yml,yaml,scss}': filenames => {
return [`prettier --write ${concatFilesForPrettier(filenames)}`]
},
}

module.exports = rules
22 changes: 18 additions & 4 deletions packages/commitlint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,32 @@
"directory": "packages/commitlint-config"
},
"license": "MIT",
"author": "[email protected]",
"main": "index.js",
"author": {
"name": "Andrij Orlenko",
"email": "[email protected]",
"url": "https://github.com/lotyp"
},
"exports": {
".": {
"require": "./src/index.js"
}
},
"main": "src/index.js",
"files": [
"index.js",
"src/*",
"scripts/*"
],
"scripts": {
"test": "echo \"No test specified\""
"clean": "rimraf ./dist ./coverage ./tsconfig.tsbuildinfo",
"lint": "eslint --ext .ts,.js,.cjs,.mjs --cache --cache-location ../../.cache/eslint/commitlint-config.eslintcache",
"lint:fix": "eslint --ext .ts,.tsx,.js,.jsx,.mjs,.cjs,.mts,.cts --fix --cache --cache-location ../../.cache/eslint/commitlint-config.eslintcache"
},
"dependencies": {
"@commitlint/config-conventional": "^17.4.2"
},
"devDependencies": {
"rimraf": "^4.1.2"
},
"peerDependencies": {
"@commitlint/cli": ">= 17"
},
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/eslint-config-bases/lint-staged.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const { concatFilesForPrettier, getEslintFixCmd } = require('../../lint-staged.c
* @type {Record<string, (filenames: string[]) => string | string[] | Promise<string | string[]>>}
*/
const rules = {
'**/*.{js,jsx,ts,tsx}': filenames => {
'**/*.{js,jsx,ts,tsx,mjs,cjs}': filenames => {
return getEslintFixCmd({
cwd: __dirname,
fix: true,
Expand Down
3 changes: 1 addition & 2 deletions packages/eslint-config-bases/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"eslint-sonar",
"eslint-storybook",
"eslint-tailwind",
"eslint-tailwindcss",
"eslint-typescript"
"eslint-tailwindcss"
],
"homepage": "https://github.com/wayofdev/npm-shareable-configs/tree/master/packages/eslint-config-bases#readme",
"bugs": {
Expand Down
Loading

0 comments on commit 8a16f5e

Please sign in to comment.