Skip to content

Commit ddebf46

Browse files
fiskerkeithamus
andcommittedDec 30, 2019
feat: sort husky hooks (#103)
* 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>
1 parent df1c4e2 commit ddebf46

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed
 

Diff for: ‎index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const sortObjectKeys = require('sort-object-keys')
33
const detectIndent = require('detect-indent')
44
const detectNewline = require('detect-newline').graceful
55
const globby = require('globby')
6+
const gitHooks = require('git-hooks-list')
67

78
const onArray = fn => x => (Array.isArray(x) ? fn(x) : x)
89
const uniq = onArray(xs => xs.filter((x, i) => i === xs.indexOf(x)))
@@ -22,6 +23,9 @@ const sortDirectories = sortObjectBy([
2223
'example',
2324
'test',
2425
])
26+
const sortProperty = (property, over) => object =>
27+
Object.assign(object, { [property]: over(object[property]) })
28+
const sortGitHooks = sortObjectBy(gitHooks)
2529

2630
// See https://docs.npmjs.com/misc/scripts
2731
const defaultNpmScripts = new Set([
@@ -117,7 +121,7 @@ const fields = [
117121
},
118122
{ key: 'scripts', over: sortScripts },
119123
{ key: 'betterScripts', over: sortScripts },
120-
{ key: 'husky' },
124+
{ key: 'husky', over: sortProperty('hooks', sortGitHooks) },
121125
{ key: 'pre-commit' },
122126
{ key: 'commitlint', over: sortObject },
123127
{ key: 'lint-staged', over: sortObject },

Diff for: ‎package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"dependencies": {
5252
"detect-indent": "^6.0.0",
5353
"detect-newline": "3.1.0",
54+
"git-hooks-list": "1.0.1",
5455
"globby": "10.0.1",
5556
"sort-object-keys": "^1.1.3"
5657
},

Diff for: ‎test.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { execFile } = require('child_process')
66

77
const UNKNOWN = 'UNKNOWN_KEY_OR_VALUE'
88
function testField(name, tests, options) {
9-
for (const { value, expect, message } of tests) {
9+
for (const { value, expect, message, property } of tests) {
1010
const packageJson = {
1111
[name]: value,
1212
}
@@ -28,12 +28,10 @@ ${output}
2828
Message:
2929
${message || defaultMessage}
3030
`
31+
const object = property ? sorted[name][property] : sorted[name]
32+
const actual = Array.isArray(value) ? object : Object.keys(object)
3133

32-
if (Array.isArray(value)) {
33-
assert.deepStrictEqual(sorted[name], expect, detail)
34-
} else if (value && typeof value === 'object') {
35-
assert.deepStrictEqual(Object.keys(sorted[name]), expect, detail)
36-
}
34+
assert.deepStrictEqual(actual, expect, detail)
3735
}
3836
}
3937

@@ -234,7 +232,6 @@ for (const field of [
234232
'examplestyle',
235233
'assets',
236234
'workspaces',
237-
'husky',
238235
'pre-commit',
239236
'browserslist',
240237
'eslintIgnore',
@@ -258,6 +255,20 @@ for (const field of [
258255
])
259256
}
260257

258+
testField('husky', [
259+
{
260+
property: 'hooks',
261+
value: {
262+
hooks: {
263+
'commit-msg': '',
264+
[UNKNOWN]: UNKNOWN,
265+
'pre-commit': '',
266+
},
267+
},
268+
expect: ['pre-commit', 'commit-msg', UNKNOWN],
269+
},
270+
])
271+
261272
testField('binary', [
262273
{
263274
value: {

0 commit comments

Comments
 (0)
Please sign in to comment.