Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: opt-in git hooks #333

Merged
merged 5 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 1 addition & 3 deletions src/commands/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const { reporter, prompt } = require('@dhis2/cli-helpers-engine')

const { log } = reporter

const { configure } = require('../utils/config.js')
const { printGroups, projects } = require('../utils/groups.js')

Expand Down Expand Up @@ -36,7 +34,7 @@ exports.builder = {

exports.handler = async argv => {
if (argv.listGroups) {
log.print(printGroups())
reporter.print(printGroups())
process.exit(0)
}

Expand Down
22 changes: 8 additions & 14 deletions src/utils/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,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 @@ -14,8 +14,6 @@ const {
LOCAL_ESLINT_REACT_CONFIG,
LOCAL_PRETTIER_CONFIG,
LOCAL_ESLINT_CONFIG,
LOCAL_HUSKY_CONFIG,
LOCAL_HUSKY_FRONTEND_CONFIG,
} = require('./paths.js')

/**
Expand All @@ -24,7 +22,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 @@ -58,12 +56,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 @@ -102,15 +99,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 @@ -120,7 +117,7 @@ const projects = [
'github/all',
'linter/eslint-react',
'formatter/prettier',
'git/husky-frontend',
'git-hooks/commit-msg',
],
],
]
Expand Down Expand Up @@ -295,9 +292,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 @@ -12,7 +12,6 @@ const {

const {
BROWSERSLIST_CONFIG,
HUSKY_CONFIG,
STALE_CONFIG,
DEPENDABOT_CONFIG,
EDITORCONFIG_CONFIG,
Expand Down Expand Up @@ -77,8 +76,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 @@ -89,12 +88,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