Skip to content

Commit

Permalink
feat: opt-in git hooks (#333)
Browse files Browse the repository at this point in the history
It is possible to install all available git hooks with:

    d2-style install git-hooks/all

Or specific hooks with e.g.

    d2-style install git-hooks/commit-msg
  • Loading branch information
varl committed Mar 26, 2021
1 parent a8a168b commit f7bde48
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 51 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ are not necessary to explicitly install:
## Usage

```
npx d2-style --help
yarn d2-style --help
npx d2-style install --help
yarn d2-style install --help
npx d2-style js --help
npx d2-style js check --help
npx d2-style js apply --help
yarn d2-style js --help
yarn d2-style js check --help
yarn d2-style js apply --help
```

## Report an issue
Expand Down
2 changes: 2 additions & 0 deletions config/git/hooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
yarn d2-style commit check "$1"
6 changes: 4 additions & 2 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ Some wisdom from Go:
# I'm getting husky errors

This library depends on husky, which requires node >= 10 and git >= 2.13.0. If you're getting errors like this:
This library as of version 8.0.0 no longer depends on husky, so if you
are getting errors like:

```
Can't find Husky, skipping pre-commit hook
You can reinstall it using 'npm install husky --save-dev' or delete this hook
```

Ensure that your node and git versions satisfy the above requirements. You can check this by running `git --version` and `node --version` from your terminal.
You can remove the husky hooks from `.git/hooks` and opt in to use the
hooks provided here with e.g. `d2-style install git-hooks/all`.
19 changes: 7 additions & 12 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ repository for you. For a list of valid groups and what tools they will
configure, use:

```bash
npx d2-style install --list-groups
yarn d2-style install --list-groups
```

Run `install` without arguments to get an interactive mode where it is
Expand All @@ -65,16 +65,11 @@ specification and get the standard
[EditorConfig](https://editorconfig.org/):

```
npx d2-style install project/base
yarn d2-style install project/base
# * project/base (includes: tools/editorconfig, git/husky)
# * project/base (includes: tools/editorconfig, git-hooks/commit-msg)
```

This gives you a starting point, and after that it is possible to extend
the `.huskyrc.js` configuration with specific hooks, see
[config/husky-frontend.local.js](config/husky-frontend.local.js) for an
example on how to extend the configuration file for Husky.

For example, most repos has structured text in the form of YAML or
Markdown, so adding a pre-commit hook to validate the format of that
makes sense in most cases.
Expand All @@ -87,10 +82,10 @@ configuration. It does not use any framework specific rules and should
be applicable to any JavaScript project.

```
npx d2-style install project/js
yarn d2-style install project/js
# * project/js (includes: tools/all, github/all, linter/eslint,
# formatter/prettier, git/husky-frontend)
# formatter/prettier, git/commit-msg)
```

### React
Expand All @@ -99,8 +94,8 @@ The `project/react` should be a good starting point for a React project,
as it adds `eslint-plugin-react`.

```
npx d2-style install project/react
yarn d2-style install project/react
# * project/react (includes: base/all, github/all, linter/eslint-react,
# formatter/prettier, git/husky-frontend)
# formatter/prettier, git/commit-msg)
```
6 changes: 3 additions & 3 deletions docs/migrate-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ it, only to install it fresh after.

```bash
# no assumption about the project
npx d2-style install project/base --force
yarn d2-style install project/base --force
# assuming a javascript project
npx d2-style install project/js --force
yarn d2-style install project/js --force
# assuming a react project
npx d2-style install project/react --force
yarn d2-style install project/react --force
```

(You did take a backup of modified configuration files earlier, right?)
Expand Down
22 changes: 8 additions & 14 deletions src/utils/groups.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const path = require('path')
const {
BROWSERSLIST_CONFIG,
COMMIT_MSG_HOOK,
ESLINT_CONFIG,
ESLINT_REACT_CONFIG,
HUSKY_CONFIG,
PRETTIER_CONFIG,
STALE_CONFIG,
DEPENDABOT_CONFIG,
Expand All @@ -12,8 +12,6 @@ const {
LOCAL_ESLINT_REACT_CONFIG,
LOCAL_PRETTIER_CONFIG,
LOCAL_ESLINT_CONFIG,
LOCAL_HUSKY_CONFIG,
LOCAL_HUSKY_FRONTEND_CONFIG,
} = require('./paths.js')

/**
Expand All @@ -22,7 +20,7 @@ const {
*
* The format of a selector is: 'identifier/specifier', e.g.
*
* git/husky
* git/hooks
*
* This is represented by a multi-dimensional array:
*
Expand Down Expand Up @@ -56,12 +54,11 @@ const groups = [
[['prettier', [LOCAL_PRETTIER_CONFIG, path.join('.prettierrc.js')]]],
],
[
'git',
'git-hooks',
[
['husky', [LOCAL_HUSKY_CONFIG, path.join('.huskyrc.js')]],
[
'husky-frontend',
[LOCAL_HUSKY_FRONTEND_CONFIG, path.join('.huskyrc.js')],
'commit-msg',
[COMMIT_MSG_HOOK, path.join('.git', 'hooks', 'commit-msg')],
],
],
],
Expand Down Expand Up @@ -100,15 +97,15 @@ const groups = [
* wants to bundle, acting as a short-hand.
*/
const projects = [
['base', ['tools/editorconfig', 'git/husky']],
['base', ['tools/editorconfig', 'git-hooks/commit-msg']],
[
'js',
[
'tools/all',
'github/all',
'linter/eslint',
'formatter/prettier',
'git/husky-frontend',
'git-hooks/commit-msg',
],
],
[
Expand All @@ -118,7 +115,7 @@ const projects = [
'github/all',
'linter/eslint-react',
'formatter/prettier',
'git/husky-frontend',
'git-hooks/commit-msg',
],
],
]
Expand Down Expand Up @@ -293,9 +290,6 @@ const bundledConfigPaths = () => {
config.eslintReact = ESLINT_REACT_CONFIG
config['eslint-react'] = ESLINT_REACT_CONFIG
break
case 'husky':
config.husky = HUSKY_CONFIG
break
default:
config[toolName] = sourceConfigPath
break
Expand Down
12 changes: 2 additions & 10 deletions src/utils/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ const EDITORCONFIG_CONFIG = path.join(CONFIG_DIR, 'editorconfig.config.rc')
const DEPENDABOT_CONFIG = path.join(CONFIG_DIR, 'github', 'dependabot.yml')
const STALE_CONFIG = path.join(CONFIG_DIR, 'github', 'stale.yml')
const SEMANTIC_PR_CONFIG = path.join(CONFIG_DIR, 'github', 'semantic.yml')

const HUSKY_CONFIG = path.join(CONFIG_DIR, 'husky.config.js')
const LOCAL_HUSKY_FRONTEND_CONFIG = path.join(
CONFIG_DIR,
'husky-frontend.local.js'
)
const LOCAL_HUSKY_CONFIG = path.join(CONFIG_DIR, 'husky.local.js')
const COMMIT_MSG_HOOK = path.join(CONFIG_DIR, 'git', 'hooks', 'commit-msg')

// local configuration files for repositories
const LOCAL_ESLINT_REACT_CONFIG = path.join(
Expand All @@ -43,6 +37,7 @@ const LOCAL_PRETTIER_CONFIG = path.join(CONFIG_DIR, 'js', 'prettier.local.js')

module.exports = {
CONSUMING_ROOT,
COMMIT_MSG_HOOK,
BROWSERSLIST_CONFIG,
COMMITLINT_CONFIG,
PACKAGE_ROOT,
Expand All @@ -57,7 +52,4 @@ module.exports = {
PRETTIER_CONFIG,
SEMANTIC_PR_CONFIG,
STALE_CONFIG,
HUSKY_CONFIG,
LOCAL_HUSKY_CONFIG,
LOCAL_HUSKY_FRONTEND_CONFIG,
}
8 changes: 3 additions & 5 deletions tests/group-resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const {
} = require('../src/utils/groups.js')
const {
BROWSERSLIST_CONFIG,
HUSKY_CONFIG,
STALE_CONFIG,
DEPENDABOT_CONFIG,
EDITORCONFIG_CONFIG,
Expand Down Expand Up @@ -75,8 +74,8 @@ test('a valid project can be resolved into groups', t => {
test('valid groups can be expanded', t => {
t.plan(1)

const comboGroup = 'git/all'
const targetGroups = findGroup('git')
const comboGroup = 'git-hooks/all'
const targetGroups = findGroup('git-hooks')
const expandedGroups = expandGroupAll(comboGroup)

t.deepEqual(
Expand All @@ -87,12 +86,11 @@ test('valid groups can be expanded', t => {
})

test('convert to tool:config object', t => {
t.plan(8)
t.plan(7)

const obj = bundledConfigPaths()

t.equals(obj.eslint, ESLINT_CONFIG)
t.equals(obj.husky, HUSKY_CONFIG)
t.equals(obj.prettier, PRETTIER_CONFIG)
t.equals(obj.dependabot, DEPENDABOT_CONFIG)
t.equals(obj['probot-stale'], STALE_CONFIG)
Expand Down

0 comments on commit f7bde48

Please sign in to comment.