Skip to content

Commit

Permalink
feat: sort husky hooks (#103)
Browse files Browse the repository at this point in the history
* feat: sort husky hooks

* refactor: use `git-hooks-list`

* refactor: field property test

* fix: update `git-hooks-list`

Co-authored-by: Keith Cirkel <keithamus@users.noreply.github.com>
  • Loading branch information
fisker and keithamus committed Dec 30, 2019
1 parent df1c4e2 commit ddebf46
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const sortObjectKeys = require('sort-object-keys')
const detectIndent = require('detect-indent')
const detectNewline = require('detect-newline').graceful
const globby = require('globby')
const gitHooks = require('git-hooks-list')

const onArray = fn => x => (Array.isArray(x) ? fn(x) : x)
const uniq = onArray(xs => xs.filter((x, i) => i === xs.indexOf(x)))
Expand All @@ -22,6 +23,9 @@ const sortDirectories = sortObjectBy([
'example',
'test',
])
const sortProperty = (property, over) => object =>
Object.assign(object, { [property]: over(object[property]) })
const sortGitHooks = sortObjectBy(gitHooks)

// See https://docs.npmjs.com/misc/scripts
const defaultNpmScripts = new Set([
Expand Down Expand Up @@ -117,7 +121,7 @@ const fields = [
},
{ key: 'scripts', over: sortScripts },
{ key: 'betterScripts', over: sortScripts },
{ key: 'husky' },
{ key: 'husky', over: sortProperty('hooks', sortGitHooks) },
{ key: 'pre-commit' },
{ key: 'commitlint', over: sortObject },
{ key: 'lint-staged', over: sortObject },
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"dependencies": {
"detect-indent": "^6.0.0",
"detect-newline": "3.1.0",
"git-hooks-list": "1.0.1",
"globby": "10.0.1",
"sort-object-keys": "^1.1.3"
},
Expand Down
25 changes: 18 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { execFile } = require('child_process')

const UNKNOWN = 'UNKNOWN_KEY_OR_VALUE'
function testField(name, tests, options) {
for (const { value, expect, message } of tests) {
for (const { value, expect, message, property } of tests) {
const packageJson = {
[name]: value,
}
Expand All @@ -28,12 +28,10 @@ ${output}
Message:
${message || defaultMessage}
`
const object = property ? sorted[name][property] : sorted[name]
const actual = Array.isArray(value) ? object : Object.keys(object)

if (Array.isArray(value)) {
assert.deepStrictEqual(sorted[name], expect, detail)
} else if (value && typeof value === 'object') {
assert.deepStrictEqual(Object.keys(sorted[name]), expect, detail)
}
assert.deepStrictEqual(actual, expect, detail)
}
}

Expand Down Expand Up @@ -234,7 +232,6 @@ for (const field of [
'examplestyle',
'assets',
'workspaces',
'husky',
'pre-commit',
'browserslist',
'eslintIgnore',
Expand All @@ -258,6 +255,20 @@ for (const field of [
])
}

testField('husky', [
{
property: 'hooks',
value: {
hooks: {
'commit-msg': '',
[UNKNOWN]: UNKNOWN,
'pre-commit': '',
},
},
expect: ['pre-commit', 'commit-msg', UNKNOWN],
},
])

testField('binary', [
{
value: {
Expand Down

0 comments on commit ddebf46

Please sign in to comment.