Skip to content

Commit b49a5a3

Browse files
committed
fix(config): handle missing config better, close #111
1 parent fc06129 commit b49a5a3

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/pre-git.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const log = require('debug')(packageName);
1414
var Promise = require('bluebird');
1515

1616
var label = 'pre-commit:';
17+
// magic value, meaning we found package.json but it has no "config" object
18+
const MISSING_GIT_CONFIG = 'MISSING_GIT_CONFIG';
1719

1820
var gitPrefix = process.env.GIT_PREFIX || '';
1921
log('git prefix env', process.env.GIT_PREFIX);
@@ -67,7 +69,7 @@ function findPackage(dir) {
6769
if (hasPreGitInFile(filename)) {
6870
log('found pre-git dependency in %s', filename);
6971
log('but no pre-git config');
70-
return;
72+
return MISSING_GIT_CONFIG;
7173
}
7274
}
7375

@@ -84,6 +86,9 @@ function findPackage(dir) {
8486
function getPackage() {
8587
var filename = findPackage();
8688
la(check.unemptyString(filename), 'could not find package');
89+
if (filename === MISSING_GIT_CONFIG) {
90+
return MISSING_GIT_CONFIG;
91+
}
8792
var pkg = require(filename);
8893
return pkg;
8994
}
@@ -180,6 +185,9 @@ function failure(label, err) {
180185

181186
function getConfig() {
182187
const pkg = getPackage();
188+
if (pkg === MISSING_GIT_CONFIG) {
189+
return;
190+
}
183191
return pkg.config && pkg.config[packageName];
184192
}
185193

@@ -212,14 +220,14 @@ function hasEnabledOption(config) {
212220

213221
function getTasks(label) {
214222
log('getting tasks with label "%s"', label);
215-
var pkg = getPackage();
216-
la(check.object(pkg), 'missing package', pkg);
217-
218223
const config = getConfig();
219224
if (!config) {
220225
return;
221226
}
222227

228+
var pkg = getPackage();
229+
la(check.object(pkg), 'missing package', pkg);
230+
223231
if (hasEnabledOption(config) && !config.enabled) {
224232
return;
225233
}

test/e2e.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ echo "Installing git hooks"
2323
# or we can install current dev source
2424
npm install $preGitFolder
2525
echo "Package.json after installing pre-git"
26+
27+
# add pre-commit sample command
28+
sed 's/"pre-commit": \[\]/"pre-commit": \["DEBUG=pre-git echo pre-commit testing..."\]/g' package.json > package.out
29+
mv package.out package.json
2630
cat package.json
2731

2832
# let us commit the code

0 commit comments

Comments
 (0)