Skip to content

Commit 5e20d2e

Browse files
committed
Implement basic support for config file
(SUMMARY) * Implements loading of optional config file `pre-commit.json` inside the root directory of the git repository. * The contents of `pre-commit.json` will override whatever the user has specified in the `pre-commit` or `precommit` property inside package.json, everything else will behave the same.
1 parent e04c467 commit 5e20d2e

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

index.js

Lines changed: 17 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,19 @@ 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 preCommitConfig
188+
189+
try {
190+
const raw = fs.readFileSync(path.join(this.root, 'pre-commit.json'), 'utf-8').toString()
191+
preCommitConfig = JSON.parse(raw)
192+
} catch (err) {}
193+
194+
if (typeof preCommitConfig === 'object' && !Array.isArray(preCommitConfig)) {
195+
this.configFile = preCommitConfig
196+
}
197+
}
198+
183199
try {
184200
this.json = require(path.join(this.root, 'package.json'))
185201
this.parse()

test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ t.test('pre-commit', function (t) {
2424
})
2525

2626
t.test('#parse', function (t) {
27-
t.plan(7)
27+
t.plan(8)
2828

2929
let hook
3030

@@ -140,6 +140,30 @@ t.test('pre-commit', function (t) {
140140

141141
t.strictSame(typeof hook.config.run, 'undefined')
142142
})
143+
144+
t.test('overrides the `pre-commit` config property in package.json with the config inside pre-commit.json if it exists', function (t) {
145+
t.plan(1)
146+
147+
const fs = require('fs')
148+
const { existsSync, readFileSync } = fs
149+
150+
fs.existsSync = function existsSyncMock () {
151+
return true
152+
}
153+
154+
fs.readFileSync = function readFileSyncMock () {
155+
return Buffer.from(JSON.stringify({ run: ['lint', 'bench'] }))
156+
}
157+
158+
hook = new Hook(function () {}, { ignorestatus: true })
159+
160+
// ----
161+
t.same(hook.config.run, ['lint', 'bench'])
162+
163+
// Restoring mocks
164+
fs.existsSync = existsSync
165+
fs.readFileSync = readFileSync
166+
})
143167
})
144168

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

0 commit comments

Comments
 (0)