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: sort husky hooks #103

Merged
merged 6 commits into from
Dec 30, 2019
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
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