Skip to content

Commit 5ad0ec2

Browse files
committed
Merge branch 'MSE99-feat-more-opts#18' into main
2 parents 61389c1 + 6968da2 commit 5ad0ec2

6 files changed

Lines changed: 109 additions & 2 deletions

File tree

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ jobs:
5353
needs: build
5454
runs-on: ubuntu-latest
5555
steps:
56+
<<<<<<< HEAD
5657
- uses: fastify/github-action-merge-dependabot@v2.2.0
58+
=======
59+
- uses: fastify/github-action-merge-dependabot@v2.1.1
60+
>>>>>>> 5e5da89adac942de074e909b4b65a8fcb2e53e72
5761
if: ${{ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request' }}
5862
with:
5963
github-token: ${{secrets.GITHUB_TOKEN}}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22
npm-debug.log
33
coverage
44
.tern-port
5+
.nyc_output
6+
package-lock.json

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ or as `"pre-commit.{key}` key properties in the `package.json`:
113113
}
114114
```
115115

116+
Configuration can also be defined inside a standalone `.pre-commit.json` config file:
117+
118+
```js
119+
{
120+
"silent": true,
121+
"colors": true,
122+
"template": "./temp",
123+
"run": [
124+
"lint",
125+
"test"
126+
]
127+
}
128+
```
129+
130+
The contents of `.pre-commit.json` will be used in the place of whatever was defined in the `pre-commit`
131+
or `precommit` property inside `package.json`.
132+
116133
It's all the same. Different styles so use what matches your project. To learn
117134
more about the scripts, please read the official `npm` documentation:
118135

index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const path = require('path')
66
const util = require('util')
77
const tty = require('tty')
88

9+
const fs = require('fs')
10+
911
/**
1012
* Representation of a hook runner.
1113
*
@@ -20,6 +22,7 @@ function Hook (fn, options) {
2022

2123
this.options = options // Used for testing only. Ignore this. Don't touch.
2224
this.config = {} // pre-commit configuration from the `package.json`.
25+
this.configFile = null // Actual contents of `pre-commit.json` if the user created one.
2326
this.json = {} // Actual content of the `package.json`.
2427
this.npm = '' // The location of the `npm` binary.
2528
this.git = '' // The location of the `git` binary.
@@ -77,7 +80,7 @@ Hook.prototype.exec = function exec (bin, args) {
7780
* @api private
7881
*/
7982
Hook.prototype.parse = function parse () {
80-
const pre = this.json['pre-commit'] || this.json.precommit
83+
const pre = this.configFile || this.json['pre-commit'] || this.json.precommit
8184
const config = !Array.isArray(pre) && typeof pre === 'object' ? pre : {};
8285

8386
['silent', 'colors', 'template'].forEach(function each (flag) {
@@ -180,6 +183,21 @@ Hook.prototype.initialize = function initialize () {
180183
this.status = this.status.stdout.toString().trim()
181184
this.root = this.root.stdout.toString().trim()
182185

186+
if (fs.existsSync(path.join(this.root, '.pre-commit.json'))) {
187+
let configFile
188+
189+
try {
190+
const rawText = fs.readFileSync(path.join(this.root, '.pre-commit.json'), 'utf-8').toString()
191+
configFile = JSON.parse(rawText)
192+
} catch (e) {
193+
return this.log(this.format(Hook.log.preCommitConfig, e.message), 1)
194+
}
195+
196+
if (typeof configFile === 'object' && !Array.isArray(configFile)) {
197+
this.configFile = configFile
198+
}
199+
}
200+
183201
try {
184202
this.json = require(path.join(this.root, 'package.json'))
185203
this.parse()
@@ -282,6 +300,14 @@ Hook.log = {
282300
'Skipping the pre-commit hook.'
283301
].join('\n'),
284302

303+
preCommitConfig: [
304+
'Received an error while parsing or locating the `.pre-commit.json` file:',
305+
'',
306+
' %s',
307+
'',
308+
'Skipping the pre-commit hook.'
309+
].join('\n'),
310+
285311
run: [
286312
'We have nothing pre-commit hooks to run. Either you\'re missing the `scripts`',
287313
'in your `package.json` or have configured pre-commit to run nothing.',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"devDependencies": {
3838
"pre-commit": "git://github.com/observing/pre-commit.git",
39+
"proxyquire": "^2.1.3",
3940
"standard": "^16.0.3",
4041
"tap": "^15.0.9"
4142
}

test.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const Hook = require('./')
44
const tty = require('tty')
55
const ttySupportColor = tty.isatty(process.stdout.fd)
66

7+
const proxyquire = require('proxyquire')
8+
79
t.test('pre-commit', function (t) {
810
t.plan(5)
911

@@ -24,7 +26,7 @@ t.test('pre-commit', function (t) {
2426
})
2527

2628
t.test('#parse', function (t) {
27-
t.plan(7)
29+
t.plan(9)
2830

2931
let hook
3032

@@ -140,6 +142,61 @@ t.test('pre-commit', function (t) {
140142

141143
t.strictSame(typeof hook.config.run, 'undefined')
142144
})
145+
146+
t.test('overrides the `pre-commit` config property in package.json with the config inside `.pre-commit.json` if it exists', function (t) {
147+
t.plan(1)
148+
149+
const Hook = proxyquire('.', {
150+
fs: {
151+
existsSync () {
152+
return true
153+
},
154+
readFileSync () {
155+
const rawText = JSON.stringify({ run: ['lint', 'bench'] })
156+
return Buffer.from(rawText)
157+
}
158+
}
159+
})
160+
161+
hook = new Hook(function () {}, { ignorestatus: true })
162+
163+
// ----
164+
t.same(hook.config.run, ['lint', 'bench'])
165+
})
166+
167+
t.test('should properly handle errors while trying to read and parse the contents of `.pre-commit.json`', function (t) {
168+
t.plan(4)
169+
170+
let Hook = proxyquire('.', {
171+
fs: {
172+
existsSync () {
173+
return true
174+
},
175+
readFileSync () {
176+
throw new Error()
177+
}
178+
}
179+
})
180+
181+
hook = new Hook(exit)
182+
183+
Hook = proxyquire('.', {
184+
fs: {
185+
existsSync () { return true },
186+
readFileSync () {
187+
return Buffer.from('{ "bad": [json }')
188+
}
189+
}
190+
})
191+
192+
hook = new Hook(exit)
193+
194+
// *****************
195+
function exit (code, lines) {
196+
t.not(lines.length, 0)
197+
t.equal(code, 1)
198+
}
199+
})
143200
})
144201

145202
t.test('#log', function (t) {

0 commit comments

Comments
 (0)