From b49a5a3b7515be19aed8867986d3d79cac34cf45 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Tue, 1 Aug 2017 16:28:57 -0400 Subject: [PATCH] fix(config): handle missing config better, close #111 --- src/pre-git.js | 16 ++++++++++++---- test/e2e.sh | 4 ++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/pre-git.js b/src/pre-git.js index 8e81657..59457ff 100644 --- a/src/pre-git.js +++ b/src/pre-git.js @@ -14,6 +14,8 @@ const log = require('debug')(packageName); var Promise = require('bluebird'); var label = 'pre-commit:'; +// magic value, meaning we found package.json but it has no "config" object +const MISSING_GIT_CONFIG = 'MISSING_GIT_CONFIG'; var gitPrefix = process.env.GIT_PREFIX || ''; log('git prefix env', process.env.GIT_PREFIX); @@ -67,7 +69,7 @@ function findPackage(dir) { if (hasPreGitInFile(filename)) { log('found pre-git dependency in %s', filename); log('but no pre-git config'); - return; + return MISSING_GIT_CONFIG; } } @@ -84,6 +86,9 @@ function findPackage(dir) { function getPackage() { var filename = findPackage(); la(check.unemptyString(filename), 'could not find package'); + if (filename === MISSING_GIT_CONFIG) { + return MISSING_GIT_CONFIG; + } var pkg = require(filename); return pkg; } @@ -180,6 +185,9 @@ function failure(label, err) { function getConfig() { const pkg = getPackage(); + if (pkg === MISSING_GIT_CONFIG) { + return; + } return pkg.config && pkg.config[packageName]; } @@ -212,14 +220,14 @@ function hasEnabledOption(config) { function getTasks(label) { log('getting tasks with label "%s"', label); - var pkg = getPackage(); - la(check.object(pkg), 'missing package', pkg); - const config = getConfig(); if (!config) { return; } + var pkg = getPackage(); + la(check.object(pkg), 'missing package', pkg); + if (hasEnabledOption(config) && !config.enabled) { return; } diff --git a/test/e2e.sh b/test/e2e.sh index 787bd8c..857a613 100755 --- a/test/e2e.sh +++ b/test/e2e.sh @@ -23,6 +23,10 @@ echo "Installing git hooks" # or we can install current dev source npm install $preGitFolder echo "Package.json after installing pre-git" + +# add pre-commit sample command +sed 's/"pre-commit": \[\]/"pre-commit": \["DEBUG=pre-git echo pre-commit testing..."\]/g' package.json > package.out +mv package.out package.json cat package.json # let us commit the code